-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathbtree.go
106 lines (84 loc) · 2.35 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
package collections
import "github.com/tidwall/btree"
type dictionaryViaBTree[TKey, TValue any] struct {
b *btree.Generic[Pair[TKey, TValue]]
less func(TKey, TKey) bool
}
var _ interface{ Dictionary[int, int] } = (*dictionaryViaBTree[int, int])(nil)
func newDictionaryViaBTree[TKey, TValue any](less func(TKey, TKey) bool) *dictionaryViaBTree[TKey, TValue] {
return &dictionaryViaBTree[TKey, TValue]{
b: btree.NewGeneric(func(a, b Pair[TKey, TValue]) bool {
return less(a.First, b.First)
}),
less: less,
}
}
func (d *dictionaryViaBTree[TKey, TValue]) Clear() {
less := d.less
d.b = btree.NewGeneric(func(a, b Pair[TKey, TValue]) bool {
return less(a.First, b.First)
})
d.less = less
}
func (d *dictionaryViaBTree[TKey, TValue]) Delete(key TKey) {
var zero TValue
d.b.Delete(NewPair(key, zero))
}
func (d *dictionaryViaBTree[TKey, TValue]) ForEach(callback func(Pair[TKey, TValue]) bool) {
d.b.Scan(func(item Pair[TKey, TValue]) bool {
return callback(item)
})
}
func (d *dictionaryViaBTree[TKey, TValue]) Get(key TKey) (value TValue, ok bool) {
item, ok := d.b.Get(NewPair(key, value))
if !ok {
return value, ok
}
return item.Second, true
}
func (d *dictionaryViaBTree[TKey, TValue]) Keys() Collection[TKey] {
return Map[Pair[TKey, TValue]](d, func(pair Pair[TKey, TValue]) TKey {
return pair.First
})
}
func (d *dictionaryViaBTree[TKey, TValue]) Set(key TKey, value TValue) {
d.b.Set(NewPair(key, value))
}
func (d *dictionaryViaBTree[TKey, TValue]) Size() int {
return d.b.Len()
}
func (d *dictionaryViaBTree[TKey, TValue]) Values() Collection[TValue] {
return Map[Pair[TKey, TValue]](d, func(pair Pair[TKey, TValue]) TValue {
return pair.Second
})
}
type setViaBTree[T any] struct {
b *btree.Generic[T]
less func(T, T) bool
}
var _ interface{ Set[int] } = (*setViaBTree[int])(nil)
func newSetViaBTree[T any](less func(T, T) bool) *setViaBTree[T] {
return &setViaBTree[T]{
b: btree.NewGeneric(less),
less: less,
}
}
func (s *setViaBTree[T]) Add(value T) {
s.b.Set(value)
}
func (s *setViaBTree[T]) Clear() {
s.b = btree.NewGeneric(s.less)
}
func (s *setViaBTree[T]) Delete(value T) {
s.b.Delete(value)
}
func (s *setViaBTree[T]) ForEach(callback func(T) bool) {
s.b.Scan(callback)
}
func (s *setViaBTree[T]) Has(value T) bool {
_, ok := s.b.Get(value)
return ok
}
func (s *setViaBTree[T]) Size() int {
return s.b.Len()
}