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
|
package multihash
// Set is a set of Multihashes, holding one copy per Multihash.
type Set struct {
set map[string]struct{}
}
// NewSet creates a new set correctly initialized.
func NewSet() *Set {
return &Set{
set: make(map[string]struct{}),
}
}
// Add adds a new multihash to the set.
func (s *Set) Add(m Multihash) {
s.set[string(m)] = struct{}{}
}
// Len returns the number of elements in the set.
func (s *Set) Len() int {
return len(s.set)
}
// Has returns true if the element is in the set.
func (s *Set) Has(m Multihash) bool {
_, ok := s.set[string(m)]
return ok
}
// Visit adds a multihash only if it is not in the set already. Returns true
// if the multihash was added (was not in the set before).
func (s *Set) Visit(m Multihash) bool {
_, ok := s.set[string(m)]
if !ok {
s.set[string(m)] = struct{}{}
return true
}
return false
}
// ForEach runs f(m) with each multihash in the set. If returns immediately if
// f(m) returns an error.
func (s *Set) ForEach(f func(m Multihash) error) error {
for elem := range s.set {
mh := Multihash(elem)
if err := f(mh); err != nil {
return err
}
}
return nil
}
// Remove removes an element from the set.
func (s *Set) Remove(m Multihash) {
delete(s.set, string(m))
}
// All returns a slice with all the elements in the set.
func (s *Set) All() []Multihash {
out := make([]Multihash, 0, len(s.set))
for m := range s.set {
out = append(out, Multihash(m))
}
return out
}
|