-
Notifications
You must be signed in to change notification settings - Fork 835
/
atree.go
500 lines (412 loc) · 11.5 KB
/
atree.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
Copyright 2014 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package augmentedtree
import "math"
func intervalOverlaps(n *node, low, high int64, interval Interval, maxDimension uint64) bool {
if !overlaps(n.high, high, n.low, low) {
return false
}
if interval == nil {
return true
}
for i := uint64(2); i <= maxDimension; i++ {
if !n.interval.OverlapsAtDimension(interval, i) {
return false
}
}
return true
}
func overlaps(high, otherHigh, low, otherLow int64) bool {
return high > otherLow && low < otherHigh
}
// compare returns an int indicating which direction the node
// should go.
func compare(nodeLow, ivLow int64, nodeID, ivID uint64) int {
if ivLow > nodeLow {
return 1
}
if ivLow < nodeLow {
return 0
}
return intFromBool(ivID > nodeID)
}
type node struct {
interval Interval
low, high, max, min int64 // max value held by children
children [2]*node // array to hold left/right
red bool // indicates if this node is red
id uint64 // we store the id locally to reduce the number of calls to the method on the interface
}
func (n *node) query(low, high int64, interval Interval, maxDimension uint64, fn func(node *node)) {
if n.children[0] != nil && overlaps(n.children[0].max, high, n.children[0].min, low) {
n.children[0].query(low, high, interval, maxDimension, fn)
}
if intervalOverlaps(n, low, high, interval, maxDimension) {
fn(n)
}
if n.children[1] != nil && overlaps(n.children[1].max, high, n.children[1].min, low) {
n.children[1].query(low, high, interval, maxDimension, fn)
}
}
func (n *node) adjustRanges() {
for i := 0; i <= 1; i++ {
if n.children[i] != nil {
n.children[i].adjustRanges()
}
}
n.adjustRange()
}
func (n *node) adjustRange() {
setMin(n)
setMax(n)
}
func newDummy() node {
return node{
children: [2]*node{},
}
}
func newNode(interval Interval, min, max int64, dimension uint64) *node {
itn := &node{
interval: interval,
min: min,
max: max,
red: true,
children: [2]*node{},
}
if interval != nil {
itn.id = interval.ID()
itn.low = interval.LowAtDimension(dimension)
itn.high = interval.HighAtDimension(dimension)
}
return itn
}
type tree struct {
root *node
maxDimension, number uint64
dummy node
}
func (tree *tree) resetDummy() {
tree.dummy.children[0], tree.dummy.children[1] = nil, nil
tree.dummy.red = false
}
// Len returns the number of items in this tree.
func (tree *tree) Len() uint64 {
return tree.number
}
// add will add the provided interval to the tree.
func (tree *tree) add(iv Interval) {
if tree.root == nil {
tree.root = newNode(
iv, iv.LowAtDimension(1),
iv.HighAtDimension(1),
1,
)
tree.root.red = false
tree.number++
return
}
tree.resetDummy()
var (
dummy = tree.dummy
parent, grandParent *node
node = tree.root
dir, last int
otherLast = 1
id = iv.ID()
max = iv.HighAtDimension(1)
ivLow = iv.LowAtDimension(1)
helper = &dummy
)
// set this AFTER clearing dummy
helper.children[1] = tree.root
for {
if node == nil {
node = newNode(iv, ivLow, max, 1)
parent.children[dir] = node
tree.number++
} else if isRed(node.children[0]) && isRed(node.children[1]) {
node.red = true
node.children[0].red = false
node.children[1].red = false
}
if max > node.max {
node.max = max
}
if ivLow < node.min {
node.min = ivLow
}
if isRed(parent) && isRed(node) {
localDir := intFromBool(helper.children[1] == grandParent)
if node == parent.children[last] {
helper.children[localDir] = rotate(grandParent, otherLast)
} else {
helper.children[localDir] = doubleRotate(grandParent, otherLast)
}
}
if node.id == id {
break
}
last = dir
otherLast = takeOpposite(last)
dir = compare(node.low, ivLow, node.id, id)
if grandParent != nil {
helper = grandParent
}
grandParent, parent, node = parent, node, node.children[dir]
}
tree.root = dummy.children[1]
tree.root.red = false
}
// Add will add the provided intervals to this tree.
func (tree *tree) Add(intervals ...Interval) {
for _, iv := range intervals {
tree.add(iv)
}
}
// delete will remove the provided interval from the tree.
func (tree *tree) delete(iv Interval) {
if tree.root == nil {
return
}
tree.resetDummy()
var (
dummy = tree.dummy
found, parent, grandParent *node
last, otherDir, otherLast int // keeping track of last direction
id = iv.ID()
dir = 1
node = &dummy
ivLow = iv.LowAtDimension(1)
)
node.children[1] = tree.root
for node.children[dir] != nil {
last = dir
otherLast = takeOpposite(last)
grandParent, parent, node = parent, node, node.children[dir]
dir = compare(node.low, ivLow, node.id, id)
otherDir = takeOpposite(dir)
if node.id == id {
found = node
}
if !isRed(node) && !isRed(node.children[dir]) {
if isRed(node.children[otherDir]) {
parent.children[last] = rotate(node, dir)
parent = parent.children[last]
} else if !isRed(node.children[otherDir]) {
t := parent.children[otherLast]
if t != nil {
if !isRed(t.children[otherLast]) && !isRed(t.children[last]) {
parent.red = false
node.red = true
t.red = true
} else {
localDir := intFromBool(grandParent.children[1] == parent)
if isRed(t.children[last]) {
grandParent.children[localDir] = doubleRotate(
parent, last,
)
} else if isRed(t.children[otherLast]) {
grandParent.children[localDir] = rotate(
parent, last,
)
}
node.red = true
grandParent.children[localDir].red = true
grandParent.children[localDir].children[0].red = false
grandParent.children[localDir].children[1].red = false
}
}
}
}
}
if found != nil {
tree.number--
found.interval, found.max, found.min, found.low, found.high, found.id = node.interval, node.max, node.min, node.low, node.high, node.id
parentDir := intFromBool(parent.children[1] == node)
childDir := intFromBool(node.children[0] == nil)
parent.children[parentDir] = node.children[childDir]
}
tree.root = dummy.children[1]
if tree.root != nil {
tree.root.red = false
}
}
// insertInterval returns an int indicating if action needs to be taken
// with the given interval. A 1 returned indicates this interval
// needs to be modified. A -1 indicates that this interval needs to
// be deleted. A 0 indicates this interval requires no action.
func insertInterval(dimension uint64, interval Interval, index, count int64) int {
low, high := interval.LowAtDimension(dimension), interval.HighAtDimension(dimension)
if index >= high {
return 0
}
if count > 0 {
return 1
}
if index <= low && count*-1 >= high-index {
return -1
}
return 1
}
// Insert will shift intervals in the tree based on the specified
// index and the specified count. Dimension specifies where to
// apply the shift. Returned is a list of intervals impacted and
// list of intervals deleted. Intervals are deleted if the shift
// makes the interval size zero or less, ie, min >= max. These
// intervals are automatically removed from the tree. The tree
// does not alter the ranges on the intervals themselves, the consumer
// is expected to do that.
func (tree *tree) Insert(dimension uint64,
index, count int64) (Intervals, Intervals) {
if tree.root == nil { // nothing to do
return nil, nil
}
modified, deleted := intervalsPool.Get().(Intervals), intervalsPool.Get().(Intervals)
tree.root.query(math.MinInt64, math.MaxInt64, nil, tree.maxDimension, func(n *node) {
if dimension > 1 {
action := insertInterval(dimension, n.interval, index, count)
switch action {
case 1:
modified = append(modified, n.interval)
case -1:
deleted = append(deleted, n.interval)
}
return
}
if n.max <= index { // won't change min or max in this case
return
}
n.max += count
if n.min >= index {
n.min += count
}
mod := false
if n.high > index {
n.high += count
if n.high < index {
n.high = index
}
mod = true
}
if n.low > index {
n.low += count
if n.low < index {
n.low = index
}
mod = true
}
if n.low >= n.high {
deleted = append(deleted, n.interval)
} else if mod {
modified = append(modified, n.interval)
}
})
tree.Delete(deleted...)
return modified, deleted
}
// Delete will remove the provided intervals from this tree.
func (tree *tree) Delete(intervals ...Interval) {
for _, iv := range intervals {
tree.delete(iv)
}
if tree.root != nil {
tree.root.adjustRanges()
}
}
// Query will return a list of intervals that intersect the provided
// interval. The provided interval's ID method is ignored so the
// provided ID is irrelevant.
func (tree *tree) Query(interval Interval) Intervals {
if tree.root == nil {
return nil
}
var (
Intervals = intervalsPool.Get().(Intervals)
ivLow = interval.LowAtDimension(1)
ivHigh = interval.HighAtDimension(1)
)
tree.root.query(ivLow, ivHigh, interval, tree.maxDimension, func(node *node) {
Intervals = append(Intervals, node.interval)
})
return Intervals
}
func (tree *tree) apply(interval Interval, fn func(*node)) {
if tree.root == nil {
return
}
low, high := interval.LowAtDimension(1), interval.HighAtDimension(1)
tree.root.query(low, high, interval, tree.maxDimension, fn)
}
func isRed(node *node) bool {
return node != nil && node.red
}
func setMax(parent *node) {
parent.max = parent.high
if parent.children[0] != nil && parent.children[0].max > parent.max {
parent.max = parent.children[0].max
}
if parent.children[1] != nil && parent.children[1].max > parent.max {
parent.max = parent.children[1].max
}
}
func setMin(parent *node) {
parent.min = parent.low
if parent.children[0] != nil && parent.children[0].min < parent.min {
parent.min = parent.children[0].min
}
if parent.children[1] != nil && parent.children[1].min < parent.min {
parent.min = parent.children[1].min
}
if parent.low < parent.min {
parent.min = parent.low
}
}
func rotate(parent *node, dir int) *node {
otherDir := takeOpposite(dir)
child := parent.children[otherDir]
parent.children[otherDir] = child.children[dir]
child.children[dir] = parent
parent.red = true
child.red = false
child.max = parent.max
setMax(child)
setMax(parent)
setMin(child)
setMin(parent)
return child
}
func doubleRotate(parent *node, dir int) *node {
otherDir := takeOpposite(dir)
parent.children[otherDir] = rotate(parent.children[otherDir], otherDir)
return rotate(parent, dir)
}
func intFromBool(value bool) int {
if value {
return 1
}
return 0
}
func takeOpposite(value int) int {
return 1 - value
}
func newTree(maxDimension uint64) *tree {
return &tree{
maxDimension: maxDimension,
dummy: newDummy(),
}
}
// New constructs and returns a new interval tree with the max
// dimensions provided.
func New(dimensions uint64) Tree {
return newTree(dimensions)
}