github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/util/adt/set.go (about) 1 package adt 2 3 import ( 4 "github.com/filecoin-project/go-state-types/abi" 5 cid "github.com/ipfs/go-cid" 6 ) 7 8 // Set interprets a Map as a set, storing keys (with empty values) in a HAMT. 9 type Set struct { 10 m *Map 11 } 12 13 // AsSet interprets a store as a HAMT-based set with root `r`. 14 // The HAMT is interpreted with branching factor 2^bitwidth. 15 func AsSet(s Store, r cid.Cid, bitwidth int) (*Set, error) { 16 m, err := AsMap(s, r, bitwidth) 17 if err != nil { 18 return nil, err 19 } 20 21 return &Set{ 22 m: m, 23 }, nil 24 } 25 26 // NewSet creates a new HAMT with root `r` and store `s`. 27 // The HAMT has branching factor 2^bitwidth. 28 func MakeEmptySet(s Store, bitwidth int) (*Set, error) { 29 m, err := MakeEmptyMap(s, bitwidth) 30 if err != nil { 31 return nil, err 32 } 33 return &Set{m}, nil 34 } 35 36 // Root return the root cid of HAMT. 37 func (h *Set) Root() (cid.Cid, error) { 38 return h.m.Root() 39 } 40 41 // Put adds `k` to the set. 42 func (h *Set) Put(k abi.Keyer) error { 43 return h.m.Put(k, nil) 44 } 45 46 // Has returns true iff `k` is in the set. 47 func (h *Set) Has(k abi.Keyer) (bool, error) { 48 return h.m.Get(k, nil) 49 } 50 51 // Removes `k` from the set, if present. 52 // Returns whether the key was previously present. 53 func (h *Set) TryDelete(k abi.Keyer) (bool, error) { 54 return h.m.TryDelete(k) 55 } 56 57 // Removes `k` from the set, expecting it to be present. 58 func (h *Set) Delete(k abi.Keyer) error { 59 return h.m.Delete(k) 60 } 61 62 // ForEach iterates over all values in the set, calling the callback for each value. 63 // Returning error from the callback stops the iteration. 64 func (h *Set) ForEach(cb func(k string) error) error { 65 return h.m.ForEach(nil, cb) 66 } 67 68 // Collects all the keys from the set into a slice of strings. 69 func (h *Set) CollectKeys() (out []string, err error) { 70 return h.m.CollectKeys() 71 }