-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
195 lines (176 loc) · 6.06 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package btree
import (
"github.com/mymmsc/gox/util"
)
func assertIteratorImplementation() {
var _ util.ReverseIteratorWithKey = (*Iterator)(nil)
}
// Iterator holding the iterator's state
type Iterator struct {
tree *Tree
node *Node
entry *Entry
position position
}
type position byte
const (
begin, between, end position = 0, 1, 2
)
// Iterator returns a stateful iterator whose elements are key/value pairs.
func (tree *Tree) Iterator() Iterator {
return Iterator{tree: tree, node: nil, position: begin}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
// If already at end, go to end
if iterator.position == end {
goto end
}
// If at beginning, get the left-most entry in the tree
if iterator.position == begin {
left := iterator.tree.Left()
if left == nil {
goto end
}
iterator.node = left
iterator.entry = left.Entries[0]
goto between
}
{
// Find current entry position in current node
e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
// Try to go down to the child right of the current entry
if e+1 < len(iterator.node.Children) {
iterator.node = iterator.node.Children[e+1]
// Try to go down to the child left of the current node
for len(iterator.node.Children) > 0 {
iterator.node = iterator.node.Children[0]
}
// Return the left-most entry
iterator.entry = iterator.node.Entries[0]
goto between
}
// Above assures that we have reached a leaf node, so return the next entry in current node (if any)
if e+1 < len(iterator.node.Entries) {
iterator.entry = iterator.node.Entries[e+1]
goto between
}
}
// Reached leaf node and there are no entries to the right of the current entry, so go up to the parent
for iterator.node.Parent != nil {
iterator.node = iterator.node.Parent
// Find next entry position in current node (note: search returns the first equal or bigger than entry)
e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
// Check that there is a next entry position in current node
if e < len(iterator.node.Entries) {
iterator.entry = iterator.node.Entries[e]
goto between
}
}
end:
iterator.End()
return false
between:
iterator.position = between
return true
}
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) Prev() bool {
// If already at beginning, go to begin
if iterator.position == begin {
goto begin
}
// If at end, get the right-most entry in the tree
if iterator.position == end {
right := iterator.tree.Right()
if right == nil {
goto begin
}
iterator.node = right
iterator.entry = right.Entries[len(right.Entries)-1]
goto between
}
{
// Find current entry position in current node
e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
// Try to go down to the child left of the current entry
if e < len(iterator.node.Children) {
iterator.node = iterator.node.Children[e]
// Try to go down to the child right of the current node
for len(iterator.node.Children) > 0 {
iterator.node = iterator.node.Children[len(iterator.node.Children)-1]
}
// Return the right-most entry
iterator.entry = iterator.node.Entries[len(iterator.node.Entries)-1]
goto between
}
// Above assures that we have reached a leaf node, so return the previous entry in current node (if any)
if e-1 >= 0 {
iterator.entry = iterator.node.Entries[e-1]
goto between
}
}
// Reached leaf node and there are no entries to the left of the current entry, so go up to the parent
for iterator.node.Parent != nil {
iterator.node = iterator.node.Parent
// Find previous entry position in current node (note: search returns the first equal or bigger than entry)
e, _ := iterator.tree.search(iterator.node, iterator.entry.Key)
// Check that there is a previous entry position in current node
if e-1 >= 0 {
iterator.entry = iterator.node.Entries[e-1]
goto between
}
}
begin:
iterator.Begin()
return false
between:
iterator.position = between
return true
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *Iterator) Value() interface{} {
return iterator.entry.Value
}
// Key returns the current element's key.
// Does not modify the state of the iterator.
func (iterator *Iterator) Key() interface{} {
return iterator.entry.Key
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Begin() {
iterator.node = nil
iterator.position = begin
iterator.entry = nil
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *Iterator) End() {
iterator.node = nil
iterator.position = end
iterator.entry = nil
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator
func (iterator *Iterator) First() bool {
iterator.Begin()
return iterator.Next()
}
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) Last() bool {
iterator.End()
return iterator.Prev()
}