-
Notifications
You must be signed in to change notification settings - Fork 1
/
flattree.go
293 lines (255 loc) · 6.48 KB
/
flattree.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package flattree
import "errors"
// Range type is an inclusive tree range that is in the form of [start, finish]
type Range []uint64
// FullRoots returns a list of all the full roots (subtrees where all nodes have either 2 or 0 children) `<` index.
// For example `fullRoots(8)` returns `[3]` since the subtree rooted at `3` spans `0 -> 6` and the tree
// rooted at `7` has a child located at `9` which is `>= 8`.
func FullRoots(index uint64, result []uint64) ([]uint64, error) {
if (index & 1) == 1 {
return nil, errors.New("You can only look up roots for depth(0) blocks")
}
index /= 2
var offset uint64
var factor uint64 = 1
for true {
if index == 0 {
return result, nil
}
for (factor * 2) <= index {
factor *= 2
}
result = append(result, offset+factor-1)
offset = offset + 2*factor
index -= factor
factor = 1
}
return nil, nil
}
// Depth returns the depth of an element.
func Depth(index uint64) uint64 {
var depth uint64
index++
for (index & 1) == 0 {
depth++
index = rightShift(index)
}
return depth
}
// Sibling returns the index of this elements sibling.
func Sibling(index, depth uint64) uint64 {
if depth == 0 {
depth = Depth(index)
}
offset := Offset(index, depth)
if offset&1 == 1 {
return Index(depth, offset-1)
}
return Index(depth, offset+1)
}
// Parent returns the index of the parent element in tree.
func Parent(index, depth uint64) uint64 {
if depth == 0 {
depth = Depth(index)
}
offset := Offset(index, depth)
return Index(depth+1, rightShift(offset))
}
// LeftChild returns the left most child at index and depth.
func LeftChild(index, depth uint64) (uint64, error) {
if (index & 1) == 0 {
return 0, errors.New("No more left children")
}
if depth == 0 {
depth = Depth(index)
}
return Index(depth-1, Offset(index, depth)*2), nil
}
// RightChild returns the right most child at index and depth.
func RightChild(index, depth uint64) (uint64, error) {
if (index & 1) == 0 {
return 0, errors.New("No more right children")
}
if depth == 0 {
depth = Depth(index)
}
return Index(depth-1, 1+Offset(index, depth)*2), nil
}
// Children returns a Range slice `[leftChild, rightChild]` with indexes of this elements children.
// If this element does not have any children it returns an empty slice.
func Children(index, depth uint64) Range {
if (index & 1) == 0 {
return Range{}
}
if depth == 0 {
depth = Depth(index)
}
offset := Offset(index, depth)
return Range{Index(depth-1, offset), Index(depth-1, offset+1)}
}
// LeftSpan returns the left spanning in index in the tree `index` spans.
func LeftSpan(index, depth uint64) uint64 {
if (index & 1) == 0 {
return index
}
if depth == 0 {
depth = Depth(index)
}
return Offset(index, depth) * twoPow(depth+1)
}
// RightSpan returns the right spanning in index in the tree `index` spans.
func RightSpan(index, depth uint64) uint64 {
if (index & 1) == 0 {
return index
}
if depth == 0 {
depth = Depth(index)
}
return (Offset(index, depth)+1)*twoPow(depth+1) - 2
}
// Count returns how many nodes (including the parent nodes) a tree contains.
func Count(index, depth uint64) uint64 {
if (index & 1) == 0 {
return 1
}
if depth == 0 {
depth = Depth(index)
}
return twoPow(depth+1) - 1
}
// Spans returns the Range (inclusive) the tree root at `index` spans.
// For example `Spans(3)` would return `[]Range{0, 6}`
func Spans(index, depth uint64) Range {
if (index & 1) == 0 {
return Range{index, index}
}
if depth == 0 {
depth = Depth(index)
}
offset := Offset(index, depth)
width := twoPow(depth + 1)
return Range{offset * width, (offset+1)*width - 2}
}
// Index returns an array index for the tree element at the given depth and offset.
func Index(depth, offset uint64) uint64 {
return (1+2*offset)*twoPow(depth) - 1
}
// Offset returns the relative offset of an element.
func Offset(index, depth uint64) uint64 {
if (index & 1) == 0 {
return index / 2
}
if depth == 0 {
depth = Depth(index)
}
return ((index+1)/twoPow(depth) - 1) / 2
}
func twoPow(n uint64) uint64 {
if n < 31 {
return 1 << n
}
return ((1 << 30) * (1 << (n - 30)))
}
func rightShift(n uint64) uint64 {
return (n - (n & 1)) / 2
}
// NewIterator returns a stateful tree iterator at a given index.
func NewIterator(index uint64) *Iterator {
var ite Iterator
ite.Seek(index)
return &ite
}
// Iterator is a stateful iterator type. Use NewIterator to create tree Iterators at a given index.
type Iterator struct {
Index, Offset, Factor uint64
}
// Seek moves the iterator to this specific tree index.
func (i *Iterator) Seek(index uint64) {
i.Index = index
if i.Index&1 == 1 {
i.Offset = Offset(index, 0)
i.Factor = twoPow(Depth(index) + 1)
} else {
i.Offset = index / 2
i.Factor = 2
}
}
// IsLeft tests if the iterator at a left sibling.
func (i *Iterator) IsLeft() bool {
if i.Offset&1 == 0 {
return true
}
return false
}
// IsRight tests if the iterator is at a right subling.
func (i *Iterator) IsRight() bool {
return !i.IsLeft()
}
// Prev moves the iterator to the prev item in the tree.
func (i *Iterator) Prev() uint64 {
if i.Offset == 0 {
return i.Index
}
i.Offset--
i.Index -= i.Factor
return i.Index
}
// Next moves the iterator to the next item in the tree.
func (i *Iterator) Next() uint64 {
i.Offset++
i.Index += i.Factor
return i.Index
}
// Sibling moves the iterator to the current sibling.
func (i *Iterator) Sibling() uint64 {
if i.IsLeft() {
return i.Next()
}
return i.Prev()
}
// Parent moves the iterator to the current parent index.
func (i *Iterator) Parent() uint64 {
if (i.Offset & 1) == 1 {
i.Index -= i.Factor / 2
i.Offset = (i.Offset - 1) / 2
} else {
i.Index += i.Factor / 2
i.Offset /= 2
}
i.Factor *= 2
return i.Index
}
// LeftSpan moves the iterator to the current left span index.
func (i *Iterator) LeftSpan() uint64 {
i.Index = i.Index - i.Factor/2 + 1
i.Offset = i.Index / 2
i.Factor = 2
return i.Index
}
// RightSpan moves the iterator to the current right span index.
func (i *Iterator) RightSpan() uint64 {
i.Index = i.Index + i.Factor/2 - 1
i.Offset = i.Index / 2
i.Factor = 2
return i.Index
}
// LeftChild moves the iterator to the current left child index.
func (i *Iterator) LeftChild() uint64 {
if i.Factor == 2 {
return i.Index
}
i.Factor /= 2
i.Index -= i.Factor / 2
i.Offset *= 2
return i.Index
}
// RightChild moves the iterator to the current right child index.
func (i *Iterator) RightChild() uint64 {
if i.Factor == 2 {
return i.Index
}
i.Factor /= 2
i.Index += i.Factor / 2
i.Offset = 2*i.Offset + 1
return i.Index
}