-
Notifications
You must be signed in to change notification settings - Fork 13
/
bucket.go
72 lines (58 loc) · 1003 Bytes
/
bucket.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
package hamt
type bucket[T Entry[T]] []T
func newBucket[T Entry[T]]() bucket[T] {
return bucket[T](nil)
}
func (b bucket[T]) Insert(e T) node[T] {
for i, f := range b {
if e.Equal(f) {
new := make(bucket[T], len(b))
copy(new, b)
new[i] = e
return new
}
}
return append(b, e)
}
func (b bucket[T]) Find(e T) *T {
for _, f := range b {
if e.Equal(f) {
return &f
}
}
return nil
}
func (b bucket[T]) Delete(e T) (node[T], bool) {
for i, f := range b {
if e.Equal(f) {
return append(b[:i], b[i+1:]...), true
}
}
return b, false
}
func (b bucket[T]) FirstRest() (*T, node[T]) {
if len(b) == 0 {
return nil, b
}
return &b[0], b[1:]
}
func (b bucket[T]) ForEach(cb func(T) error) error {
for _, e := range b {
if err := cb(e); err != nil {
return err
}
}
return nil
}
func (b bucket[T]) State() nodeState {
switch len(b) {
case 0:
return empty
case 1:
return singleton
}
return more
}
func (b bucket[T]) Size() int {
return len(b)
}