-
Notifications
You must be signed in to change notification settings - Fork 212
Slices: add Intersection function #50
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This PR (HEAD: f72bc6b) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/exp/+/458397 to see it. Tip: You can toggle comments from me using the |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Keith Randall: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
This PR (HEAD: 549e8bf) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/exp/+/458397 to see it. Tip: You can toggle comments from me using the |
Message from Olek Dudek: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Ian Lance Taylor: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Keith Randall: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Olek Dudek: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
Message from Ian Lance Taylor: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/458397. |
func Intersection[E comparable](s1, s2 []E) []E { | ||
var result []E | ||
s2len := len(s2) | ||
s1len := len(s1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not make sense. The built-in len() function is inlined, and instead of len(), the value of len from the slice structure is substituted.
el := s2[i] | ||
s2Map[el] = true | ||
} | ||
for i := 0; i < s1len; i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is necessary to create maps for each slice in order to avoid duplication due to duplicates in one of the slices.
} | ||
} | ||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a result, the implementation may look like this:
func Intersect[T comparable](a, b []T) []T {
if len(a) == 0 || len(b) == 0 {
return nil
}
base, other := a, b
if len(b) < len(a) {
base, other = b, a
}
// Base sequence -- unique elements of the smaller slice (preserve order)
intersection := Unique(base)
otherSet := HashSet(other)
writeIdx := 0
for _, v := range intersection {
if _, ok := otherSet[v]; ok {
intersection[writeIdx] = v
writeIdx++
}
}
intersection = intersection[:writeIdx]
if len(intersection) == 0 {
return nil
}
return intersection
}
// Stable function
func Unique[V comparable](slice []V) []V {
if len(slice) == 0 {
return nil
}
hashSet := make(map[V]struct{}, len(slice))
result := make([]V, 0, len(slice))
for _, elem := range slice {
if _, ok := hashSet[elem]; !ok {
result = append(result, elem)
hashSet[elem] = struct{}{}
}
}
return result
}
func HashSet[V comparable](slice []V) map[V]struct{} {
if len(slice) == 0 {
return nil
}
result := make(map[V]struct{}, len(slice))
for _, elem := range slice {
result[elem] = struct{}{}
}
return result
}
@@ -767,3 +767,80 @@ func BenchmarkReplace(b *testing.B) { | |||
} | |||
|
|||
} | |||
|
|||
func TestIntersection(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add a unit test for the case of duplicates within a single slice.
It is also customary to add benchmarks for functions of this type.
This function allow to compare two slices and return new slice without duplication