Debian Codegraph @

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