-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.go
59 lines (49 loc) · 1.59 KB
/
store.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
package immutable
// Store is a Hash Array Mapped Trie with an external split function.
type Store[D any, K comparable, V any] struct {
amt[K, V]
split func(D) (K, V)
}
func StoreWith[D any, K comparable, V any](splitter func(D) (K, V)) Store[D, K, V] {
return Store[D, K, V]{amt[K, V]{}, splitter}
}
// Len returns the number of entries that are present.
func (a Store[D, K, V]) Len() int {
return a.len()
}
// Depth returns the number of levels in the Hamt. Calling Depth on an empty
// Hamt returns 1.
func (a Store[D, K, V]) Depth() int {
return a.depth()
}
// Has returns true when an entry with the given key is present.
func (a Store[D, K, V]) Has(data D) bool {
k, _ := a.split(data)
_, b := a.lookup(hash(k), 0, k)
return b
}
// Get returns the value for the entry with the given key or nil when it is
// not present.
func (a Store[D, K, V]) Get(data D) any {
k, _ := a.split(data)
v, _ := a.lookup(hash(k), 0, k)
return v
}
// Range calls the given function for every key,value pair present.
func (a Store[D, K, V]) Range(f func(K, V) bool) {
a.foreach(f)
}
// String returns a string representation of the key,value pairs present.
func (a Store[D, K, V]) String() string {
return a.string()
}
// Put returns a copy of the Set with the key as part of the set.
func (a Store[D, K, V]) Put(data D) Store[D, K, V] {
k, v := a.split(data)
return Store[D, K, V]{a.set(hash(k), 0, k, v), a.split}
}
// Del returns a copy of the Store with the key removed from the set.
func (a Store[D, K, V]) Del(data D) Store[D, K, V] {
k, _ := a.split(data)
return Store[D, K, V]{a.delete(hash(k), 0, k), a.split}
}