Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ODudek
Copy link

@ODudek ODudek commented Dec 19, 2022

This function allow to compare two slices and return new slice without duplication

@google-cla
Copy link

google-cla bot commented Dec 19, 2022

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.

@gopherbot
Copy link
Contributor

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 comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

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 comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Olek Dudek:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ian Lance Taylor:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Olek Dudek:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ian Lance Taylor:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/458397.
After addressing review feedback, remember to publish your drafts!

func Intersection[E comparable](s1, s2 []E) []E {
var result []E
s2len := len(s2)
s1len := len(s1)

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++ {

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
}

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) {

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants