feat: use CIDs as their byte representation instead of a struct

This commit is contained in:
dignifiedquire
2018-05-03 15:12:25 +02:00
committed by Kevin Atkinson
parent 6ddb575a8d
commit e153340e5a
6 changed files with 127 additions and 117 deletions

14
set.go
View File

@@ -12,18 +12,18 @@ func NewSet() *Set {
}
// Add puts a Cid in the Set.
func (s *Set) Add(c *Cid) {
func (s *Set) Add(c Cid) {
s.set[string(c.Bytes())] = struct{}{}
}
// Has returns if the Set contains a given Cid.
func (s *Set) Has(c *Cid) bool {
func (s *Set) Has(c Cid) bool {
_, ok := s.set[string(c.Bytes())]
return ok
}
// Remove deletes a Cid from the Set.
func (s *Set) Remove(c *Cid) {
func (s *Set) Remove(c Cid) {
delete(s.set, string(c.Bytes()))
}
@@ -33,8 +33,8 @@ func (s *Set) Len() int {
}
// Keys returns the Cids in the set.
func (s *Set) Keys() []*Cid {
out := make([]*Cid, 0, len(s.set))
func (s *Set) Keys() []Cid {
out := make([]Cid, 0, len(s.set))
for k := range s.set {
c, _ := Cast([]byte(k))
out = append(out, c)
@@ -44,7 +44,7 @@ func (s *Set) Keys() []*Cid {
// Visit adds a Cid to the set only if it is
// not in it already.
func (s *Set) Visit(c *Cid) bool {
func (s *Set) Visit(c Cid) bool {
if !s.Has(c) {
s.Add(c)
return true
@@ -55,7 +55,7 @@ func (s *Set) Visit(c *Cid) bool {
// ForEach allows to run a custom function on each
// Cid in the set.
func (s *Set) ForEach(f func(c *Cid) error) error {
func (s *Set) ForEach(f func(c Cid) error) error {
for cs := range s.set {
c, _ := Cast([]byte(cs))
err := f(c)