Skip to content

feat: SortedSet merge-based set algebra and Maxer/Minner fast paths #minor - #42

Merged
freeformz merged 7 commits into
mainfrom
sortedset-algebra-maxer-minner
Jul 13, 2026
Merged

feat: SortedSet merge-based set algebra and Maxer/Minner fast paths #minor#42
freeformz merged 7 commits into
mainfrom
sortedset-algebra-maxer-minner

Conversation

@freeformz

Copy link
Copy Markdown
Owner

Summary

Two additions from the optional-interface survey (the rest — Equaler/Disjointer/Subsetter, a bulk-AppendSeq hook, Locked delegation — are saved for follow-ups):

SortedSet implements the four set-algebra optimization interfaces

When both operands of the package-level Union/Intersection/Difference/SymmetricDifference are *SortedSet[M], the result is now computed by a single O(N+M) two-pointer merge of the two sorted backing slices (one shared mergeSorted helper; each operation is a flag combination of keep-only-in-a / keep-only-in-b / keep-in-both). This replaces fallbacks that were:

  • O(N·M) for Union and SymmetricDifference — every insert into the middle of the sorted slice shifts the tail
  • O(N log M) for Intersection and Difference — a binary search per element

Non-SortedSet operands are declined (false), keeping the generic algorithm in exactly one place, per the Unioner contract.

New optional Maxer/Minner interfaces

Same pattern as the set-algebra interfaces: package-level Max/Min check their argument and fall back to the generic O(N) iteration when the method reports false. Implemented by both always-sorted types:

  • SortedSet: O(1) from the ends of the sorted slice
  • BitSet: outermost set bit scanning from the span edges — O(1) unless Removes have left zero edge words (Remove never trims)

Testing

  • TestSortedSet_MergeOps: rapid differential test of all four ops against Map-based generic results — asserts the fast path returns a sorted *SortedSet[int], mixed operands fall back correctly, and non-SortedSet operands are declined
  • TestSortedSet_MaxMin / TestBitSet_MaxMin: direct method + package-level fast-path checks, covering empty sets, Removes leaving zero words at BitSet span edges, and sign-bit mapping extremes (MaxUint64, MinInt64)
  • TestMinMaxOptionalInterfaces: third-party-style stub proving Max/Min honor external implementations and fall back on decline
  • The existing rapid state machine exercises the new merge paths automatically (it builds its second operand via NewEmpty(), so both sides are the same concrete type)
  • go vet, staticcheck, and go test -race ./... all clean

🤖 Generated with Claude Code

https://claude.ai/code/session_01MVcvyLahh4ssVbaXJJA8dV

…minor

SortedSet now implements the optional Unioner, Intersectioner, Differencer,
and SymmetricDifferencer interfaces: when both operands are SortedSets, the
package-level set-algebra functions compute their result with a single
O(N+M) linear merge of the two sorted backing slices, replacing fallbacks
that were O(N*M) for Union and SymmetricDifference (each insert into the
middle of the sorted slice shifts the tail) and O(N log M) for
Intersection and Difference.

Add Maxer and Minner, new optional interfaces following the same contract
as the set-algebra ones: the package-level Max and Min check them and fall
back to the generic O(N) iteration when the method reports false. SortedSet
answers in O(1) from the ends of its slice; BitSet scans from its span
edges for the outermost set bit.

Tests mirror the existing BitSet patterns: a rapid differential test of the
merge ops against Map-based generic results (including mixed-operand
fallback and decline reporting), direct Max/Min tests covering empty sets,
Removes that leave zero words at BitSet span edges, and the sign-bit
mapping extremes, plus a third-party-style stub proving Max/Min honor
external implementations.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MVcvyLahh4ssVbaXJJA8dV
Copilot AI review requested due to automatic review settings July 7, 2026 04:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds optional-interface fast paths for set algebra and extreme-element queries, improving performance for SortedSet/BitSet when using the package-level helpers while preserving the generic fallback algorithms in set.go.

Changes:

  • Implemented merge-based Union/Intersection/Difference/SymmetricDifference fast paths for SortedSet via optional set-algebra interfaces.
  • Added optional Maxer/Minner interfaces and integrated them into package-level Max/Min, with SortedSet and BitSet implementations.
  • Added differential and direct tests covering merge operations and Max/Min fast paths, plus documentation updates.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sorted.go Adds Max/Min and merge-based set-algebra optional interface implementations for SortedSet.
sorted_test.go Adds rapid differential tests for merge-based ops and direct tests for SortedSet Max/Min.
set.go Introduces Maxer/Minner and uses them in package-level Max/Min.
README.MD Documents SortedSet set-algebra optimization and new Max/Min optional interfaces.
coverage_test.go Adds a third-party-style stub to verify Max/Min honor optional interfaces and fall back correctly.
CLAUDE.md Updates repository documentation to reflect new SortedSet and BitSet optional-interface capabilities.
bitset.go Implements Maxer/Minner for BitSet.
bitset_test.go Adds tests for BitSet Max/Min behavior including edge cases and package-level fast-path usage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread set.go
Comment thread set.go
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 7, 2026 16:32
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comment thread set.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 7, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Comment thread bitset.go
Comment thread bitset.go
Comment thread sorted.go Outdated
… mergeSorted prealloc

- BitSet.Max/Min return early when the set is empty (card == 0), avoiding a
  scan of a retained-but-empty word span; also nil-receiver safe like Cardinality
- mergeSorted only applies the intersection capacity bound when the flags are
  actually the intersection combination, so the all-false combination no longer
  over-allocates
- drop unnecessary explicit type arguments in TestMinMaxOptionalInterfaces

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLrWSEKLMPuf1kq1KDCoxs
Copilot AI review requested due to automatic review settings July 13, 2026 18:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Comment thread sorted.go
Comment thread sorted.go
A typed-nil *SortedSet passed as Set[M] previously reached the nil-guarded
Cardinality() in the package-level Max/Min and panicked with the intended
"empty set" message; the new Maxer/Minner fast path dereferenced s.el first
and crashed with a raw nil-pointer panic. Guard the receiver like BitSet does.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLrWSEKLMPuf1kq1KDCoxs
Copilot AI review requested due to automatic review settings July 13, 2026 18:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Regression test for the typed-nil SortedSet/BitSet Max/Min panics caught in
review — a nil receiver must report ok=false, not dereference backing storage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RLrWSEKLMPuf1kq1KDCoxs
Copilot AI review requested due to automatic review settings July 13, 2026 19:09
@claude

claude Bot commented Jul 13, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@freeformz
freeformz merged commit 8ec8e7a into main Jul 13, 2026
11 checks passed
@freeformz
freeformz deleted the sortedset-algebra-maxer-minner branch July 13, 2026 19:16
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.

2 participants