1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

adt: add queue (#12941)

This commit is contained in:
Hunam 2021-12-23 17:57:15 +01:00 committed by GitHub
parent 54a6973548
commit 68452cff76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 5 deletions

View File

@ -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)
- [ ] ...

36
vlib/adt/queue.v Normal file
View File

@ -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()
}

49
vlib/adt/queue_test.v Normal file
View File

@ -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
}