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

symmetric difference operation for sets via xor #24286

Merged
merged 2 commits into from
Oct 19, 2024
Merged

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Oct 11, 2024

closes nim-lang/RFCs#554

Adds a symmetric difference operation to the language bitset type. This maps to a simple xor operation on the backend and thus is likely faster than the current alternatives, namely (a - b) + (b - a) or a + b - a * b. The compiler VM implementation of bitsets already implemented this via symdiffSets but it was never used.

The standalone binary operation is added to setutils, named symmetricDifference in line with hash sets. An operator version -+- and an in-place version like toggle as described in the RFC are also added, implemented as trivial sugar.

@mrgaturus
Copy link

mrgaturus commented Oct 18, 2024

if you don't want add -+- and toggle to system, i think symmetricDifference should be in setutils instead and also add -+- and toggle to that module

@metagn
Copy link
Collaborator Author

metagn commented Oct 18, 2024

Either way is fine but: incl and excl with 2 set arguments are also simple sugar and still in the system module, and symmetricDifference is a compiler magic same as *, + etc above it; so they could all still go in system. I just figured this PR would be simpler if it just had symmetricDifference as the compiler magic. We can even use another name than -+- if we also add it to hash sets.

@Araq
Copy link
Member

Araq commented Oct 18, 2024

IIRC I removed the -+- operator before version 1.0 as nobody used it. Now apparently there is some demand for it, but it's so rare that it shouldn't be in system.

@metagn
Copy link
Collaborator Author

metagn commented Oct 18, 2024

Moved to setutils

@Araq Araq merged commit ae9287c into nim-lang:devel Oct 19, 2024
19 checks passed
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from ae9287c

Hint: mm: orc; opt: speed; options: -d:release
175245 lines; 8.679s; 655.348MiB peakmem

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.

introduce two xor operations for set[T]
3 participants