Merge pull request #142 from jmdacruz/implement-set-intersection

Simple implementation of set intersection
pull/195/head
Emir Pasic 2 years ago committed by GitHub
commit ae81334c3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -97,3 +97,14 @@ func (set *Set) String() string {
str += strings.Join(items, ", ")
return str
}
// Intersection returns the intersection between two sets
func (set *Set) Intersection(another *Set) *Set {
result := New()
for item, _ := range another.items {
if set.Contains(item) {
result.Add(item)
}
}
return result
}

@ -111,6 +111,18 @@ func TestSetSerialization(t *testing.T) {
}
}
func TestSetIntersection(t *testing.T) {
set := New("a", "b", "c", "d")
anotherSet := New("c", "d", "f", "g")
intersection := set.Intersection(anotherSet)
if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue := set.Contains("c", "d"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
}
func benchmarkContains(b *testing.B, set *Set, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {

@ -116,3 +116,14 @@ func (set *Set) String() string {
str += strings.Join(items, ", ")
return str
}
// Intersection returns the intersection between two sets
func (set *Set) Intersection(another *Set) *Set {
result := New()
for item, _ := range another.table {
if set.Contains(item) {
result.Add(item)
}
}
return result
}

@ -465,6 +465,18 @@ func TestSetSerialization(t *testing.T) {
}
}
func TestSetIntersection(t *testing.T) {
set := New("a", "b", "c", "d")
anotherSet := New("c", "d", "f", "g")
intersection := set.Intersection(anotherSet)
if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue := set.Contains("c", "d"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
}
func benchmarkContains(b *testing.B, set *Set, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {

@ -16,6 +16,7 @@ type Set interface {
Add(elements ...interface{})
Remove(elements ...interface{})
Contains(elements ...interface{}) bool
Intersection(another *Set) *Set
containers.Container
// Empty() bool

@ -111,3 +111,14 @@ func (set *Set) String() string {
str += strings.Join(items, ", ")
return str
}
// Intersection returns the intersection between two sets
func (set *Set) Intersection(another *Set) *Set {
result := NewWith(set.tree.Comparator)
for _, item := range another.Values() {
if set.Contains(item) {
result.Add(item)
}
}
return result
}

@ -474,6 +474,18 @@ func TestSetSerialization(t *testing.T) {
}
}
func TestSetIntersection(t *testing.T) {
set := NewWithStringComparator("a", "b", "c", "d")
anotherSet := NewWithStringComparator("c", "d", "f", "g")
intersection := set.Intersection(anotherSet)
if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if actualValue := set.Contains("c", "d"); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
}
func benchmarkContains(b *testing.B, set *Set, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {

Loading…
Cancel
Save