| 1 | // Copyright (c) 2017, Benjamin Scher Purcell. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package avltree |
| 6 | |
| 7 | import "github.com/emirpasic/gods/v2/containers" |
| 8 | |
| 9 | // Assert Iterator implementation |
| 10 | var _ containers.ReverseIteratorWithKey[string, int] = (*Iterator[string, int])(nil) |
| 11 | |
| 12 | // Iterator holding the iterator's state |
| 13 | type Iterator[K comparable, V any] struct { |
| 14 | tree *Tree[K, V] |
| 15 | node *Node[K, V] |
| 16 | position position |
| 17 | } |
| 18 | |
| 19 | type position byte |
| 20 | |
| 21 | const ( |
| 22 | begin, between, end position = 0, 1, 2 |
| 23 | ) |
| 24 | |
| 25 | // Iterator returns a stateful iterator whose elements are key/value pairs. |
| 26 | func (tree *Tree[K, V]) Iterator() *Iterator[K, V] { |
| 27 | return &Iterator[K, V]{tree: tree, node: nil, position: begin} |
| 28 | } |
| 29 | |
| 30 | // Next moves the iterator to the next element and returns true if there was a next element in the container. |
| 31 | // If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 32 | // If Next() was called for the first time, then it will point the iterator to the first element if it exists. |
| 33 | // Modifies the state of the iterator. |
| 34 | func (iterator *Iterator[K, V]) Next() bool { |
| 35 | switch iterator.position { |
| 36 | case begin: |
| 37 | iterator.position = between |
| 38 | iterator.node = iterator.tree.Left() |
| 39 | case between: |
| 40 | iterator.node = iterator.node.Next() |
| 41 | } |
| 42 | |
| 43 | if iterator.node == nil { |
| 44 | iterator.position = end |
| 45 | return false |
| 46 | } |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | // Prev moves the iterator to the next element and returns true if there was a previous element in the container. |
| 51 | // If Prev() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 52 | // If Prev() was called for the first time, then it will point the iterator to the first element if it exists. |
| 53 | // Modifies the state of the iterator. |
| 54 | func (iterator *Iterator[K, V]) Prev() bool { |
| 55 | switch iterator.position { |
| 56 | case end: |
| 57 | iterator.position = between |
| 58 | iterator.node = iterator.tree.Right() |
| 59 | case between: |
| 60 | iterator.node = iterator.node.Prev() |
| 61 | } |
| 62 | |
| 63 | if iterator.node == nil { |
| 64 | iterator.position = begin |
| 65 | return false |
| 66 | } |
| 67 | return true |
| 68 | } |
| 69 | |
| 70 | // Value returns the current element's value. |
| 71 | // Does not modify the state of the iterator. |
| 72 | func (iterator *Iterator[K, V]) Value() (v V) { |
| 73 | if iterator.node == nil { |
| 74 | return v |
| 75 | } |
| 76 | return iterator.node.Value |
| 77 | } |
| 78 | |
| 79 | // Key returns the current element's key. |
| 80 | // Does not modify the state of the iterator. |
| 81 | func (iterator *Iterator[K, V]) Key() (k K) { |
| 82 | if iterator.node == nil { |
| 83 | return k |
| 84 | } |
| 85 | return iterator.node.Key |
| 86 | } |
| 87 | |
| 88 | // Node returns the current element's node. |
| 89 | // Does not modify the state of the iterator. |
| 90 | func (iterator *Iterator[K, V]) Node() *Node[K, V] { |
| 91 | return iterator.node |
| 92 | } |
| 93 | |
| 94 | // Begin resets the iterator to its initial state (one-before-first) |
| 95 | // Call Next() to fetch the first element if any. |
| 96 | func (iterator *Iterator[K, V]) Begin() { |
| 97 | iterator.node = nil |
| 98 | iterator.position = begin |
| 99 | } |
| 100 | |
| 101 | // End moves the iterator past the last element (one-past-the-end). |
| 102 | // Call Prev() to fetch the last element if any. |
| 103 | func (iterator *Iterator[K, V]) End() { |
| 104 | iterator.node = nil |
| 105 | iterator.position = end |
| 106 | } |
| 107 | |
| 108 | // First moves the iterator to the first element and returns true if there was a first element in the container. |
| 109 | // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). |
| 110 | // Modifies the state of the iterator |
| 111 | func (iterator *Iterator[K, V]) First() bool { |
| 112 | iterator.Begin() |
| 113 | return iterator.Next() |
| 114 | } |
| 115 | |
| 116 | // Last moves the iterator to the last element and returns true if there was a last element in the container. |
| 117 | // If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). |
| 118 | // Modifies the state of the iterator. |
| 119 | func (iterator *Iterator[K, V]) Last() bool { |
| 120 | iterator.End() |
| 121 | return iterator.Prev() |
| 122 | } |
| 123 | |
| 124 | // NextTo moves the iterator to the next element from current position that satisfies the condition given by the |
| 125 | // passed function, and returns true if there was a next element in the container. |
| 126 | // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 127 | // Modifies the state of the iterator. |
| 128 | func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool { |
| 129 | for iterator.Next() { |
| 130 | key, value := iterator.Key(), iterator.Value() |
| 131 | if f(key, value) { |
| 132 | return true |
| 133 | } |
| 134 | } |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the |
| 139 | // passed function, and returns true if there was a next element in the container. |
| 140 | // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). |
| 141 | // Modifies the state of the iterator. |
| 142 | func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool { |
| 143 | for iterator.Prev() { |
| 144 | key, value := iterator.Key(), iterator.Value() |
| 145 | if f(key, value) { |
| 146 | return true |
| 147 | } |
| 148 | } |
| 149 | return false |
| 150 | } |