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 1 commit
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
}
43 changes: 43 additions & 0 deletions pkg/common/stringset/stringset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,46 @@ func TestStringSet_ToSlice(t *testing.T) {
assert.True(t, testSet.Contains(item))
}
}

func TestStringSet_Intersect(t *testing.T) {
testSet := &stringSet{
m: make(map[string]bool),
}

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

testItems := []string{
"testitem1",
"testitem2",
}

// Add testItems to testSet
for _, item := range testItems {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use string set Add method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it but then tests will be different from previous ones.

testSet.m[item] = true
}

testItems1 := []string{
"testitem1",
"testitem2",
"testitem3",
}

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

// Intersect the two stringSet
intersection := testSet.Intersect(testSet1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use test table and add more unit tests such as

  • No intersection result
  • Empty sets
  • both set are purely different.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use table driven tests, rather than creating separate tests.
https://github.com/golang/go/wiki/TableDrivenTests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool stuff!
Thanks I understand what you meant now.
Tests unified in one according to TableDrivenTests.


items := intersection.ToSlice()

for _, item := range items {
if item == "testitem3" {
assert.False(t, testSet.Contains(item))
}
assert.True(t, testSet.Contains(item))
}
}