forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
223 lines (200 loc) · 4.33 KB
/
list.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package interval
import "sort"
type Node interface {
Span() (start, end uint64)
}
type RList interface {
Len() int
GetInterval(index int) (start, end uint64)
SetInterval(index int, start, end uint64)
}
type List interface {
RList
Copy(to, from, count int)
Cap() int
SetLen(len int)
GrowTo(length, capacity int)
}
type ExtendedList interface {
MergeData(index int, i Node)
}
type intersection struct {
overlap int
lowIndex int
lowStart, lowEnd uint64
intersectsLow bool
highIndex int
highStart, highEnd uint64
intersectsHigh bool
}
const (
minSpace = 3 // Max growth of 2 plus one slot for temporary
minCap = 5
)
func Merge(l List, i Node) {
start, end := i.Span()
s := intersection{}
s.intersect(l, start, end)
adjust(l, s.lowIndex, 1-s.overlap)
if s.intersectsLow {
start = s.lowStart
}
if s.intersectsHigh {
end = s.highEnd
}
l.SetInterval(s.lowIndex, start, end)
if dl, ok := l.(ExtendedList); ok {
dl.MergeData(s.lowIndex, i)
}
}
func Replace(l List, i Node) {
start, end := i.Span()
index, start, end := replace(l, start, end, true)
l.SetInterval(index, start, end)
if dl, ok := l.(ExtendedList); ok {
dl.MergeData(index, i)
}
}
func Remove(l List, i Node) {
start, end := i.Span()
replace(l, start, end, false)
}
func Intersect(l RList, i Node) (first, count int) {
start, end := i.Span()
s := intersection{}
s.intersect(l, start, end)
return s.lowIndex, s.overlap
}
type Visitor func(start, end uint64, index int)
func Visit(l RList, i Node, v Visitor) {
start, end := i.Span()
s := intersection{}
s.intersect(l, start, end)
for index := s.lowIndex; index < s.lowIndex+s.overlap; index++ {
s, e := l.GetInterval(index)
if s < start {
s = start
}
if e > end {
e = end
}
v(s, e, index)
}
}
func Contains(l RList, p uint64) bool {
return IndexOf(l, p) >= 0
}
func IndexOf(l RList, p uint64) int {
index := sort.Search(l.Len(), func(at int) bool {
iStart, _ := l.GetInterval(at)
return p < iStart
})
index--
if index >= 0 {
_, iEnd := l.GetInterval(index)
if p < iEnd {
return index
}
}
return -1
}
func FindStart(l RList, at int, start uint64) bool {
_, iEnd := l.GetInterval(at)
return start < iEnd
}
func FindEnd(l RList, at int, end uint64) bool {
iStart, _ := l.GetInterval(at)
return end <= iStart
}
func Search(l RList, v uint64, f func(l RList, at int, v uint64) bool) int {
i, j := 0, l.Len()
for i < j {
h := i + (j-i)/2
if !f(l, h, v) {
i = h + 1
} else {
j = h
}
}
return i
}
func (s *intersection) intersect(l RList, start, end uint64) {
beforeLen := Search(l, start, FindStart)
afterIndex := Search(l, end, FindEnd)
if afterIndex < beforeLen {
afterIndex, beforeLen = beforeLen, afterIndex
}
s.lowIndex = beforeLen
s.highIndex = afterIndex - 1
s.overlap = afterIndex - beforeLen
s.intersectsLow = false
s.intersectsHigh = false
if s.overlap > 0 {
s.lowStart, s.lowEnd = l.GetInterval(s.lowIndex)
s.intersectsLow = s.lowStart < start
s.highStart, s.highEnd = l.GetInterval(s.highIndex)
s.intersectsHigh = end < s.highEnd
}
}
func adjust(l List, at, delta int) {
if delta == 0 {
return
}
oldLen := l.Len()
newLen := oldLen + delta
if delta > 0 {
cap := l.Cap()
if cap < newLen {
newCap := newLen + (newLen >> 1)
l.GrowTo(newLen, newCap)
} else {
l.SetLen(newLen)
}
}
copyStart := at - delta
copyTo := at
if copyStart < 0 {
copyTo -= copyStart
copyStart = 0
}
l.Copy(copyTo, copyStart, newLen-copyTo)
if delta < 0 {
l.SetLen(newLen)
}
}
func replace(l List, start, end uint64, add bool) (int, uint64, uint64) {
s := intersection{}
s.intersect(l, start, end)
if s.overlap == 0 {
if add {
adjust(l, s.lowIndex, 1)
}
return s.lowIndex, start, end
}
insertLen := 0
insertPoint := s.lowIndex
if s.intersectsLow {
s.lowEnd = start
insertLen++
insertPoint++
}
if add {
insertLen++
}
if s.intersectsHigh {
s.highStart = end
insertLen++
}
delta := insertLen - s.overlap
adjust(l, insertPoint, delta)
if s.intersectsLow {
l.SetInterval(s.lowIndex, s.lowStart, s.lowEnd)
}
if s.intersectsHigh {
l.SetInterval(s.lowIndex+insertLen-1, s.highStart, s.highEnd)
}
return insertPoint, start, end
}