Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intersect util method for StringSet #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pkg/common/stringset/stringset.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,60 @@ func (s *stringSet) ToSlice() []string {
}
return keys
}

// Intersect returns the intersection between two StringSet
func (s *stringSet) Intersect(other *stringSet) (intersection *stringSet) {
var ret *stringSet
var wg sync.WaitGroup

var slen, otherlen int

createIntersect := func(smallerlen int, smaller, greater *stringSet) (ret *stringSet) {
ret = &stringSet{
m: make(map[string]bool, smallerlen),
}
// Copy smaller set in ret
smaller.Lock()
for str := range smaller.m {
ret.m[str] = true
}
smaller.Unlock()

greater.Lock()
defer greater.Unlock()
for element := range ret.m {
// If element in smaller exists also in greater moves along
if _, exists := greater.m[element]; exists {
continue
}
// otherwise deletes it also from ret
ret.Lock()
delete(ret.m, element)
ret.Unlock()
}
return ret
}

wg.Add(2)
go func() {
defer wg.Done()
s.Lock()
slen = len(s.m)
s.Unlock()
}()
go func() {
defer wg.Done()
other.Lock()
otherlen = len(other.m)
other.Unlock()
}()
wg.Wait()
switch {
case slen >= otherlen:
ret = createIntersect(otherlen, other, s)

case slen < otherlen:
ret = createIntersect(slen, s, other)
}
return ret
}
54 changes: 54 additions & 0 deletions pkg/common/stringset/stringset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package stringset

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -97,3 +98,56 @@ func TestStringSet_ToSlice(t *testing.T) {
assert.True(t, testSet.Contains(item))
}
}

var flagtests = []struct {
name string
testdata1 []string
testdata2 []string
out []string
}{
{"both sets void", []string{}, []string{}, []string{}},
{"first set void", []string{}, []string{"testitem1", "testitem2"}, []string{}},
{"second set void", []string{"testitem2", "testitem1"}, []string{}, []string{}},
{"intersect all first set", []string{"testitem1"}, []string{"testitem1", "testitem2"}, []string{"testitem1"}},
{"intersect all second set", []string{"testitem1", "testitem2"}, []string{"testitem1"}, []string{"testitem1"}},
{"interscet subset of first set", []string{"testitem1", "testitem2", "testitem3"}, []string{"testitem1", "testitem2"}, []string{"testitem1", "testitem2"}},
{"interscet subset of second set", []string{"testitem1", "testitem2"}, []string{"testitem1", "testitem2", "testitem3"}, []string{"testitem1", "testitem2"}},
}

func TestStringSet_Intersect(t *testing.T) {

for _, tt := range flagtests {
t.Run(tt.name, func(t *testing.T) {

// Creates first testSet
testSet1 := &stringSet{
m: make(map[string]bool),
}

// Add testItems to testSet1
for _, item := range tt.testdata1 {
testSet1.m[item] = true
}

// Creates second testSet
testSet2 := &stringSet{
m: make(map[string]bool),
}

// Add testItems to testSet2
for _, item := range tt.testdata2 {
testSet2.m[item] = true
}
s := testSet1.Intersect(testSet2)
slice := s.ToSlice()
sort.Strings(slice)
sort.Strings(tt.out)

for i := range slice {
if slice[i] != tt.out[i] {
t.Errorf("got %q, want %q", slice[i], tt.out[i])
}
}
})
}
}