mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 00:07:50 +00:00
Merge pull request #4422 from derekwaynecarr/set_difference
Set should have a difference function
This commit is contained in:
commit
063ff6677c
@ -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 {
|
||||
|
@ -98,3 +98,22 @@ func TestStringSetList(t *testing.T) {
|
||||
t.Errorf("List gave unexpected result: %#v", s.List())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSetDifference(t *testing.T) {
|
||||
a := NewStringSet("1", "2", "3")
|
||||
b := NewStringSet("1", "2", "4", "5")
|
||||
c := a.Difference(b)
|
||||
d := b.Difference(a)
|
||||
if len(c) != 1 {
|
||||
t.Errorf("Expected len=1: %d", len(c))
|
||||
}
|
||||
if !c.Has("3") {
|
||||
t.Errorf("Unexpected contents: %#v", c.List())
|
||||
}
|
||||
if len(d) != 2 {
|
||||
t.Errorf("Expected len=2: %d", len(d))
|
||||
}
|
||||
if !d.Has("4") || !d.Has("5") {
|
||||
t.Errorf("Unexpected contents: %#v", d.List())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user