string_btree.gno
1.86 Kb · 78 lines
1package btree
2
3type StringIterCbFn func(key string, value any) bool
4
5type StringBTree struct {
6 tree *bTree
7}
8
9func NewStringBTree(degree int) *StringBTree {
10 return &StringBTree{tree: newBTree(degree)}
11}
12
13func (t *StringBTree) Size() int {
14 return t.tree.Size()
15}
16
17func (t *StringBTree) Set(key string, value any) bool {
18 return t.tree.Set(stringKey(key), value)
19}
20
21func (t *StringBTree) Get(key string) (any, bool) {
22 return t.tree.Get(stringKey(key))
23}
24
25func (t *StringBTree) Has(key string) bool {
26 return t.tree.Has(stringKey(key))
27}
28
29func (t *StringBTree) Remove(key string) (any, bool) {
30 return t.tree.Remove(stringKey(key))
31}
32
33func (t *StringBTree) GetByIndex(index int) (string, any) {
34 key, value := t.tree.GetByIndex(index)
35 return string(key.(stringKey)), value
36}
37
38func (t *StringBTree) Iterate(start, end *string, cb StringIterCbFn) bool {
39 var startKey key
40 if start != nil {
41 startKey = stringKey(*start)
42 }
43
44 var endKey key
45 if end != nil {
46 endKey = stringKey(*end)
47 }
48 return t.tree.Iterate(startKey, endKey, func(key key, value any) bool {
49 return cb(string(key.(stringKey)), value)
50 })
51}
52
53func (t *StringBTree) ReverseIterate(start, end *string, cb StringIterCbFn) bool {
54 var startKey key
55 if start != nil {
56 startKey = stringKey(*start)
57 }
58
59 var endKey key
60 if end != nil {
61 endKey = stringKey(*end)
62 }
63 return t.tree.ReverseIterate(startKey, endKey, func(key key, value any) bool {
64 return cb(string(key.(stringKey)), value)
65 })
66}
67
68func (t *StringBTree) IterateByOffset(offset int, count int, cb StringIterCbFn) bool {
69 return t.tree.IterateByOffset(offset, count, func(key key, value any) bool {
70 return cb(string(key.(stringKey)), value)
71 })
72}
73
74func (t *StringBTree) ReverseIterateByOffset(offset int, count int, cb StringIterCbFn) bool {
75 return t.tree.ReverseIterateByOffset(offset, count, func(key key, value any) bool {
76 return cb(string(key.(stringKey)), value)
77 })
78}