RichΛrd 40359f9c1b
go-waku integration (#2247)
* Adding wakunode module
* Adding wakuv2 fleet files
* Add waku fleets to update-fleet-config script
* Adding config items for waku v2
* Conditionally start waku v2 node depending on config
* Adapting common code to use go-waku
* Setting log level to info
* update dependencies
* update fleet config to use WakuNodes instead of BootNodes
* send and receive messages
* use hash returned when publishing a message
* add waku store protocol
* trigger signal after receiving store messages
* exclude linting rule SA1019 to check deprecated packages
2021-06-16 16:19:45 -04:00

67 lines
1.4 KiB
Go

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
}