-
Notifications
You must be signed in to change notification settings - Fork 106
/
segment_tree.go
619 lines (562 loc) · 13.2 KB
/
segment_tree.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package nsqd
import (
"fmt"
"github.com/Workiva/go-datastructures/augmentedtree"
"github.com/absolute8511/goskiplist/skiplist"
)
type QueueInterval interface {
Start() int64
End() int64
EndCnt() uint64
augmentedtree.Interval
}
type MsgQueueInterval struct {
Start int64
End int64
EndCnt uint64
}
type queueInterval struct {
start int64
end int64
endCnt uint64
}
func (self *queueInterval) Start() int64 {
return self.start
}
func (self *queueInterval) End() int64 {
return self.end
}
func (self *queueInterval) EndCnt() uint64 {
return self.endCnt
}
// the augmentedtree use the low and the id to determin if the interval is the duplicate
// so here we use the end as the id of segment
func (self *queueInterval) ID() uint64 {
return uint64(self.end)
}
func (self *queueInterval) LowAtDimension(dim uint64) int64 {
return self.start
}
func (self *queueInterval) HighAtDimension(dim uint64) int64 {
return self.end
}
func (self *queueInterval) OverlapsAtDimension(inter augmentedtree.Interval, dim uint64) bool {
if inter.HighAtDimension(dim) < self.start {
return false
}
if inter.LowAtDimension(dim) > self.end {
return false
}
return true
}
type IntervalTree struct {
tr augmentedtree.Tree
}
func NewIntervalTree() *IntervalTree {
return &IntervalTree{
tr: augmentedtree.New(1),
}
}
// return the merged interval, if no overlap just return the original
func (self *IntervalTree) AddOrMerge(inter QueueInterval) QueueInterval {
overlaps := self.tr.Query(inter)
if len(overlaps) == 1 && overlaps[0].LowAtDimension(1) <= inter.LowAtDimension(1) &&
overlaps[0].HighAtDimension(1) >= inter.HighAtDimension(1) {
return overlaps[0].(QueueInterval)
} else if len(overlaps) == 0 {
self.tr.Add(inter)
return inter
} else {
qi := &queueInterval{}
qi.start = inter.Start()
qi.end = inter.End()
qi.endCnt = inter.EndCnt()
for _, v := range overlaps {
if v.LowAtDimension(0) < qi.LowAtDimension(0) {
qi.start = v.LowAtDimension(0)
}
if v.HighAtDimension(0) > qi.HighAtDimension(0) {
qi.end = v.HighAtDimension(0)
qi.endCnt = v.(QueueInterval).EndCnt()
}
self.tr.Delete(v)
}
self.tr.Add(qi)
return qi
}
}
func (self *IntervalTree) Len() int {
return int(self.tr.Len())
}
func (self *IntervalTree) ToIntervalList() []MsgQueueInterval {
il := make([]MsgQueueInterval, 0, self.Len())
self.tr.Traverse(func(inter augmentedtree.Interval) {
qi, ok := inter.(QueueInterval)
if ok {
var mqi MsgQueueInterval
mqi.Start = qi.Start()
mqi.End = qi.End()
mqi.EndCnt = qi.EndCnt()
il = append(il, mqi)
}
})
return il
}
func (self *IntervalTree) ToString() string {
dataStr := ""
self.tr.Traverse(func(inter augmentedtree.Interval) {
dataStr += fmt.Sprintf("interval %v, ", inter)
})
return dataStr
}
func (self *IntervalTree) DeleteRange(inter QueueInterval) {
overlaps := self.tr.Query(inter)
for _, v := range overlaps {
if v.LowAtDimension(1) >= inter.LowAtDimension(1) &&
v.HighAtDimension(1) <= inter.HighAtDimension(1) {
self.tr.Delete(v)
}
}
}
func (self *IntervalTree) DeleteInterval(inter QueueInterval) {
overlaps := self.tr.Query(inter)
for _, v := range overlaps {
if v.LowAtDimension(1) == inter.LowAtDimension(1) &&
v.HighAtDimension(1) == inter.HighAtDimension(1) {
self.tr.Delete(v)
}
}
}
func (self *IntervalTree) DeleteLower(low int64) int {
qi := &queueInterval{
start: 0,
end: low,
endCnt: 0,
}
overlaps := self.tr.Query(qi)
cnt := 0
for _, v := range overlaps {
if v.HighAtDimension(1) <= low {
self.tr.Delete(v)
cnt++
}
}
return cnt
}
func (self *IntervalTree) IsCompleteOverlap(inter QueueInterval) bool {
overlaps := self.tr.Query(inter)
for _, v := range overlaps {
if v.LowAtDimension(1) >= inter.HighAtDimension(1) {
continue
}
if v.HighAtDimension(1) <= inter.LowAtDimension(1) {
continue
}
return true
}
return false
}
func (self *IntervalTree) Query(inter QueueInterval, excludeBoard bool) []QueueInterval {
overlaps := self.tr.Query(inter)
rets := make([]QueueInterval, 0, len(overlaps))
for _, v := range overlaps {
if excludeBoard {
if v.LowAtDimension(1) >= inter.HighAtDimension(1) {
continue
}
if v.HighAtDimension(1) <= inter.LowAtDimension(1) {
continue
}
}
rets = append(rets, v.(QueueInterval))
}
return rets
}
func (self *IntervalTree) IsLowestAt(low int64) QueueInterval {
qi := &queueInterval{
start: 0,
end: low,
endCnt: 0,
}
overlaps := self.tr.Query(qi)
if len(overlaps) == 0 {
return nil
} else if len(overlaps) == 1 {
if overlaps[0].LowAtDimension(1) == low {
return overlaps[0].(QueueInterval)
}
return nil
}
return nil
}
type IntervalSkipList struct {
sl *skiplist.SkipList
}
func NewIntervalSkipList() *IntervalSkipList {
sl := skiplist.NewIntMap()
return &IntervalSkipList{
sl: sl,
}
}
// return the merged interval, if no overlap just return the original
func (self *IntervalSkipList) AddOrMerge(inter QueueInterval) QueueInterval {
minStart := inter.Start()
maxEnd := inter.End()
maxEndCnt := inter.EndCnt()
removings := self.Query(inter, false)
if len(removings) == 0 {
self.sl.Set(inter.Start(), inter)
return inter
}
if removings[0].Start() < minStart {
minStart = removings[0].Start()
}
if removings[len(removings)-1].End() > maxEnd {
maxEnd = removings[len(removings)-1].End()
maxEndCnt = removings[len(removings)-1].EndCnt()
}
for _, v := range removings {
self.sl.Delete(v.Start())
}
n := &queueInterval{
start: minStart,
end: maxEnd,
endCnt: maxEndCnt,
}
self.sl.Set(minStart, n)
return n
}
func (self *IntervalSkipList) Len() int {
return int(self.sl.Len())
}
func (self *IntervalSkipList) ToIntervalList() []MsgQueueInterval {
il := make([]MsgQueueInterval, 0, self.Len())
it := self.sl.Iterator()
for it.Next() {
qi, ok := it.Value().(QueueInterval)
if ok {
var mqi MsgQueueInterval
mqi.Start = qi.Start()
mqi.End = qi.End()
mqi.EndCnt = qi.EndCnt()
il = append(il, mqi)
}
}
it.Close()
return il
}
func (self *IntervalSkipList) ToString() string {
dataStr := ""
it := self.sl.Iterator()
cnt := 0
for it.Next() {
dataStr += fmt.Sprintf("interval %v, ", it.Value())
cnt++
if cnt > 100 {
break
}
}
it.Close()
return dataStr
}
func (self *IntervalSkipList) DeleteRange(inter QueueInterval) {
overlaps := self.Query(inter, false)
for _, v := range overlaps {
if v.Start() >= inter.Start() &&
v.End() <= inter.End() {
self.sl.Delete(v.Start())
}
}
}
func (self *IntervalSkipList) DeleteInterval(inter QueueInterval) {
self.sl.Delete(inter.Start())
}
func (self *IntervalSkipList) DeleteLower(low int64) int {
qi := &queueInterval{
start: 0,
end: low,
endCnt: 0,
}
overlaps := self.Query(qi, false)
cnt := 0
for _, v := range overlaps {
if v.End() <= low {
self.sl.Delete(v.Start())
cnt++
}
}
return cnt
}
func (self *IntervalSkipList) IsCompleteOverlap(inter QueueInterval) bool {
overlaps := self.Query(inter, true)
return len(overlaps) > 0
}
func (self *IntervalSkipList) Query(inter QueueInterval, excludeBoard bool) []QueueInterval {
rets := make([]QueueInterval, 0)
queryEnd := inter.End()
queryStart := inter.Start()
if !excludeBoard {
queryEnd++
} else {
queryStart++
}
overlaps := self.sl.Seek(queryStart)
if overlaps == nil {
overlaps = self.sl.SeekToLast()
if overlaps != nil {
prevEnd := overlaps.Value().(QueueInterval).End()
if prevEnd < inter.Start() {
} else if excludeBoard && prevEnd == inter.Start() {
} else {
rets = append(rets, overlaps.Value().(QueueInterval))
}
}
return rets
}
defer overlaps.Close()
hasPrev := overlaps.Previous()
if hasPrev {
prevEnd := overlaps.Value().(QueueInterval).End()
if prevEnd < inter.Start() {
} else if excludeBoard && prevEnd == inter.Start() {
} else {
rets = append(rets, overlaps.Value().(QueueInterval))
}
} else {
if overlaps.Key().(int64) < queryEnd {
rets = append(rets, overlaps.Value().(QueueInterval))
}
}
for overlaps.Next() {
if overlaps.Key().(int64) >= queryEnd {
break
}
rets = append(rets, overlaps.Value().(QueueInterval))
}
return rets
}
func (self *IntervalSkipList) IsLowestAt(low int64) QueueInterval {
qi := &queueInterval{
start: 0,
end: low,
endCnt: 0,
}
overlaps := self.Query(qi, false)
if len(overlaps) == 0 {
return nil
} else if len(overlaps) == 1 {
if overlaps[0].Start() == low {
return overlaps[0].(QueueInterval)
}
return nil
}
return nil
}
type IntervalHash struct {
elems map[int64]QueueInterval
historyMsg map[int64]int64
}
func NewIntervalHash() *IntervalHash {
return &IntervalHash{
elems: make(map[int64]QueueInterval),
historyMsg: make(map[int64]int64),
}
}
// return the merged interval, if no overlap just return the original
func (self *IntervalHash) AddOrMerge(inter QueueInterval) QueueInterval {
minStart := inter.Start()
maxEnd := inter.End()
maxEndCnt := inter.EndCnt()
self.historyMsg[inter.Start()] = inter.End()
qi, ok := self.elems[minStart]
if ok {
if qi.Start() < minStart {
minStart = qi.Start()
}
if qi.End() > maxEnd {
maxEnd = qi.End()
maxEndCnt = qi.EndCnt()
}
delete(self.elems, qi.Start())
delete(self.elems, qi.End())
}
qi, ok = self.elems[maxEnd]
if ok {
if qi.End() > maxEnd {
maxEnd = qi.End()
maxEndCnt = qi.EndCnt()
}
if qi.Start() < minStart {
minStart = qi.Start()
}
delete(self.elems, qi.Start())
delete(self.elems, qi.End())
}
n := &queueInterval{
start: minStart,
end: maxEnd,
endCnt: maxEndCnt,
}
self.elems[minStart] = n
self.elems[maxEnd] = n
return n
}
func (self *IntervalHash) Len() int {
return int(len(self.elems) / 2)
}
func (self *IntervalHash) ToIntervalList() []MsgQueueInterval {
il := make([]MsgQueueInterval, 0, self.Len())
sorted := skiplist.NewIntMap()
for _, qi := range self.elems {
sorted.Set(qi.Start(), qi)
}
it := sorted.Iterator()
for it.Next() {
qi, ok := it.Value().(QueueInterval)
if ok {
var mqi MsgQueueInterval
mqi.Start = qi.Start()
mqi.End = qi.End()
mqi.EndCnt = qi.EndCnt()
il = append(il, mqi)
}
}
it.Close()
return il
}
func (self *IntervalHash) ToString() string {
dataStr := ""
dataStr += fmt.Sprintf("intervals %v, ", len(self.elems))
dataStr += fmt.Sprintf("history intervals %v, ", len(self.historyMsg))
if len(self.elems) < 100 {
sorted := skiplist.NewIntMap()
for _, qi := range self.elems {
sorted.Set(qi.Start(), qi)
}
it := sorted.Iterator()
for it.Next() {
dataStr += fmt.Sprintf("interval %v, ", it.Value())
}
it.Close()
}
if len(self.historyMsg) < 100 {
for s, e := range self.historyMsg {
dataStr += fmt.Sprintf("interval %v-%v, ", s, e)
}
}
return dataStr
}
func (self *IntervalHash) DeleteRange(inter QueueInterval) {
for _, v := range self.elems {
if v.Start() >= inter.Start() &&
v.End() <= inter.End() {
delete(self.elems, v.Start())
delete(self.elems, v.End())
}
}
}
func (self *IntervalHash) DeleteInterval(inter QueueInterval) {
qi, ok := self.elems[inter.Start()]
if ok && qi.Start() == inter.Start() && qi.End() == inter.End() {
delete(self.elems, qi.Start())
delete(self.elems, qi.End())
}
}
func (self *IntervalHash) DeleteLower(low int64) int {
cnt := 0
for _, v := range self.elems {
if v.End() <= low {
delete(self.elems, v.Start())
delete(self.elems, v.End())
cnt++
}
}
for s, v := range self.historyMsg {
if v <= low && s < low {
delete(self.historyMsg, s)
}
}
return cnt
}
func (self *IntervalHash) IsCompleteOverlap(inter QueueInterval) bool {
qi, ok := self.elems[inter.Start()]
if ok {
if qi.End() >= inter.End() {
return true
}
}
qi, ok = self.elems[inter.End()]
if ok {
if qi.Start() <= inter.Start() {
return true
}
}
historyM, ok := self.historyMsg[inter.Start()]
if ok {
if historyM >= inter.End() {
return true
}
}
return false
//for _, v := range self.elems {
// if (inter.Start() >= v.Start()) && (inter.Start() <= v.End()) &&
// (inter.End() >= v.Start()) && (inter.End() <= v.End()) {
// return true
// }
//}
//return false
}
func (self *IntervalHash) QueryExist(inter QueueInterval, excludeBoard bool) []QueueInterval {
return self.query(inter, excludeBoard, true)
}
func (self *IntervalHash) Query(inter QueueInterval, excludeBoard bool) []QueueInterval {
return self.query(inter, excludeBoard, false)
}
func (self *IntervalHash) query(inter QueueInterval, excludeBoard bool, earlyExit bool) []QueueInterval {
rets := make([]QueueInterval, 0, 4)
sorted := skiplist.NewIntMap()
for _, v := range self.elems {
if v.End() < inter.Start() {
continue
}
if v.End() == inter.Start() && excludeBoard {
continue
}
if v.Start() > inter.End() {
continue
}
if v.Start() == inter.End() && excludeBoard {
continue
}
if earlyExit {
rets = append(rets, v)
return rets
}
sorted.Set(v.Start(), v)
}
overlaps := sorted.Iterator()
for overlaps.Next() {
rets = append(rets, overlaps.Value().(QueueInterval))
}
overlaps.Close()
return rets
}
func (self *IntervalHash) IsLowestAt(low int64) QueueInterval {
qi, ok := self.elems[low]
if ok && qi.Start() == low {
// check if any lower segment
foundLower := false
for _, v := range self.elems {
if v.Start() < low {
foundLower = true
break
}
}
if foundLower {
return nil
} else {
return qi
}
}
return nil
}