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

datatypes: change optional to result (#16546)

This commit is contained in:
yuyi 2022-11-28 16:24:47 +08:00 committed by GitHub
parent 1d24dbe602
commit 7d8c386721
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 179 additions and 179 deletions

View File

@ -256,13 +256,13 @@ fn (bst &BSTree[T]) get_node(node &BSTreeNode[T], value T) &BSTreeNode[T] {
//```v
// left_value, exist := bst.to_left(10)
//```
pub fn (bst &BSTree[T]) to_left(value T) ?T {
pub fn (bst &BSTree[T]) to_left(value T) !T {
if bst.is_empty() {
return none
return error('BSTree is empty')
}
node := bst.get_node(bst.root, value)
if !node.is_init {
return none
return error('BSTree is not initialized')
}
left_node := node.left
return left_node.value
@ -275,13 +275,13 @@ pub fn (bst &BSTree[T]) to_left(value T) ?T {
//```v
// left_value, exist := bst.to_right(10)
//```
pub fn (bst &BSTree[T]) to_right(value T) ?T {
pub fn (bst &BSTree[T]) to_right(value T) !T {
if bst.is_empty() {
return none
return error('BSTree is empty')
}
node := bst.get_node(bst.root, value)
if !node.is_init {
return none
return error('BSTree is not initialized')
}
right_node := node.right
return right_node.value
@ -289,26 +289,26 @@ pub fn (bst &BSTree[T]) to_right(value T) ?T {
// max return the max element inside the BST.
// Time complexity O(N) if the BST is not balanced
pub fn (bst &BSTree[T]) max() ?T {
pub fn (bst &BSTree[T]) max() !T {
if bst.is_empty() {
return none
return error('BSTree is empty')
}
max := bst.get_max_from_right(bst.root)
if !max.is_init {
return none
return error('BSTree is not initialized')
}
return max.value
}
// min return the minimum element in the BST.
// Time complexity O(N) if the BST is not balanced.
pub fn (bst &BSTree[T]) min() ?T {
pub fn (bst &BSTree[T]) min() !T {
if bst.is_empty() {
return none
return error('BSTree is empty')
}
min := bst.get_min_from_left(bst.root)
if !min.is_init {
return none
return error('BSTree is not initialized')
}
return min.value
}

View File

@ -30,7 +30,7 @@ pub fn (list DoublyLinkedList[T]) len() int {
}
// first returns the first element of the linked list
pub fn (list DoublyLinkedList[T]) first() ?T {
pub fn (list DoublyLinkedList[T]) first() !T {
if list.is_empty() {
return error('Linked list is empty')
}
@ -38,7 +38,7 @@ pub fn (list DoublyLinkedList[T]) first() ?T {
}
// last returns the last element of the linked list
pub fn (list DoublyLinkedList[T]) last() ?T {
pub fn (list DoublyLinkedList[T]) last() !T {
if list.is_empty() {
return error('Linked list is empty')
}
@ -80,7 +80,7 @@ pub fn (mut list DoublyLinkedList[T]) push_front(item T) {
}
// pop_back removes the last element of the linked list
pub fn (mut list DoublyLinkedList[T]) pop_back() ?T {
pub fn (mut list DoublyLinkedList[T]) pop_back() !T {
if list.is_empty() {
return error('Linked list is empty')
}
@ -101,7 +101,7 @@ pub fn (mut list DoublyLinkedList[T]) pop_back() ?T {
}
// pop_front removes the last element of the linked list
pub fn (mut list DoublyLinkedList[T]) pop_front() ?T {
pub fn (mut list DoublyLinkedList[T]) pop_front() !T {
if list.is_empty() {
return error('Linked list is empty')
}
@ -122,7 +122,7 @@ pub fn (mut list DoublyLinkedList[T]) pop_front() ?T {
}
// insert adds an element to the linked list at the given index
pub fn (mut list DoublyLinkedList[T]) insert(idx int, item T) ? {
pub fn (mut list DoublyLinkedList[T]) insert(idx int, item T) ! {
if idx < 0 || idx > list.len {
return error('Index ${idx} out of bounds [0..${list.len}]')
} else if idx == 0 {
@ -212,7 +212,7 @@ fn (list &DoublyLinkedList[T]) node(idx int) &DoublyListNode[T] {
// index searches the linked list for item and returns the forward index
// or none if not found.
pub fn (list &DoublyLinkedList[T]) index(item T) ?int {
pub fn (list &DoublyLinkedList[T]) index(item T) !int {
mut hn := list.head
mut tn := list.tail
for h, t := 0, list.len - 1; h <= t; {
@ -226,7 +226,7 @@ pub fn (list &DoublyLinkedList[T]) index(item T) ?int {
t -= 1
tn = tn.prev
}
return none
return error('none')
}
// delete removes index idx from the linked list and is safe to call

View File

@ -7,118 +7,118 @@ fn test_is_empty() {
assert list.is_empty() == false
}
fn test_len() ? {
fn test_len() {
mut list := DoublyLinkedList[int]{}
assert list.len() == 0
list.push_back(1)
assert list.len() == 1
list.pop_back()?
list.pop_back()!
assert list.len() == 0
}
fn test_first() ? {
fn test_first() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
assert list.first()? == 1
assert list.first()! == 1
list.push_back(2)
assert list.first()? == 1
assert list.first()! == 1
list = DoublyLinkedList[int]{}
list.first() or { return }
assert false
}
fn test_last() ? {
fn test_last() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
assert list.last()? == 1
assert list.last()! == 1
list.push_back(2)
assert list.last()? == 2
assert list.last()! == 2
list = DoublyLinkedList[int]{}
list.last() or { return }
assert false
}
fn test_push() ? {
fn test_push() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
assert list.last()? == 1
assert list.last()! == 1
list.push_back(2)
assert list.last()? == 2
assert list.last()! == 2
list.push_back(3)
assert list.last()? == 3
assert list.last()! == 3
}
fn test_pop() ? {
fn test_pop() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
list.push_back(3)
assert list.pop_back()? == 3
assert list.pop_back()! == 3
list.push_back(4)
assert list.pop_back()? == 4
assert list.pop_back()? == 2
assert list.pop_back()! == 4
assert list.pop_back()! == 2
list = DoublyLinkedList[int]{}
list.pop_back() or { return }
assert false
}
fn test_pop_front() ? {
fn test_pop_front() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
list.push_back(3)
assert list.pop_front()? == 1
assert list.pop_front()! == 1
list.push_back(4)
assert list.pop_front()? == 2
assert list.pop_front()? == 3
assert list.pop_front()! == 2
assert list.pop_front()! == 3
list = DoublyLinkedList[int]{}
list.pop_front() or { return }
assert false
}
fn test_insert() ? {
fn test_insert() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
list.push_back(3)
// [1, 2, 3]
list.insert(1, 111)?
list.insert(1, 111)!
// [1, 111, 2, 3]
list.insert(3, 222)?
list.insert(3, 222)!
// [1, 111, 2, 222, 3]
assert list.pop_back()? == 3
assert list.pop_back()? == 222
assert list.pop_front()? == 1
assert list.pop_front()? == 111
assert list.pop_back()! == 3
assert list.pop_back()! == 222
assert list.pop_front()! == 1
assert list.pop_front()! == 111
}
fn test_push_front() ? {
fn test_push_front() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
list.push_back(3)
list.push_front(111)
assert list.first()? == 111
assert list.first()! == 111
}
fn test_delete() ? {
fn test_delete() {
mut list := DoublyLinkedList[int]{}
list.push_back(0)
list.push_back(1)
list.push_back(2)
list.delete(1)
assert list.first()? == 0
assert list.last()? == 2
assert list.first()! == 0
assert list.last()! == 2
assert list.len() == 2
list.delete(1)
assert list.first()? == 0
assert list.last()? == 0
assert list.first()! == 0
assert list.last()! == 0
assert list.len() == 1
list.delete(0)
assert list.len() == 0
}
fn test_iter() ? {
fn test_iter() {
mut list := DoublyLinkedList[int]{}
for i := 0; i < 10; i++ {
list.push_back(i * 10)
@ -140,18 +140,18 @@ fn test_iter() ? {
assert count == 10
}
fn test_index() ? {
fn test_index() {
mut list := DoublyLinkedList[int]{}
for i := 0; i < 10; i++ {
list.push_back(i * 10)
}
for i := 0; i < 10; i++ {
assert list.index(i * 10)? == i
assert list.index(i * 10)! == i
}
}
fn test_str() ? {
fn test_str() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
@ -159,7 +159,7 @@ fn test_str() ? {
assert list.str() == '[1, 2, 3]'
}
fn test_array() ? {
fn test_array() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
@ -167,7 +167,7 @@ fn test_array() ? {
assert list.array() == [1, 2, 3]
}
fn test_string_array() ? {
fn test_string_array() {
mut list := DoublyLinkedList[[]string]{}
list.push_back(['a'])
list.push_back(['b'])
@ -175,7 +175,7 @@ fn test_string_array() ? {
assert list.array() == [['a'], ['b'], ['c']]
}
fn test_iteration_with_for() ? {
fn test_iteration_with_for() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
@ -187,7 +187,7 @@ fn test_iteration_with_for() ? {
assert res == [1, 2, 3]
}
fn test_iterator() ? {
fn test_iterator() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)
@ -200,7 +200,7 @@ fn test_iterator() ? {
assert res == [1, 2, 3]
}
fn test_back_iterator() ? {
fn test_back_iterator() {
mut list := DoublyLinkedList[int]{}
list.push_back(1)
list.push_back(2)

View File

@ -21,9 +21,9 @@ pub fn (mut heap MinHeap[T]) insert(item T) {
}
// pop removes the top-most element from the heap.
pub fn (mut heap MinHeap[T]) pop() ?T {
pub fn (mut heap MinHeap[T]) pop() !T {
if heap.data.len == 0 {
return none
return error('Heap is empty')
} else if heap.data.len == 1 {
return heap.data.pop()
}
@ -46,9 +46,9 @@ pub fn (mut heap MinHeap[T]) pop() ?T {
}
// peek gets the top-most element from the heap without removing it.
pub fn (heap MinHeap[T]) peek() ?T {
pub fn (heap MinHeap[T]) peek() !T {
if heap.data.len == 0 {
return none
return error('Heap is empty')
}
return heap.data[0]
}
@ -60,20 +60,20 @@ pub fn (heap MinHeap[T]) len() int {
// left_child is a helper function that returns the index of the left
// child given a parent idx, or none if there is no left child.
fn (heap MinHeap[T]) left_child(idx int) ?int {
fn (heap MinHeap[T]) left_child(idx int) !int {
child := 2 * idx + 1
if child >= heap.data.len {
return none
return error('none')
}
return child
}
// right_child is a helper function that returns the index of the right
// child given a parent idx, or none if there is no right child.
fn (heap MinHeap[T]) right_child(idx int) ?int {
fn (heap MinHeap[T]) right_child(idx int) !int {
child := 2 * idx + 2
if child >= heap.data.len {
return none
return error('none')
}
return child
}

View File

@ -1,6 +1,6 @@
module datatypes
fn test_min_heap() ? {
fn test_min_heap() {
mut heap := MinHeap[int]{}
heap.insert(2)
heap.insert(0)
@ -8,11 +8,11 @@ fn test_min_heap() ? {
heap.insert(4)
heap.insert(1)
assert heap.pop()? == 0
assert heap.pop()? == 1
assert heap.pop()? == 2
assert heap.pop()? == 4
assert heap.pop()? == 8
assert heap.pop()! == 0
assert heap.pop()! == 1
assert heap.pop()! == 2
assert heap.pop()! == 4
assert heap.pop()! == 8
if _ := heap.pop() {
panic('expected none')
}
@ -27,7 +27,7 @@ fn (lhs Item) < (rhs Item) bool {
return rhs.priority < lhs.priority
}
fn test_min_heap_custom() ? {
fn test_min_heap_custom() {
mut heap := MinHeap[Item]{}
heap.insert(Item{'buz', 10})
heap.insert(Item{'qux', 0})
@ -35,17 +35,17 @@ fn test_min_heap_custom() ? {
heap.insert(Item{'foo', 100})
heap.insert(Item{'bar', 80})
assert heap.pop()?.data == 'foo'
assert heap.pop()?.data == 'bar'
assert heap.pop()?.data == 'baz'
assert heap.pop()?.data == 'buz'
assert heap.pop()?.data == 'qux'
assert heap.pop()!.data == 'foo'
assert heap.pop()!.data == 'bar'
assert heap.pop()!.data == 'baz'
assert heap.pop()!.data == 'buz'
assert heap.pop()!.data == 'qux'
if _ := heap.pop() {
panic('expected none')
}
}
fn test_heap_len() ? {
fn test_heap_len() {
mut heap := MinHeap[int]{}
heap.insert(2)
assert heap.len() == 1
@ -56,12 +56,12 @@ fn test_heap_len() ? {
heap.insert(1)
assert heap.len() == 5
heap.pop()?
heap.pop()?
heap.pop()?
heap.pop()!
heap.pop()!
heap.pop()!
assert heap.len() == 2
heap.pop()?
heap.pop()?
heap.pop()!
heap.pop()!
assert heap.len() == 0
heap.pop() or {}
assert heap.len() == 0

View File

@ -27,12 +27,12 @@ pub fn (list LinkedList[T]) len() int {
}
// first returns the first element of the linked list
pub fn (list LinkedList[T]) first() ?T {
pub fn (list LinkedList[T]) first() !T {
return if !list.is_empty() { list.head.data } else { error('Linked list is empty') }
}
// last returns the last element of the linked list
pub fn (list LinkedList[T]) last() ?T {
pub fn (list LinkedList[T]) last() !T {
if unsafe { list.head == 0 } {
return error('Linked list is empty')
} else {
@ -45,7 +45,7 @@ pub fn (list LinkedList[T]) last() ?T {
}
// index returns the element at the given index of the linked list
pub fn (list LinkedList[T]) index(idx int) ?T {
pub fn (list LinkedList[T]) index(idx int) !T {
if unsafe { list.head == 0 } {
return error('Linked list is empty')
} else {
@ -82,7 +82,7 @@ pub fn (mut list LinkedList[T]) push(item T) {
}
// pop removes the last element of the linked list
pub fn (mut list LinkedList[T]) pop() ?T {
pub fn (mut list LinkedList[T]) pop() !T {
if unsafe { list.head == 0 } {
return error('Linked list is empty')
}
@ -105,7 +105,7 @@ pub fn (mut list LinkedList[T]) pop() ?T {
}
// shift removes the first element of the linked list
pub fn (mut list LinkedList[T]) shift() ?T {
pub fn (mut list LinkedList[T]) shift() !T {
if unsafe { list.head == 0 } {
return error('Linked list is empty')
} else {
@ -117,7 +117,7 @@ pub fn (mut list LinkedList[T]) shift() ?T {
}
// insert adds an element to the linked list at the given index
pub fn (mut list LinkedList[T]) insert(idx int, item T) ? {
pub fn (mut list LinkedList[T]) insert(idx int, item T) ! {
if idx < 0 || idx > list.len {
return error('Index ${idx} out of bounds [0..${list.len}]')
} else if list.len == 0 {

View File

@ -7,87 +7,87 @@ fn test_is_empty() {
assert list.is_empty() == false
}
fn test_len() ? {
fn test_len() {
mut list := LinkedList[int]{}
assert list.len() == 0
list.push(1)
assert list.len() == 1
list.pop()?
list.pop()!
assert list.len() == 0
}
fn test_first() ? {
fn test_first() {
mut list := LinkedList[int]{}
list.push(1)
assert list.first()? == 1
assert list.first()! == 1
list.push(2)
assert list.first()? == 1
assert list.first()! == 1
list = LinkedList[int]{}
list.first() or { return }
assert false
}
fn test_last() ? {
fn test_last() {
mut list := LinkedList[int]{}
list.push(1)
assert list.last()? == 1
assert list.last()! == 1
list.push(2)
assert list.last()? == 2
assert list.last()! == 2
list = LinkedList[int]{}
list.last() or { return }
assert false
}
fn test_index() ? {
fn test_index() {
mut list := LinkedList[int]{}
list.push(1)
assert list.index(0)? == 1
assert list.index(0)! == 1
list.push(2)
assert list.index(1)? == 2
list.pop()?
assert list.index(1)! == 2
list.pop()!
list.index(1) or { return }
assert false
}
fn test_push() ? {
fn test_push() {
mut list := LinkedList[int]{}
list.push(1)
assert list.last()? == 1
assert list.last()! == 1
list.push(2)
assert list.last()? == 2
assert list.last()! == 2
list.push(3)
assert list.last()? == 3
assert list.last()! == 3
}
fn test_pop() ? {
fn test_pop() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
list.push(3)
assert list.pop()? == 3
assert list.pop()! == 3
list.push(4)
assert list.pop()? == 4
assert list.pop()? == 2
assert list.pop()! == 4
assert list.pop()! == 2
list = LinkedList[int]{}
list.pop() or { return }
assert false
}
fn test_shift() ? {
fn test_shift() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
list.push(3)
assert list.shift()? == 1
assert list.shift()! == 1
list.push(4)
assert list.shift()? == 2
assert list.shift()? == 3
assert list.shift()! == 2
assert list.shift()! == 3
list = LinkedList[int]{}
list.shift() or { return }
assert false
}
fn test_insert() ? {
fn test_insert() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
@ -96,16 +96,16 @@ fn test_insert() ? {
assert true
}
fn test_prepend() ? {
fn test_prepend() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
list.push(3)
list.prepend(111)
assert list.first()? == 111
assert list.first()! == 111
}
fn test_str() ? {
fn test_str() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
@ -113,7 +113,7 @@ fn test_str() ? {
assert list.str() == '[1, 2, 3]'
}
fn test_array() ? {
fn test_array() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
@ -121,7 +121,7 @@ fn test_array() ? {
assert list.array() == [1, 2, 3]
}
fn test_linked_list_iterating_with_for() ? {
fn test_linked_list_iterating_with_for() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
@ -133,7 +133,7 @@ fn test_linked_list_iterating_with_for() ? {
assert res == [1, 2, 3]
}
fn test_linked_list_separate_iterators() ? {
fn test_linked_list_separate_iterators() {
mut list := LinkedList[int]{}
list.push(1)
list.push(2)
@ -141,10 +141,10 @@ fn test_linked_list_separate_iterators() ? {
mut it1 := list.iterator()
mut it2 := list.iterator()
mut it3 := list.iterator()
assert it1.next()? == 1
assert it1.next()? == 2
assert it1.next()? == 3
assert it2.next()? == 1
assert it1.next()! == 1
assert it1.next()! == 2
assert it1.next()! == 3
assert it2.next()! == 1
if _ := it1.next() {
assert false
} else {
@ -155,8 +155,8 @@ fn test_linked_list_separate_iterators() ? {
} else {
assert true
}
assert it2.next()? == 2
assert it2.next()? == 3
assert it2.next()! == 2
assert it2.next()! == 3
if _ := it2.next() {
assert false
} else {

View File

@ -16,17 +16,17 @@ pub fn (queue Queue[T]) len() int {
}
// peek returns the head of the queue (first element added)
pub fn (queue Queue[T]) peek() ?T {
pub fn (queue Queue[T]) peek() !T {
return queue.elements.first()
}
// last returns the tail of the queue (last element added)
pub fn (queue Queue[T]) last() ?T {
pub fn (queue Queue[T]) last() !T {
return queue.elements.last()
}
// index returns the element at the given index of the queue
pub fn (queue Queue[T]) index(idx int) ?T {
pub fn (queue Queue[T]) index(idx int) !T {
return queue.elements.index(idx)
}
@ -36,7 +36,7 @@ pub fn (mut queue Queue[T]) push(item T) {
}
// pop removes the element at the head of the queue and returns it
pub fn (mut queue Queue[T]) pop() ?T {
pub fn (mut queue Queue[T]) pop() !T {
return queue.elements.shift()
}

View File

@ -7,70 +7,70 @@ fn test_is_empty() {
assert queue.is_empty() == false
}
fn test_len() ? {
fn test_len() {
mut queue := Queue[int]{}
assert queue.len() == 0
queue.push(1)
assert queue.len() == 1
queue.pop()?
queue.pop()!
assert queue.len() == 0
}
fn test_peek() ? {
fn test_peek() {
mut queue := Queue[int]{}
queue.push(1)
assert queue.peek()? == 1
assert queue.peek()! == 1
queue.push(2)
assert queue.peek()? == 1
assert queue.peek()! == 1
queue = Queue[int]{}
queue.peek() or { return }
assert false
}
fn test_last() ? {
fn test_last() {
mut queue := Queue[int]{}
queue.push(1)
assert queue.last()? == 1
assert queue.last()! == 1
queue.push(2)
assert queue.last()? == 2
assert queue.last()! == 2
queue = Queue[int]{}
queue.last() or { return }
assert false
}
fn test_index() ? {
fn test_index() {
mut queue := Queue[int]{}
queue.push(1)
assert queue.index(0)? == 1
assert queue.index(0)! == 1
queue.push(2)
assert queue.index(1)? == 2
queue.pop()?
assert queue.index(1)! == 2
queue.pop()!
queue.index(1) or { return }
assert false
}
fn test_push() ? {
fn test_push() {
mut queue := Queue[int]{}
queue.push(1)
queue.push(2)
assert queue.peek()? == 1
assert queue.peek()! == 1
}
fn test_pop() ? {
fn test_pop() {
mut queue := Queue[int]{}
queue.push(1)
queue.push(2)
queue.push(3)
assert queue.pop()? == 1
assert queue.pop()! == 1
queue.push(4)
assert queue.pop()? == 2
assert queue.pop()? == 3
assert queue.pop()! == 2
assert queue.pop()! == 3
queue = Queue[int]{}
queue.pop() or { return }
assert false
}
fn test_array() ? {
fn test_array() {
mut queue := Queue[int]{}
queue.push(1)
queue.push(2)

View File

@ -18,7 +18,7 @@ pub fn new_ringbuffer[T](s int) RingBuffer[T] {
}
// push - adds an element to the ringbuffer
pub fn (mut rb RingBuffer[T]) push(element T) ? {
pub fn (mut rb RingBuffer[T]) push(element T) ! {
if rb.is_full() {
return error('Buffer overflow')
} else {
@ -28,7 +28,7 @@ pub fn (mut rb RingBuffer[T]) push(element T) ? {
}
// pop - returns the oldest element of the buffer
pub fn (mut rb RingBuffer[T]) pop() ?T {
pub fn (mut rb RingBuffer[T]) pop() !T {
mut v := rb.content[rb.reader]
if rb.is_empty() {
return error('Buffer is empty')
@ -39,14 +39,14 @@ pub fn (mut rb RingBuffer[T]) pop() ?T {
}
// push_many - pushes an array to the buffer
pub fn (mut rb RingBuffer[T]) push_many(elements []T) ? {
pub fn (mut rb RingBuffer[T]) push_many(elements []T) ! {
for v in elements {
rb.push(v) or { return err }
}
}
// pop_many - returns a given number(n) of elements of the buffer starting with the oldest one
pub fn (mut rb RingBuffer[T]) pop_many(n u64) ?[]T {
pub fn (mut rb RingBuffer[T]) pop_many(n u64) ![]T {
mut elements := []T{}
for _ in 0 .. n {
elements << rb.pop() or { return err }

View File

@ -21,7 +21,7 @@ pub fn (mut set Set[T]) remove(element T) {
}
// pick returns an arbitrary element of set, if set is not empty.
pub fn (set Set[T]) pick() ?T {
pub fn (set Set[T]) pick() !T {
for k, _ in set.elements {
return k
}
@ -29,14 +29,14 @@ pub fn (set Set[T]) pick() ?T {
}
// rest returns the set consisting of all elements except for the arbitrary element.
pub fn (mut set Set[T]) rest() ?[]T {
element := set.pick()?
pub fn (mut set Set[T]) rest() ![]T {
element := set.pick()!
return set.elements.keys().filter(it != element)
}
// pop returns an arbitrary element and deleting it from set.
pub fn (mut set Set[T]) pop() ?T {
element := set.pick()?
pub fn (mut set Set[T]) pop() !T {
element := set.pick()!
set.elements.delete(element)
return element
}

View File

@ -16,7 +16,7 @@ pub fn (stack Stack[T]) len() int {
}
// peek returns the top of the stack
pub fn (stack Stack[T]) peek() ?T {
pub fn (stack Stack[T]) peek() !T {
return if !stack.is_empty() { stack.elements.last() } else { error('Stack is empty') }
}
@ -26,7 +26,7 @@ pub fn (mut stack Stack[T]) push(item T) {
}
// pop removes the element at the top of the stack and returns it
pub fn (mut stack Stack[T]) pop() ?T {
pub fn (mut stack Stack[T]) pop() !T {
return if !stack.is_empty() { stack.elements.pop() } else { error('Stack is empty') }
}

View File

@ -7,51 +7,51 @@ fn test_is_empty() {
assert stack.is_empty() == false
}
fn test_len() ? {
fn test_len() {
mut stack := dt.Stack[int]{}
assert stack.len() == 0
stack.push(1)
assert stack.len() == 1
stack.pop()?
stack.pop()!
assert stack.len() == 0
}
fn test_peek() ? {
fn test_peek() {
mut stack := dt.Stack[int]{}
stack.push(1)
assert stack.peek()? == 1
assert stack.peek()! == 1
stack.push(2)
assert stack.peek()? == 2
assert stack.peek()! == 2
stack = dt.Stack[int]{}
stack.peek() or { return }
assert false
}
fn test_push() ? {
fn test_push() {
mut stack := dt.Stack[int]{}
stack.push(1)
assert stack.peek()? == 1
assert stack.peek()! == 1
stack.push(2)
assert stack.peek()? == 2
assert stack.peek()! == 2
stack.push(3)
assert stack.peek()? == 3
assert stack.peek()! == 3
}
fn test_pop() ? {
fn test_pop() {
mut stack := dt.Stack[int]{}
stack.push(1)
stack.push(2)
stack.push(3)
assert stack.pop()? == 3
assert stack.pop()! == 3
stack.push(4)
assert stack.pop()? == 4
assert stack.pop()? == 2
assert stack.pop()! == 4
assert stack.pop()! == 2
stack = dt.Stack[int]{}
stack.pop() or { return }
assert false
}
fn test_array() ? {
fn test_array() {
mut stack := dt.Stack[int]{}
stack.push(1)
stack.push(2)