adt: add queue (#12941)
							parent
							
								
									54a6973548
								
							
						
					
					
						commit
						68452cff76
					
				| 
						 | 
				
			
			@ -1,10 +1,27 @@
 | 
			
		|||
# adt
 | 
			
		||||
# datatypes
 | 
			
		||||
 | 
			
		||||
A library providing common ADTs (Abstract Data Type) implementations which have proved useful in a
 | 
			
		||||
great variety of applications.
 | 
			
		||||
This module provides implementations of less frequently used, but still common
 | 
			
		||||
data types.
 | 
			
		||||
 | 
			
		||||
## implementations
 | 
			
		||||
V's `builtin` module is imported implicitly, and has implementations for arrays,
 | 
			
		||||
maps and strings. These are good for many applications, but there are a plethora
 | 
			
		||||
of other useful data structures/containers, like linked lists, priority queues,
 | 
			
		||||
tries, etc, that allow for algorithms with different time complexities, which may
 | 
			
		||||
be more suitable for your specific application.
 | 
			
		||||
 | 
			
		||||
It is implemented using generics, that you have to specialise for the type of
 | 
			
		||||
your actual elements. For example:
 | 
			
		||||
```v
 | 
			
		||||
import adt
 | 
			
		||||
 | 
			
		||||
mut stack := adt.Stack<int>{}
 | 
			
		||||
stack.push(1)
 | 
			
		||||
println(stack)
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Currently Implemented Datatypes:
 | 
			
		||||
 | 
			
		||||
- [x] Stack (LIFO)
 | 
			
		||||
- [x] Linked list
 | 
			
		||||
- [x] Stack (LIFO)
 | 
			
		||||
- [x] Queue (FIFO)
 | 
			
		||||
- [ ] ...
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
module adt
 | 
			
		||||
 | 
			
		||||
pub struct Queue<T> {
 | 
			
		||||
mut:
 | 
			
		||||
	elements LinkedList<T>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// is_empty checks if the queue is empty
 | 
			
		||||
pub fn (queue Queue<T>) is_empty() bool {
 | 
			
		||||
	return queue.elements.is_empty()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// len returns the length of the queue
 | 
			
		||||
pub fn (queue Queue<T>) len() int {
 | 
			
		||||
	return queue.elements.len()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// peek returns the head of the queue
 | 
			
		||||
pub fn (queue Queue<T>) peek() ?T {
 | 
			
		||||
	return if !queue.is_empty() { queue.elements.first() ? } else { error('Queue is empty') }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// push adds an element to the tail of the queue
 | 
			
		||||
pub fn (mut queue Queue<T>) push(item T) {
 | 
			
		||||
	queue.elements.push(item)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// pop removes the element at the head of the queue and returns it
 | 
			
		||||
pub fn (mut queue Queue<T>) pop() ?T {
 | 
			
		||||
	return if !queue.is_empty() { queue.elements.shift() ? } else { error('Queue is empty') }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// str returns a string representation of the queue
 | 
			
		||||
pub fn (queue Queue<T>) str() string {
 | 
			
		||||
	return queue.elements.str()
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,49 @@
 | 
			
		|||
module adt
 | 
			
		||||
 | 
			
		||||
fn test_is_empty() {
 | 
			
		||||
	mut queue := Queue<int>{}
 | 
			
		||||
	assert queue.is_empty() == true
 | 
			
		||||
	queue.push(1)
 | 
			
		||||
	assert queue.is_empty() == false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_len() ? {
 | 
			
		||||
	mut queue := Queue<int>{}
 | 
			
		||||
	assert queue.len() == 0
 | 
			
		||||
	queue.push(1)
 | 
			
		||||
	assert queue.len() == 1
 | 
			
		||||
	queue.pop() ?
 | 
			
		||||
	assert queue.len() == 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_peek() ? {
 | 
			
		||||
	mut queue := Queue<int>{}
 | 
			
		||||
	queue.push(1)
 | 
			
		||||
	assert queue.peek() ? == 1
 | 
			
		||||
	queue.push(2)
 | 
			
		||||
	assert queue.peek() ? == 1
 | 
			
		||||
	queue = Queue<int>{}
 | 
			
		||||
	queue.peek() or { return }
 | 
			
		||||
	assert false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_push() ? {
 | 
			
		||||
	mut queue := Queue<int>{}
 | 
			
		||||
	queue.push(1)
 | 
			
		||||
	queue.push(2)
 | 
			
		||||
	assert queue.peek() ? == 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_pop() ? {
 | 
			
		||||
	mut queue := Queue<int>{}
 | 
			
		||||
	queue.push(1)
 | 
			
		||||
	queue.push(2)
 | 
			
		||||
	queue.push(3)
 | 
			
		||||
	assert queue.pop() ? == 1
 | 
			
		||||
	queue.push(4)
 | 
			
		||||
	assert queue.pop() ? == 2
 | 
			
		||||
	assert queue.pop() ? == 3
 | 
			
		||||
	queue = Queue<int>{}
 | 
			
		||||
	queue.pop() or { return }
 | 
			
		||||
	assert false
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue