-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsortlist.go
354 lines (323 loc) · 6.92 KB
/
sortlist.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
package gosolist
const (
DefaultLoadFactor = 1000
)
type SortedList struct {
offset int
load int
maxes []interface{}
lists [][]interface{}
indexes []int //index sum tree
size int
c Compare
}
func (l *SortedList) Push(a interface{}) {
l.size++
if len(l.maxes) == 0 {
l.maxes = append(l.maxes, a)
l.lists = append(l.lists, []interface{}{a})
return
}
pos := BisectLeft(l.maxes, l.c, a)
if pos > 0 && l.maxes[pos-1] == a {
pos--
}
if pos == len(l.maxes) {
pos--
l.maxes[pos] = a
l.lists[pos] = append(l.lists[pos], a)
} else {
l.lists[pos] = InSort(l.lists[pos], l.c, a)
}
l.fresh(pos)
}
func (l *SortedList) DeleteItem(a interface{}) bool {
if l.size == 0 {
return false
}
pos := BisectLeft(l.maxes, l.c, a)
if pos == len(l.maxes) {
return false
}
var removed bool
l.lists[pos], removed = RemoveSort(l.lists[pos], l.c, a)
if !removed {
return removed
}
l.size--
if len(l.lists[pos]) == 0 {
// delete maxes at pos
copy(l.maxes[pos:], l.maxes[pos+1:])
l.maxes = l.maxes[:len(l.maxes)-1]
// delete lists at pos
copy(l.lists[pos:], l.lists[pos+1:])
l.lists = l.lists[:len(l.lists)-1]
l.resetIndex()
} else {
l.maxes[pos] = l.lists[pos][len(l.lists[pos])-1]
l.updateIndex(pos, -1)
}
return removed
}
func (l *SortedList) Delete(index int) {
if index >= l.size {
return
}
var pos, in int
if index == 0 {
pos, in = 0, 0
} else if index == l.size-1 {
pos = len(l.lists) - 1
in = len(l.lists[pos]) - 1
} else {
if len(l.indexes) == 0 {
l.buildIndex()
}
pos, in = l.findPos(index)
}
l.size--
l.lists[pos] = Remove(l.lists[pos], in)
if len(l.lists[pos]) == 0 {
// delete maxes at pos
l.maxes = Remove(l.maxes, pos)
// delete lists at pos
copy(l.lists[pos:], l.lists[pos+1:])
l.lists = l.lists[:len(l.lists)-1]
l.resetIndex()
} else {
l.maxes[pos] = l.lists[pos][len(l.lists[pos])-1]
l.updateIndex(pos, -1)
}
}
func (l *SortedList) Values() []interface{} {
res := make([]interface{}, l.Size())
i := 0
l.Each(func(_ int, a interface{}) {
res[i] = a
i++
})
return res
}
func (l *SortedList) At(index int) interface{} {
if index >= l.size {
return nil
}
if index < len(l.lists[0]) {
return l.lists[0][index]
}
if index == l.size-1 {
return l.maxes[len(l.maxes)-1]
}
//if l.size-index <= len(l.lists[len(l.lists)-1]) {
// return l.lists[len(l.lists)-1][l.size-index-1]
//}
if len(l.indexes) == 0 {
l.buildIndex()
}
pos, in := l.findPos(index)
return l.lists[pos][in]
}
func (l *SortedList) Each(f ForEach) {
i := 0
for _, list := range l.lists {
for _, j := range list {
f(i, j)
i++
}
}
}
func (l *SortedList) Has(a interface{}) bool {
if l.size == 0 {
return false
}
pos := BisectLeft(l.maxes, l.c, a)
if pos == len(l.maxes) {
return false
}
index := BisectLeft(l.lists[pos], l.c, a)
return l.lists[pos][index] == a
}
func (l *SortedList) Floor(a interface{}) interface{} {
if l.size == 0 {
return nil
}
pos := BisectLeft(l.maxes, l.c, a)
if pos == len(l.maxes) {
return l.maxes[pos-1]
}
index := BisectLeft(l.lists[pos], l.c, a)
if index == 0 && l.lists[pos][0] != a {
if pos == 0 {
return nil
} else {
return l.maxes[pos-1]
}
}
if l.lists[pos][index] == a {
return l.lists[pos][index]
}
return l.lists[pos][index-1]
}
func (l *SortedList) Ceil(a interface{}) interface{} {
if l.size == 0 {
return nil
}
pos := BisectLeft(l.maxes, l.c, a)
if pos == len(l.maxes) {
return nil
}
index := BisectLeft(l.lists[pos], l.c, a)
return l.lists[pos][index]
}
//Index return the index of the position where the item to insert,and if the item exist or not.
func (l *SortedList) Index(a interface{}) (int, bool) {
if l.size == 0 {
return 0, false
}
pos := BisectLeft(l.maxes, l.c, a)
if pos == len(l.maxes) {
return l.size, false
}
if a == l.lists[0][0] {
return 0, true
}
if a == l.maxes[0] {
return len(l.lists[0]) - 1, true
}
if a == l.maxes[len(l.maxes)-1] {
return l.size - 1, true
}
index := BisectLeft(l.lists[pos], l.c, a)
exist := index < len(l.lists[pos]) && l.lists[pos][index] == a
return l.locate(pos, index), exist
}
func (l *SortedList) Empty() bool {
return l.size == 0
}
func (l *SortedList) Size() int {
return l.size
}
func (l *SortedList) Clear() {
l.resetIndex()
l.lists = [][]interface{}{}
l.maxes = []interface{}{}
l.size = 0
}
func (l *SortedList) Top() interface{} {
if l.size == 0 {
return nil
}
return l.maxes[len(l.maxes)-1]
}
func (l *SortedList) Bottom() interface{} {
if l.size == 0 {
return nil
}
return l.lists[0][0]
}
// fresh update the index and rebuild basic array if the load is greater than load factor after insert
func (l *SortedList) fresh(pos int) {
listPosLen := len(l.lists[pos])
if listPosLen > l.load {
halfLen := listPosLen >> 1
half := append([]interface{}{}, l.lists[pos][halfLen:]...)
l.lists[pos] = l.lists[pos][:halfLen]
l.lists = append(l.lists, nil)
copy(l.lists[pos+2:], l.lists[pos+1:])
l.lists[pos+1] = half
// update max
l.maxes[pos] = l.lists[pos][halfLen-1]
l.maxes = append(l.maxes, nil)
copy(l.maxes[pos+2:], l.maxes[pos+1:])
l.maxes[pos+1] = l.lists[pos+1][len(l.lists[pos+1])-1]
l.resetIndex()
} else {
l.maxes[pos] = l.lists[pos][listPosLen-1]
l.updateIndex(pos, 1)
}
}
func (l *SortedList) buildIndex() {
n := len(l.lists)
rowLens := roundUpOf2((n + 1) / 2)
l.offset = rowLens*2 - 1
indexLens := l.offset + n
indexes := make([]int, indexLens)
for i, list := range l.lists { // fill row0
indexes[len(indexes)-n+i] = len(list)
}
last := indexLens - n - rowLens
for rowLens > 0 {
for i := 0; i < rowLens; i++ {
if (last+i)*2+1 >= indexLens {
break
}
if (last+i)*2+2 >= indexLens {
indexes[last+i] = indexes[(last+i)*2+1]
break
}
indexes[last+i] = indexes[(last+i)*2+1] + indexes[(last+i)*2+2]
}
rowLens >>= 1
last -= rowLens
}
l.indexes = indexes
}
func (l *SortedList) updateIndex(pos, incr int) {
if len(l.indexes) > 0 {
child := l.offset + pos
for child > 0 {
l.indexes[child] += incr
child = (child - 1) >> 1
}
l.indexes[0] += 1
}
}
func (l *SortedList) findPos(index int) (int, int) {
if index < len(l.lists[0]) {
return 0, index
}
pos := 0
child := 1
lenIndex := len(l.indexes)
for child < lenIndex {
indexChild := l.indexes[child]
if index < indexChild {
pos = child
} else {
index -= indexChild
pos = child + 1
}
child = (pos << 1) + 1
}
return pos - l.offset, index
}
func (l *SortedList) locate(pos, index int) int {
if len(l.indexes) == 0 {
l.buildIndex()
}
total := 0
pos += l.offset
for pos > 0 {
if pos&1 == 0 {
total += l.indexes[pos-1]
}
pos = (pos - 1) >> 1
}
return total + index
}
func (l *SortedList) resetIndex() {
l.indexes = []int{}
l.offset = 0
}
func roundUpOf2(a int) int {
i := 1
for ; i < a; i <<= 1 {
}
return i
}
func NewSortedList(c Compare, loadFactor int) SortedList {
if loadFactor <= 0 {
loadFactor = DefaultLoadFactor
}
return SortedList{load: loadFactor, c: c}
}