1package cid 2 3// Set is a implementation of a set of Cids, that is, a structure 4// to which holds a single copy of every Cids that is added to it. 5type Set struct { 6 set map[Cid]struct{} 7} 8 9// NewSet initializes and returns a new Set. 10func NewSet() *Set { 11 return &Set{set: make(map[Cid]struct{})} 12} 13 14// Add puts a Cid in the Set. 15func (s *Set) Add(c Cid) { 16 s.set[c] = struct{}{} 17} 18 19// Has returns if the Set contains a given Cid. 20func (s *Set) Has(c Cid) bool { 21 _, ok := s.set[c] 22 return ok 23} 24 25// Remove deletes a Cid from the Set. 26func (s *Set) Remove(c Cid) { 27 delete(s.set, c) 28} 29 30// Len returns how many elements the Set has. 31func (s *Set) Len() int { 32 return len(s.set) 33} 34 35// Keys returns the Cids in the set. 36func (s *Set) Keys() []Cid { 37 out := make([]Cid, 0, len(s.set)) 38 for k := range s.set { 39 out = append(out, k) 40 } 41 return out 42} 43 44// Visit adds a Cid to the set only if it is 45// not in it already. 46func (s *Set) Visit(c Cid) bool { 47 if !s.Has(c) { 48 s.Add(c) 49 return true 50 } 51 52 return false 53} 54 55// ForEach allows to run a custom function on each 56// Cid in the set. 57func (s *Set) ForEach(f func(c Cid) error) error { 58 for c := range s.set { 59 err := f(c) 60 if err != nil { 61 return err 62 } 63 } 64 return nil 65} 66