remove(at:) recursed into the child holding the offset and returned
without balancing it, so repeated removals could empty a leaf. Iterating
then read a stale slot, which returned already-removed elements and read
released references in release builds, and trapped in debug builds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run the member identity checker against SortedSet, including a
multi-level tree. With #761's fix reverted, it fails with the wrong
member stored by insert(_:) and update(with:), as reported in #760.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SortedSet declares SetAPIChecker since #746, but was never run through
checkSetAlgebra. The models are the same as TreeSet's and OrderedSet's,
plus three larger ones so that the sets span more than one B-tree level
in debug builds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SortedSet's set operations only accepted another SortedSet, so
SortedSet([1, 2]).union(0 ..< 10) did not compile and the type could
not conform to SetAPIChecker.
Add the customary Sequence generalizations for all 13 operations,
keeping the existing Self overloads as the fast path (concrete beats
generic in overload resolution). Arbitrary sequences fall back through
a temporary SortedSet or, for the membership predicates, allSatisfy.
Fixes#743.
Signed-off-by: Aniket <aniketkomailkro@gmail.com>
SortedSet.SubSequence has its own copy of SortedSet's == and
hash(into:), and neither was tested. Equal slices are taken from
bulk-built and inserted sets, so they start at different leaf offsets;
the last two classes match the evens slice in count and endpoints and
differ only past the first leaf.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SortedSet conforms to Hashable but was never run through checkHashable.
Multi-level sets are built both in bulk and by insertion, so equal sets
with different tree layouts must compare and hash equal. The last two
classes match the evens in count and endpoints and differ only past the
first leaf.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SortedSet conforms to BidirectionalCollection but was never run through
checkBidirectionalCollection. The counts sit on either side of the sizes
where a debug-build tree gains a level, and each is checked both
bulk-built and built by insertion, since the two give differently shaped
trees.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
The Range<Element> subscript covers half-open intervals. Add four
concrete overloads so callers can also write set[lo...hi], set[lo...],
set[..<hi], and set[...hi] — covering every stdlib RangeExpression
type that makes sense for a sorted set.
A single generic subscript<R: RangeExpression> would have been nicer,
but RangeExpression only exposes bounds via relative(to:), which needs
a Collection with Index == Bound. Element is Comparable but not an
index of any collection we have, so the generic path doesn't work for
arbitrary Element types. Five overloads it is.
The inclusive-upper variants (ClosedRange, PartialRangeThrough) use a
private _firstTreeIndex(after:) helper that returns the first
_BTree.Index strictly greater than a given element (or endIndex) —
the same logic as the public firstIndex(after:), but returning a
_Tree.Index directly for internal use.
Tests probe every lo/hi pair using a set of even numbers, so each
query exercises both "hits a member" and "falls between members"
cases, plus bounds that extend past each end of the set.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The Range<Element> subscript covers half-open intervals, and with
partial-range support it would compose into ceiling/floor/lower (first
>= x, last <= x, last < x). The strict upper bound "first element > x"
(Java's higher(_:)) can't be expressed as a range for arbitrary
Comparable types because Comparable has no successor operation, so
expose it — together with the symmetric strict lower bound — as
first-class operations.
Both are built on _BTree.startIndex(forKey:), which already returns the
first element >= the given key:
- firstIndex(after: x) advances past that index if the element there
equals x.
- lastIndex(before: x) steps back one index, or returns nil if the
lookup landed at startIndex.
Add test_firstIndexAfter and test_lastIndexBefore, covering queries
that hit an element, queries that fall between elements, and out-of-
range queries on both ends.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When the key was found at slot k of an internal node but an earlier
occurrence was sought in the child subtree and not found, the fallback
path returned the internal-node element's position. The absolute offset
formula for element[k] in a non-leaf node is:
ancestors + sum(subtreeCount[0..k]) + k
The `+ k` (slot) term was omitted, so the returned index carried the
wrong offset for any key that lives at slot > 0 of an internal node.
Add test_startIndexForKey (BTree Tests) to verify the offset for every
key in trees of several sizes, and test_rangeSubscript (SortedSet Tests)
to exercise the SortedSet[range] subscript which depends on this method.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This re-enables the SortedCollections for easi(er) experimentation.
The module remains source unstable — it may go away entirely or it may receive arbitrary source-breaking changes in any new swift-collections release, with no special pre-announcement.
Key Changes:
- Add tests and benchmarks for SortedSet
- Fix bug during _Node.UnsafeHandle.popFirstElement() rebalancing
- Enable _Node.UnsafeHandle.subscript(elementAt:) to work on value-less nodes.
Minor Changes:
- Optimize Codable to use _BTree.Builder and forEach
- Codable now checks for order, not strictly uniqueness
- Add smaller node size for debug builds to easily catch errors