-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval_tree.go
132 lines (110 loc) · 2.44 KB
/
interval_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
package main
import (
// "fmt"
"github.com/petar/GoLLRB/llrb"
"math"
)
type IntervalItem struct {
*Interval
maxEnd float64
}
func max(a ...float64) (m float64) {
m = math.Inf(-1)
for _, x := range a {
m = math.Max(m, x)
}
return
}
func (i *IntervalItem) Notify(n *llrb.Node) {
// fmt.Println("notified of a change!")
leftMax := math.Inf(-1)
rightMax := math.Inf(-1)
if n.Left != nil {
leftMax = n.Left.Item.(IntervalItem).maxEnd
}
if n.Right != nil {
rightMax = n.Right.Item.(IntervalItem).maxEnd
}
i.maxEnd = math.Max(leftMax, rightMax)
}
func lessFunc(x, y interface{}) bool {
return x.(IntervalItem).Start < y.(IntervalItem).Start
}
type IntervalTreeInterface interface {
Insert(i Interval)
Remove(i Interval)
Cover(x float64) []Interval
Len() int
}
type Tree struct {
*llrb.Tree
}
func NewTree() *Tree {
return &Tree{llrb.New(lessFunc)}
}
func (t *Tree) Insert(i Interval) {
item := IntervalItem{&i, i.End}
t.InsertNoReplace(item)
DoOnNodes(t.Root(), updateMaxEnd)
}
func updateMaxEnd(n *llrb.Node) {
item := n.Item.(IntervalItem)
item.Notify(n)
}
func DoOnNodes(n *llrb.Node, f func(n *llrb.Node)) {
f(n)
if n.Left != nil {
DoOnNodes(n.Left, f)
}
if n.Right != nil {
DoOnNodes(n.Right, f)
}
}
func (t *Tree) Remove(i Interval) {
t.Delete(i)
}
func (t *Tree) Cover(x float64) (out []Interval) {
c := make(chan Interval, 100)
out = make([]Interval, 0)
go func() {
t.search(t.Root(), x, c)
close(c)
}()
for x := range c {
out = append(out, x)
}
// fmt.Println("final out", out)
return out
}
func (t *Tree) search(n *llrb.Node, x float64, out chan Interval) {
// fmt.Println("search(", n.Item, x, out, ")")
interval := n.Item.(IntervalItem)
// fmt.Println("interval:", interval, "maxEnd:", interval.maxEnd)
// If x is past the end, then there won't be any matches.
if x > interval.maxEnd {
// fmt.Println("past the end")
return
}
// Search left children
if n.Left != nil {
// fmt.Println("searching left")
t.search(n.Left, x, out)
}
// Check this interval
if interval.Contains(x) {
// fmt.Println("got one!", interval.Interval)
out <- *interval.Interval
// fmt.Println("out:", out)
}
// If x is to the left of the start, then it can't be in any child on the
// right.
if x < interval.Start {
// fmt.Println("we're to the left of the start")
return
}
// Otherwise search the right children.
if n.Right != nil {
// fmt.Println("searching right")
t.search(n.Right, x, out)
}
}