forked from cerc-io/ipld-eth-server
Update dependencies (#34)
This commit is contained in:
+3
@@ -162,6 +162,9 @@ type Set interface {
|
||||
// Otherwise, IsSuperset will panic.
|
||||
Union(other Set) Set
|
||||
|
||||
// Pop removes and returns an arbitrary item from the set.
|
||||
Pop() interface{}
|
||||
|
||||
// Returns all subsets of a given set (Power Set).
|
||||
PowerSet() Set
|
||||
|
||||
|
||||
+78
@@ -967,6 +967,70 @@ func Test_IteratorStop(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PopSafe(t *testing.T) {
|
||||
a := NewSet()
|
||||
|
||||
a.Add("a")
|
||||
a.Add("b")
|
||||
a.Add("c")
|
||||
a.Add("d")
|
||||
|
||||
captureSet := NewSet()
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
finalNil := a.Pop()
|
||||
|
||||
if captureSet.Cardinality() != 4 {
|
||||
t.Error("unexpected captureSet cardinality; should be 4")
|
||||
}
|
||||
|
||||
if a.Cardinality() != 0 {
|
||||
t.Error("unepxected a cardinality; should be zero")
|
||||
}
|
||||
|
||||
if !captureSet.Contains("c", "a", "d", "b") {
|
||||
t.Error("unexpected result set; should be a,b,c,d (any order is fine")
|
||||
}
|
||||
|
||||
if finalNil != nil {
|
||||
t.Error("when original set is empty, further pops should result in nil")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PopUnsafe(t *testing.T) {
|
||||
a := NewThreadUnsafeSet()
|
||||
|
||||
a.Add("a")
|
||||
a.Add("b")
|
||||
a.Add("c")
|
||||
a.Add("d")
|
||||
|
||||
captureSet := NewThreadUnsafeSet()
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
captureSet.Add(a.Pop())
|
||||
finalNil := a.Pop()
|
||||
|
||||
if captureSet.Cardinality() != 4 {
|
||||
t.Error("unexpected captureSet cardinality; should be 4")
|
||||
}
|
||||
|
||||
if a.Cardinality() != 0 {
|
||||
t.Error("unepxected a cardinality; should be zero")
|
||||
}
|
||||
|
||||
if !captureSet.Contains("c", "a", "d", "b") {
|
||||
t.Error("unexpected result set; should be a,b,c,d (any order is fine")
|
||||
}
|
||||
|
||||
if finalNil != nil {
|
||||
t.Error("when original set is empty, further pops should result in nil")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PowerSet(t *testing.T) {
|
||||
a := NewThreadUnsafeSet()
|
||||
|
||||
@@ -981,6 +1045,20 @@ func Test_PowerSet(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_PowerSetThreadSafe(t *testing.T) {
|
||||
set := NewSet().PowerSet()
|
||||
_, setIsThreadSafe := set.(*threadSafeSet)
|
||||
if !setIsThreadSafe {
|
||||
t.Error("result of PowerSet should be thread safe")
|
||||
}
|
||||
|
||||
subset := set.Pop()
|
||||
_, subsetIsThreadSafe := subset.(*threadSafeSet)
|
||||
if !subsetIsThreadSafe {
|
||||
t.Error("subsets in PowerSet result should be thread safe")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_EmptySetProperties(t *testing.T) {
|
||||
empty := NewSet()
|
||||
|
||||
|
||||
+13
-1
@@ -226,11 +226,23 @@ func (set *threadSafeSet) String() string {
|
||||
|
||||
func (set *threadSafeSet) PowerSet() Set {
|
||||
set.RLock()
|
||||
ret := set.s.PowerSet()
|
||||
unsafePowerSet := set.s.PowerSet().(*threadUnsafeSet)
|
||||
set.RUnlock()
|
||||
|
||||
ret := &threadSafeSet{s: newThreadUnsafeSet()}
|
||||
for subset := range unsafePowerSet.Iter() {
|
||||
unsafeSubset := subset.(*threadUnsafeSet)
|
||||
ret.Add(&threadSafeSet{s: *unsafeSubset})
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (set *threadSafeSet) Pop() interface{} {
|
||||
set.Lock()
|
||||
defer set.Unlock()
|
||||
return set.s.Pop()
|
||||
}
|
||||
|
||||
func (set *threadSafeSet) CartesianProduct(other Set) Set {
|
||||
o := other.(*threadSafeSet)
|
||||
|
||||
|
||||
+13
-1
@@ -57,8 +57,12 @@ func (pair *OrderedPair) Equal(other OrderedPair) bool {
|
||||
|
||||
func (set *threadUnsafeSet) Add(i interface{}) bool {
|
||||
_, found := (*set)[i]
|
||||
if found {
|
||||
return false //False if it existed already
|
||||
}
|
||||
|
||||
(*set)[i] = struct{}{}
|
||||
return !found //False if it existed already
|
||||
return true
|
||||
}
|
||||
|
||||
func (set *threadUnsafeSet) Contains(i ...interface{}) bool {
|
||||
@@ -233,6 +237,14 @@ func (pair OrderedPair) String() string {
|
||||
return fmt.Sprintf("(%v, %v)", pair.First, pair.Second)
|
||||
}
|
||||
|
||||
func (set *threadUnsafeSet) Pop() interface{} {
|
||||
for item := range *set {
|
||||
delete(*set, item)
|
||||
return item
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (set *threadUnsafeSet) PowerSet() Set {
|
||||
powSet := NewThreadUnsafeSet()
|
||||
nullset := newThreadUnsafeSet()
|
||||
|
||||
Reference in New Issue
Block a user