forked from tidwall/btree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.go
399 lines (349 loc) · 8.63 KB
/
btree.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
// Copyright 2020 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package btree
type BTree struct {
base *BTreeG[any]
}
// New returns a new BTree
func New(less func(a, b any) bool) *BTree {
if less == nil {
panic("nil less")
}
return &BTree{base: NewBTreeG(less)}
}
// NewNonConcurrent returns a new BTree which is not safe for concurrent
// write operations by multiple goroutines.
//
// This is useful for when you do not need the BTree to manage the locking,
// but would rather do it yourself.
//
// Deprecated: use NewOptions
func NewNonConcurrent(less func(a, b any) bool) *BTree {
if less == nil {
panic("nil less")
}
return &BTree{base: NewBTreeGOptions(less, Options{NoLocks: true})}
}
// NewOptions returns a new BTree
func NewOptions(less func(a, b any) bool, opts Options) *BTree {
if less == nil {
panic("nil less")
}
return &BTree{base: NewBTreeGOptions(less, opts)}
}
// Less is a convenience function that performs a comparison of two items
// using the same "less" function provided to New.
func (tr *BTree) Less(a, b any) bool {
return tr.base.Less(a, b)
}
// Set or replace a value for a key
// Returns the value for the replaced item or nil if the key was not found.
func (tr *BTree) Set(item any) (prev any) {
return tr.SetHint(item, nil)
}
// SetHint sets or replace a value for a key using a path hint
// Returns the value for the replaced item or nil if the key was not found.
func (tr *BTree) SetHint(item any, hint *PathHint) (prev any) {
if item == nil {
panic("nil item")
}
v, ok := tr.base.SetHint(item, hint)
if !ok {
return nil
}
return v
}
// Get a value for key.
// Returns nil if the key was not found.
func (tr *BTree) Get(key any) any {
return tr.getHintMut(key, nil, false)
}
func (tr *BTree) GetMut(key any) any {
return tr.getHintMut(key, nil, true)
}
func (tr *BTree) GetHint(key any, hint *PathHint) any {
return tr.getHintMut(key, hint, false)
}
func (tr *BTree) GetHintMut(key any, hint *PathHint) any {
return tr.getHintMut(key, hint, true)
}
// GetHint gets a value for key using a path hint.
// Returns nil if the item was not found.
func (tr *BTree) getHintMut(key any, hint *PathHint, mut bool) (value any) {
if key == nil {
return nil
}
var v any
var ok bool
if mut {
v, ok = tr.base.GetHintMut(key, hint)
} else {
v, ok = tr.base.GetHint(key, hint)
}
if !ok {
return nil
}
return v
}
// Len returns the number of items in the tree
func (tr *BTree) Len() int {
return tr.base.Len()
}
// Delete an item for a key.
// Returns the deleted value or nil if the key was not found.
func (tr *BTree) Delete(key any) (prev any) {
return tr.DeleteHint(key, nil)
}
// DeleteHint deletes a value for a key using a path hint
// Returns the deleted value or nil if the key was not found.
func (tr *BTree) DeleteHint(key any, hint *PathHint) (prev any) {
if key == nil {
return nil
}
v, ok := tr.base.DeleteHint(key, nil)
if !ok {
return nil
}
return v
}
// Ascend the tree within the range [pivot, last]
// Pass nil for pivot to scan all item in ascending order
// Return false to stop iterating
func (tr *BTree) Ascend(pivot any, iter func(item any) bool) {
if pivot == nil {
tr.base.Scan(iter)
} else {
tr.base.Ascend(pivot, iter)
}
}
func (tr *BTree) AscendMut(pivot any, iter func(item any) bool) {
if pivot == nil {
tr.base.ScanMut(iter)
} else {
tr.base.AscendMut(pivot, iter)
}
}
func (tr *BTree) AscendHint(pivot any, iter func(item any) bool,
hint *PathHint,
) {
if pivot == nil {
tr.base.Scan(iter)
} else {
tr.base.AscendHint(pivot, iter, hint)
}
}
func (tr *BTree) AscendHintMut(pivot any, iter func(item any) bool,
hint *PathHint,
) {
if pivot == nil {
tr.base.ScanMut(iter)
} else {
tr.base.AscendHintMut(pivot, iter, hint)
}
}
// Descend the tree within the range [pivot, first]
// Pass nil for pivot to scan all item in descending order
// Return false to stop iterating
func (tr *BTree) Descend(pivot any, iter func(item any) bool) {
if pivot == nil {
tr.base.Reverse(iter)
} else {
tr.base.Descend(pivot, iter)
}
}
func (tr *BTree) DescendMut(pivot any, iter func(item any) bool) {
if pivot == nil {
tr.base.ReverseMut(iter)
} else {
tr.base.DescendMut(pivot, iter)
}
}
func (tr *BTree) DescendHint(pivot any, iter func(item any) bool,
hint *PathHint,
) {
if pivot == nil {
tr.base.Reverse(iter)
} else {
tr.base.DescendHint(pivot, iter, hint)
}
}
func (tr *BTree) DescendHintMut(pivot any, iter func(item any) bool,
hint *PathHint,
) {
if pivot == nil {
tr.base.ReverseMut(iter)
} else {
tr.base.DescendHintMut(pivot, iter, hint)
}
}
// Load is for bulk loading pre-sorted items
// If the load replaces and existing item then the value for the replaced item
// is returned.
func (tr *BTree) Load(item any) (prev any) {
if item == nil {
panic("nil item")
}
v, ok := tr.base.Load(item)
if !ok {
return nil
}
return v
}
// Min returns the minimum item in tree.
// Returns nil if the tree has no items.
func (tr *BTree) Min() any {
v, ok := tr.base.Min()
if !ok {
return nil
}
return v
}
func (tr *BTree) MinMut() any {
v, ok := tr.base.MinMut()
if !ok {
return nil
}
return v
}
// Max returns the maximum item in tree.
// Returns nil if the tree has no items.
func (tr *BTree) Max() any {
v, ok := tr.base.Max()
if !ok {
return nil
}
return v
}
func (tr *BTree) MaxMut() any {
v, ok := tr.base.Max()
if !ok {
return nil
}
return v
}
// PopMin removes the minimum item in tree and returns it.
// Returns nil if the tree has no items.
func (tr *BTree) PopMin() any {
v, ok := tr.base.PopMin()
if !ok {
return nil
}
return v
}
// PopMax removes the maximum item in tree and returns it.
// Returns nil if the tree has no items.
func (tr *BTree) PopMax() any {
v, ok := tr.base.PopMax()
if !ok {
return nil
}
return v
}
// GetAt returns the value at index.
// Return nil if the tree is empty or the index is out of bounds.
func (tr *BTree) GetAt(index int) any {
v, ok := tr.base.GetAt(index)
if !ok {
return nil
}
return v
}
func (tr *BTree) GetAtMut(index int) any {
v, ok := tr.base.GetAtMut(index)
if !ok {
return nil
}
return v
}
// DeleteAt deletes the item at index.
// Return nil if the tree is empty or the index is out of bounds.
func (tr *BTree) DeleteAt(index int) any {
v, ok := tr.base.DeleteAt(index)
if !ok {
return nil
}
return v
}
// Height returns the height of the tree.
// Returns zero if tree has no items.
func (tr *BTree) Height() int {
return tr.base.Height()
}
// Walk iterates over all items in tree, in order.
// The items param will contain one or more items.
func (tr *BTree) Walk(iter func(items []any)) {
tr.base.Walk(func(items []any) bool {
iter(items)
return true
})
}
func (tr *BTree) WalkMut(iter func(items []any)) {
tr.base.WalkMut(func(items []any) bool {
iter(items)
return true
})
}
// Copy the tree. This is a copy-on-write operation and is very fast because
// it only performs a shadowed copy.
func (tr *BTree) Copy() *BTree {
return &BTree{base: tr.base.Copy()}
}
func (tr *BTree) IsoCopy() *BTree {
return &BTree{base: tr.base.IsoCopy()}
}
// Clear will delete all items.
func (tr *BTree) Clear() {
tr.base.Clear()
}
// Iter is an iterator for
type Iter struct {
base IterG[any]
}
// Iter returns a read-only iterator.
// The Release method must be called finished with iterator.
func (tr *BTree) Iter() Iter {
return Iter{tr.base.Iter()}
}
func (tr *BTree) IterMut() Iter {
return Iter{tr.base.IterMut()}
}
// Seek to item greater-or-equal-to key.
// Returns false if there was no item found.
func (iter *Iter) Seek(key any) bool {
return iter.base.Seek(key)
}
func (iter *Iter) SeekHint(key any, hint *PathHint) bool {
return iter.base.SeekHint(key, hint)
}
// First moves iterator to first item in tree.
// Returns false if the tree is empty.
func (iter *Iter) First() bool {
return iter.base.First()
}
// Last moves iterator to last item in tree.
// Returns false if the tree is empty.
func (iter *Iter) Last() bool {
return iter.base.Last()
}
// First moves iterator to first item in tree.
// Returns false if the tree is empty.
func (iter *Iter) Release() {
iter.base.Release()
}
// Next moves iterator to the next item in iterator.
// Returns false if the tree is empty or the iterator is at the end of
// the tree.
func (iter *Iter) Next() bool {
return iter.base.Next()
}
// Prev moves iterator to the previous item in iterator.
// Returns false if the tree is empty or the iterator is at the beginning of
// the tree.
func (iter *Iter) Prev() bool {
return iter.base.Prev()
}
// Item returns the current iterator item.
func (iter *Iter) Item() any {
return iter.base.Item()
}