/
bucket.go
72 lines (58 loc) · 964 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 []Entry
func newBucket() bucket {
return bucket(nil)
}
func (b bucket) Insert(e Entry) node {
for i, f := range b {
if e.Equal(f) {
new := make(bucket, len(b))
copy(new, b)
new[i] = e
return new
}
}
return append(b, e)
}
func (b bucket) Find(e Entry) Entry {
for _, f := range b {
if e.Equal(f) {
return f
}
}
return nil
}
func (b bucket) Delete(e Entry) (node, bool) {
for i, f := range b {
if e.Equal(f) {
return append(b[:i], b[i+1:]...), true
}
}
return b, false
}
func (b bucket) FirstRest() (Entry, node) {
if len(b) == 0 {
return nil, b
}
return b[0], b[1:]
}
func (b bucket) ForEach(cb func(Entry) error) error {
for _, e := range b {
if err := cb(e); err != nil {
return err
}
}
return nil
}
func (b bucket) State() nodeState {
switch len(b) {
case 0:
return empty
case 1:
return singleton
}
return more
}
func (b bucket) Size() int {
return len(b)
}