Skip to content

fix(set): correct IsDisjoint logic and optimize memory allocation - #64

Merged
zyedidia merged 1 commit into
zyedidia:masterfrom
mohitjoshi-hey:fix/set-isdisjoint
Oct 13, 2026
Merged

zyedidia merged 1 commit into
zyedidia:masterfrom
mohitjoshi-hey:fix/set-isdisjoint

Conversation

@mohitjoshi-hey

Copy link
Copy Markdown
Contributor

This PR fixes a logical bug in IsDisjoint and addresses the TODO: maybe optimize? comment.

The Bug:
Previously, IsDisjoint returned true if the intersection size was > 0, which is the mathematical opposite of disjoint (sharing zero elements).

The Optimization:
Instead of fixing it by changing the condition to == 0, I rewrote the method using s.Each(). The previous implementation called s.Intersection(other), which forced a full O(N) memory allocation to clone the set just to check for a single overlap. The new implementation checks for overlaps in-place, returning immediately with O(1) auxiliary space.

Changes:

  • Fixed IsDisjoint logic to return true only if zero elements are shared.
  • Rewrote the check to use s.Each() for zero-allocation performance.
  • Added ExampleSet_IsDisjoint in set_test.go to verify both true and false disjoint conditions while matching the existing testing style.

Testing:

  • go test ./set/... passes locally.
  • go fmt applied.
image

Closes #63

@zyedidia
zyedidia merged commit 7a2d437 into zyedidia:master Oct 13, 2026
1 check passed
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.

Bug: Method IsDisjoint return true if size of the intersection set > 0

2 participants