Merge pull request #28329 from serejkus/dag/set-tiny-optimisations

tiny optimisations of dag.Set
This commit is contained in:
James Bardin 2021-04-14 16:23:43 -04:00 committed by GitHub
commit 2cd1619c40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -56,13 +56,13 @@ func (s Set) Intersection(other Set) Set {
// Difference returns a set with the elements that s has but
// other doesn't.
func (s Set) Difference(other Set) Set {
if other == nil || other.Len() == 0 {
return s.Copy()
}
result := make(Set)
for k, v := range s {
var ok bool
if other != nil {
_, ok = other[k]
}
if !ok {
if _, ok := other[k]; !ok {
result.Add(v)
}
}
@ -105,7 +105,7 @@ func (s Set) List() []interface{} {
// Copy returns a shallow copy of the set.
func (s Set) Copy() Set {
c := make(Set)
c := make(Set, len(s))
for k, v := range s {
c[k] = v
}

View File

@ -31,6 +31,12 @@ func TestSetDifference(t *testing.T) {
[]interface{}{3, 2, 1, 4},
[]interface{}{},
},
{
"B is nil",
[]interface{}{1, 2, 3},
nil,
[]interface{}{1, 2, 3},
},
}
for i, tc := range cases {
@ -44,6 +50,9 @@ func TestSetDifference(t *testing.T) {
for _, v := range tc.B {
two.Add(v)
}
if tc.B == nil {
two = nil
}
for _, v := range tc.Expected {
expected.Add(v)
}