Skip to content

Commit 8f7e665

Browse files
committed
Pop returns an arbitrary item from the set.
1 parent b87793d commit 8f7e665

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

set.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ type Set interface {
162162
// Otherwise, IsSuperset will panic.
163163
Union(other Set) Set
164164

165+
// Pop removes and returns an arbitrary item from the set.
166+
Pop() interface{}
167+
165168
// Returns all subsets of a given set (Power Set).
166169
PowerSet() Set
167170

threadsafe.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ func (set *threadSafeSet) PowerSet() Set {
231231
return ret
232232
}
233233

234+
func (set *threadSafeSet) Pop() interface{} {
235+
set.Lock()
236+
defer set.Unlock()
237+
return set.Pop()
238+
}
239+
234240
func (set *threadSafeSet) CartesianProduct(other Set) Set {
235241
o := other.(*threadSafeSet)
236242

threadunsafe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ func (pair OrderedPair) String() string {
237237
return fmt.Sprintf("(%v, %v)", pair.First, pair.Second)
238238
}
239239

240+
func (set *threadUnsafeSet) Pop() interface{} {
241+
for item := range *set {
242+
delete(*set, item)
243+
return item
244+
}
245+
return nil
246+
}
247+
240248
func (set *threadUnsafeSet) PowerSet() Set {
241249
powSet := NewThreadUnsafeSet()
242250
nullset := newThreadUnsafeSet()

0 commit comments

Comments
 (0)