`copyNodeAndPushItemIntoNewChild` takes an existing item out of `self` and
pushes it into `newChild`, which is then installed as a child of `self` at
`bucket`. `newChild` therefore sits one level below `self`, but the item was
inserted into it using `self`'s level:
let r = newChild.inserting(level, item, hash)
Navigating with the parent's level picks the wrong hash digit, so the item —
and the subtree it is merged with — end up filed under a path that doesn't
match their hash.
`TreeSet.union` reaches this when it merges an incoming collision node against
an item already occupying that bucket. The result is a set that still reports
the right `count` and still yields every element when iterated, but whose
misplaced members cannot be found by `contains`, since lookup navigates by
hash:
let a1 = RawCollider(1, "A")
let a2 = RawCollider(2, "B")
let c1 = RawCollider(5, "ACAD")
let c2 = RawCollider(6, "ACAD")
let u = TreeSet([a1, a2]).union(TreeSet([c1, c2]))
u.count // 4
Array(u) // [2#B, 1#A, 5#ACAD, 6#ACAD]
u.contains(c1) // false
u.contains(a1) // false
Building the same set directly with `TreeSet([a1, a2, c1, c2])` is unaffected;
only the structural merge paths are.
Insert at `level.descend()` instead.
Enumerating every pair of subsets of the shared `testItems` fixture, 171 of
4096 unions contained at least one member that `contains` could not find.
With this change all 4096 are clean.
Also fix the full invariant check, which could not run on any tree containing
a collision node. Collision nodes store their item count in `itemMap` and
`childMap` rather than a set of occupied buckets, so treating those bitmaps as
buckets walks items that aren't there — a node holding 5 items has
`itemMap._value == 5`, whose bits nominate buckets 0 and 2. The bucket loop now
only runs for regular nodes. Before this, `swift test
-Xswiftc -DCOLLECTIONS_INTERNAL_CHECKS` trapped immediately in
HashTreeCollections; that trap was also what kept the invariant check from
reporting the misplaced nodes above.
Add a regression test alongside the existing `test_union_collision_node_ordering`.
SetAPIChecker verifies that a set-like type has the right shape: it is
a protocol with no assertions, so conforming makes the compiler check
that the API surface exists. This adds the other half — that the
operations behave as documented.
Constrained on SetAPIChecker rather than SetAlgebra, because
OrderedSet deliberately does not conform to SetAlgebra (only its
UnorderedView does, for documented reasons about order-sensitive
equality). Constraining on the stdlib protocol would exclude the type
that needs this most.
Follows the house pattern of validating the specification before the
implementation. A Set<Element> model is only as sound as Element's
Hashable conformance, so _checkSetAlgebraModels runs _checkHashable
over the model elements and verifies each model survives a round trip
through Array before any instance is judged; it returns false and the
caller stops rather than reporting defects in the wrong artefact. A
broken model and a broken implementation can agree with each other,
which fails quietly in a way a wrong implementation does not.
Per-instance: contents and count against the model, isEmpty against
count independently of it, the empty set's identity and absorption
cases, the reflexive containment predicates, and filter at both
extremes and in between.
Per-pair: the four operations against the model and again through the
Sequence-taking overloads, which are separate entry points and can
diverge from the Self-taking ones; commutativity, both absorption
laws, and symmetricDifference against its definition in terms of the
other three; and the containment predicates against the model and
against each other. A type can implement each predicate plausibly and
still have them disagree.
Contents and count are asserted separately throughout: a type that
maintains a stored count can return the right elements alongside a
stale one.
Call sites for BitSet, OrderedSet and TreeSet.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- For CustomStringConvertible, `description` should print elements using `String(reflecting:)` rather than `String(describing:)`, to avoid element output from interfering with syntactically relevant punctuation within structured list displays.
- To make this work without making displays overly verbose, make `debugDescription` mostly the same as `description`, except for escaping raw string displays when and if needed.
- Avoid printing type names in `debugDescription` — it is generally not helpful, as the type is already easily accessible elsewhere. Including it makes for overly verbose displays when the type serves as an Element of a collection.
- Avoid having `debugDescription` return the empty string — it can be confusing when such a description appears in list displays.
Resolves#301.