github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/trees/redblacktree/iterator.go (about) 1 package redblacktree 2 3 import "github.com/songzhibin97/go-baseutils/structure/containers" 4 5 // Assert Iterator implementation 6 var _ containers.ReverseIteratorWithKey[any, any] = (*Iterator[any, any])(nil) 7 8 // Iterator holding the iterator's state 9 type Iterator[K, V any] struct { 10 tree *Tree[K, V] 11 node *Node[K, V] 12 position position 13 } 14 15 type position byte 16 17 const ( 18 begin, between, end position = 0, 1, 2 19 ) 20 21 // Iterator returns a stateful iterator whose elements are key/value pairs. 22 func (tree *Tree[K, V]) Iterator() Iterator[K, V] { 23 return Iterator[K, V]{tree: tree, node: nil, position: begin} 24 } 25 26 // IteratorAt returns a stateful iterator whose elements are key/value pairs that is initialised at a particular node. 27 func (tree *Tree[K, V]) IteratorAt(node *Node[K, V]) Iterator[K, V] { 28 return Iterator[K, V]{tree: tree, node: node, position: between} 29 } 30 31 // Next moves the iterator to the next element and returns true if there was a next element in the container. 32 // If Next() returns true, then next element's key and value can be retrieved by Key() and Value(). 33 // If Next() was called for the first time, then it will point the iterator to the first element if it exists. 34 // Modifies the state of the iterator. 35 func (iterator *Iterator[K, V]) Next() bool { 36 if iterator.position == end { 37 goto end 38 } 39 if iterator.position == begin { 40 left := iterator.tree.Left() 41 if left == nil { 42 goto end 43 } 44 iterator.node = left 45 goto between 46 } 47 if iterator.node.Right != nil { 48 iterator.node = iterator.node.Right 49 for iterator.node.Left != nil { 50 iterator.node = iterator.node.Left 51 } 52 goto between 53 } 54 for iterator.node.Parent != nil { 55 node := iterator.node 56 iterator.node = iterator.node.Parent 57 if node == iterator.node.Left { 58 goto between 59 } 60 } 61 62 end: 63 iterator.node = nil 64 iterator.position = end 65 return false 66 67 between: 68 iterator.position = between 69 return true 70 } 71 72 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. 73 // If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value(). 74 // Modifies the state of the iterator. 75 func (iterator *Iterator[K, V]) Prev() bool { 76 if iterator.position == begin { 77 goto begin 78 } 79 if iterator.position == end { 80 right := iterator.tree.Right() 81 if right == nil { 82 goto begin 83 } 84 iterator.node = right 85 goto between 86 } 87 if iterator.node.Left != nil { 88 iterator.node = iterator.node.Left 89 for iterator.node.Right != nil { 90 iterator.node = iterator.node.Right 91 } 92 goto between 93 } 94 for iterator.node.Parent != nil { 95 node := iterator.node 96 iterator.node = iterator.node.Parent 97 if node == iterator.node.Right { 98 goto between 99 } 100 } 101 102 begin: 103 iterator.node = nil 104 iterator.position = begin 105 return false 106 107 between: 108 iterator.position = between 109 return true 110 } 111 112 // Value returns the current element's value. 113 // Does not modify the state of the iterator. 114 func (iterator *Iterator[K, V]) Value() V { 115 return iterator.node.Value 116 } 117 118 // Key returns the current element's key. 119 // Does not modify the state of the iterator. 120 func (iterator *Iterator[K, V]) Key() K { 121 return iterator.node.Key 122 } 123 124 // Node returns the current element's node. 125 // Does not modify the state of the iterator. 126 func (iterator *Iterator[K, V]) Node() *Node[K, V] { 127 return iterator.node 128 } 129 130 // Begin resets the iterator to its initial state (one-before-first) 131 // Call Next() to fetch the first element if any. 132 func (iterator *Iterator[K, V]) Begin() { 133 iterator.node = nil 134 iterator.position = begin 135 } 136 137 // End moves the iterator past the last element (one-past-the-end). 138 // Call Prev() to fetch the last element if any. 139 func (iterator *Iterator[K, V]) End() { 140 iterator.node = nil 141 iterator.position = end 142 } 143 144 // First moves the iterator to the first element and returns true if there was a first element in the container. 145 // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). 146 // Modifies the state of the iterator 147 func (iterator *Iterator[K, V]) First() bool { 148 iterator.Begin() 149 return iterator.Next() 150 } 151 152 // Last moves the iterator to the last element and returns true if there was a last element in the container. 153 // If Last() returns true, then last element's key and value can be retrieved by Key() and Value(). 154 // Modifies the state of the iterator. 155 func (iterator *Iterator[K, V]) Last() bool { 156 iterator.End() 157 return iterator.Prev() 158 } 159 160 // NextTo moves the iterator to the next element from current position that satisfies the condition given by the 161 // passed function, and returns true if there was a next element in the container. 162 // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value(). 163 // Modifies the state of the iterator. 164 func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool { 165 for iterator.Next() { 166 key, value := iterator.Key(), iterator.Value() 167 if f(key, value) { 168 return true 169 } 170 } 171 return false 172 } 173 174 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the 175 // passed function, and returns true if there was a next element in the container. 176 // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value(). 177 // Modifies the state of the iterator. 178 func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool { 179 for iterator.Prev() { 180 key, value := iterator.Key(), iterator.Value() 181 if f(key, value) { 182 return true 183 } 184 } 185 return false 186 }