Merge pull request #4422 from derekwaynecarr/set_difference

Set should have a difference function
This commit is contained in:
Tim Hockin
2015-02-13 14:16:28 -08:00
2 changed files with 35 additions and 0 deletions

View File

@@ -75,6 +75,22 @@ func (s StringSet) HasAll(items ...string) bool {
return true
}
// Difference returns a set of objects that are not in s2
// For example:
// s1 = {1, 2, 3}
// s2 = {1, 2, 4, 5}
// s1.Difference(s2) = {3}
// s2.Difference(s1) = {4, 5}
func (s StringSet) Difference(s2 StringSet) StringSet {
result := NewStringSet()
for key := range s {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
}
// IsSuperset returns true iff s1 is a superset of s2.
func (s1 StringSet) IsSuperset(s2 StringSet) bool {
for item := range s2 {