Debian Codegraph @

trees/btree/iterator.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. 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 btree
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 entry *Entry[K, V]
17 position position
18 }
19
20 type position byte
21
22 const (
23 begin, between, end position = 0, 1, 2
24 )
25
26 // Iterator returns a stateful iterator whose elements are key/value pairs.
27 func (tree *Tree[K, V]) Iterator() *Iterator[K, V] {
28 return &Iterator[K, V]{tree: tree, node: nil, position: begin}
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 already at end, go to end
37 if iterator.position == end {
38 goto end
39 }
40 // If at beginning, get the left-most entry in the tree
41 if iterator.position == begin {
42 left := iterator.tree.Left()
43 if left == nil {
44 goto end
45 }
46 iterator.node = left
47 iterator.entry = left.Entries[0]
48 goto between
49 }
50 {
51 // Find current entry position in current node
52 e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
53 // Try to go down to the child right of the current entry
54 if e+1 < len(iterator.node.Children) {
55 iterator.node = iterator.node.Children[e+1]
56 // Try to go down to the child left of the current node
57 for len(iterator.node.Children) > 0 {
58 iterator.node = iterator.node.Children[0]
59 }
60 // Return the left-most entry
61 iterator.entry = iterator.node.Entries[0]
62 goto between
63 }
64 // Above assures that we have reached a leaf node, so return the next entry in current node (if any)
65 if e+1 < len(iterator.node.Entries) {
66 iterator.entry = iterator.node.Entries[e+1]
67 goto between
68 }
69 }
70 // Reached leaf node and there are no entries to the right of the current entry, so go up to the parent
71 for iterator.node.Parent != nil {
72 iterator.node = iterator.node.Parent
73 // Find next entry position in current node (note: search returns the first equal or bigger than entry)
74 e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
75 // Check that there is a next entry position in current node
76 if e < len(iterator.node.Entries) {
77 iterator.entry = iterator.node.Entries[e]
78 goto between
79 }
80 }
81
82 end:
83 iterator.End()
84 return false
85
86 between:
87 iterator.position = between
88 return true
89 }
90
91 // Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
92 // If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
93 // Modifies the state of the iterator.
94 func (iterator *Iterator[K, V]) Prev() bool {
95 // If already at beginning, go to begin
96 if iterator.position == begin {
97 goto begin
98 }
99 // If at end, get the right-most entry in the tree
100 if iterator.position == end {
101 right := iterator.tree.Right()
102 if right == nil {
103 goto begin
104 }
105 iterator.node = right
106 iterator.entry = right.Entries[len(right.Entries)-1]
107 goto between
108 }
109 {
110 // Find current entry position in current node
111 e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
112 // Try to go down to the child left of the current entry
113 if e < len(iterator.node.Children) {
114 iterator.node = iterator.node.Children[e]
115 // Try to go down to the child right of the current node
116 for len(iterator.node.Children) > 0 {
117 iterator.node = iterator.node.Children[len(iterator.node.Children)-1]
118 }
119 // Return the right-most entry
120 iterator.entry = iterator.node.Entries[len(iterator.node.Entries)-1]
121 goto between
122 }
123 // Above assures that we have reached a leaf node, so return the previous entry in current node (if any)
124 if e-1 >= 0 {
125 iterator.entry = iterator.node.Entries[e-1]
126 goto between
127 }
128 }
129 // Reached leaf node and there are no entries to the left of the current entry, so go up to the parent
130 for iterator.node.Parent != nil {
131 iterator.node = iterator.node.Parent
132 // Find previous entry position in current node (note: search returns the first equal or bigger than entry)
133 e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
134 // Check that there is a previous entry position in current node
135 if e-1 >= 0 {
136 iterator.entry = iterator.node.Entries[e-1]
137 goto between
138 }
139 }
140
141 begin:
142 iterator.Begin()
143 return false
144
145 between:
146 iterator.position = between
147 return true
148 }
149
150 // Value returns the current element's value.
151 // Does not modify the state of the iterator.
152 func (iterator *Iterator[K, V]) Value() V {
153 return iterator.entry.Value
154 }
155
156 // Key returns the current element's key.
157 // Does not modify the state of the iterator.
158 func (iterator *Iterator[K, V]) Key() K {
159 return iterator.entry.Key
160 }
161
162 // Node returns the current element's node.
163 // Does not modify the state of the iterator.
164 func (iterator *Iterator[K, V]) Node() *Node[K, V] {
165 return iterator.node
166 }
167
168 // Begin resets the iterator to its initial state (one-before-first)
169 // Call Next() to fetch the first element if any.
170 func (iterator *Iterator[K, V]) Begin() {
171 iterator.node = nil
172 iterator.position = begin
173 iterator.entry = nil
174 }
175
176 // End moves the iterator past the last element (one-past-the-end).
177 // Call Prev() to fetch the last element if any.
178 func (iterator *Iterator[K, V]) End() {
179 iterator.node = nil
180 iterator.position = end
181 iterator.entry = nil
182 }
183
184 // First moves the iterator to the first element and returns true if there was a first element in the container.
185 // If First() returns true, then first element's key and value can be retrieved by Key() and Value().
186 // Modifies the state of the iterator
187 func (iterator *Iterator[K, V]) First() bool {
188 iterator.Begin()
189 return iterator.Next()
190 }
191
192 // Last moves the iterator to the last element and returns true if there was a last element in the container.
193 // If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
194 // Modifies the state of the iterator.
195 func (iterator *Iterator[K, V]) Last() bool {
196 iterator.End()
197 return iterator.Prev()
198 }
199
200 // NextTo moves the iterator to the next element from current position that satisfies the condition given by the
201 // passed function, and returns true if there was a next element in the container.
202 // If NextTo() returns true, then next element's key and value can be retrieved by Key() and Value().
203 // Modifies the state of the iterator.
204 func (iterator *Iterator[K, V]) NextTo(f func(key K, value V) bool) bool {
205 for iterator.Next() {
206 key, value := iterator.Key(), iterator.Value()
207 if f(key, value) {
208 return true
209 }
210 }
211 return false
212 }
213
214 // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
215 // passed function, and returns true if there was a next element in the container.
216 // If PrevTo() returns true, then next element's key and value can be retrieved by Key() and Value().
217 // Modifies the state of the iterator.
218 func (iterator *Iterator[K, V]) PrevTo(f func(key K, value V) bool) bool {
219 for iterator.Prev() {
220 key, value := iterator.Key(), iterator.Value()
221 if f(key, value) {
222 return true
223 }
224 }
225 return false
226 }