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

Difference operation support #4

Closed
sunny-repo88 opened this issue Jul 30, 2024 · 4 comments
Closed

Difference operation support #4

sunny-repo88 opened this issue Jul 30, 2024 · 4 comments

Comments

@sunny-repo88
Copy link

Hi,

Thanks for this cool idea project with bit manipulation and great performance. Do u plan to support difference operation. This is the solution i can think of without bit manipulation. This might be the slowest.
Any solution can be faster than this?

setA := nset.NewNSet[uint32]()

setA.Add(0)
setA.Add(300)
setA.Add(256)
setA.Add(4)

setB := nset.NewNSet[uint32]()
setB.Add(0)
setB.Add(4)

setA = setA.Difference(setB)
fmt.Println(setA.GetAllElements()) //[256 300]

Possible solution

func (n *NSet[T]) Difference(otherSet *NSet[T]) *NSet[T] {
	outSet := n
	filter := otherSet.GetAllElements()
	for _, x := range filter {
		if outSet.Contains(x) {
			outSet.Remove(x)
		}
	}
	return outSet
}
@bloeys
Copy link
Owner

bloeys commented Aug 1, 2024

Hey,

Glad you liked the project.

So I think by difference you kind of mean set1.NotIn(set2), basically saying: 'create a set containing elements in set1 but not in set2`.

Did I get that right?
This seems like a nice thing to have and definitely should be able to speed this by playing with bits and such.

I am busy this week but perhaps I'll have time in ~10 days or so.

Thanks

@sunny-repo88
Copy link
Author

Hi @bloeys , yes you are right.

@bloeys
Copy link
Owner

bloeys commented Sep 9, 2024

Hey @sunny-repo88

The function GetDifference has been added in this release https://github.com/bloeys/nset/releases/tag/v1.3.0 (Edit: Use v1.3.1 as v1.3.0 has a small bug)

Please check it out, and if all is good close the issue.

Hope nset proves useful :)

@sunny-repo88
Copy link
Author

@bloeys , this looks good. thanks for the effort

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

No branches or pull requests

2 participants