-
Notifications
You must be signed in to change notification settings - Fork 35
/
set.go
73 lines (60 loc) · 1.85 KB
/
set.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
// Ported from specs-actors: https://github.com/filecoin-project/specs-actors/blob/845089a6d2580e46055c24415a6c32ee688e5186/actors/util/adt/set.go#L8
package adt
import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/ipfs/go-cid"
)
// Set interprets a Map as a set, storing keys (with empty values) in a HAMT.
type Set struct {
m *Map
}
// AsSet interprets a store as a HAMT-based set with root `r`.
// The HAMT is interpreted with branching factor 2^bitwidth.
func AsSet(s Store, r cid.Cid, bitwidth int) (*Set, error) {
m, err := AsMap(s, r, bitwidth)
if err != nil {
return nil, err
}
return &Set{
m: m,
}, nil
}
// NewSet creates a new HAMT with root `r` and store `s`.
// The HAMT has branching factor 2^bitwidth.
func MakeEmptySet(s Store, bitwidth int) (*Set, error) {
m, err := MakeEmptyMap(s, bitwidth)
if err != nil {
return nil, err
}
return &Set{m}, nil
}
// Root return the root cid of HAMT.
func (h *Set) Root() (cid.Cid, error) {
return h.m.Root()
}
// Put adds `k` to the set.
func (h *Set) Put(k abi.Keyer) error {
return h.m.Put(k, nil)
}
// Has returns true iff `k` is in the set.
func (h *Set) Has(k abi.Keyer) (bool, error) {
return h.m.Get(k, nil)
}
// Removes `k` from the set, if present.
// Returns whether the key was previously present.
func (h *Set) TryDelete(k abi.Keyer) (bool, error) {
return h.m.TryDelete(k)
}
// Removes `k` from the set, expecting it to be present.
func (h *Set) Delete(k abi.Keyer) error {
return h.m.Delete(k)
}
// ForEach iterates over all values in the set, calling the callback for each value.
// Returning error from the callback stops the iteration.
func (h *Set) ForEach(cb func(k string) error) error {
return h.m.ForEach(nil, cb)
}
// Collects all the keys from the set into a slice of strings.
func (h *Set) CollectKeys() (out []string, err error) {
return h.m.CollectKeys()
}