From 84e65e8b5a90a41d7ee7f44c7ca8d709aab1a205 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <166978100+Denimworld12@users.noreply.github.com> Date: Fri, 25 Sep 2026 13:02:10 +0530 Subject: [PATCH] [BitCollections] Fix BitSet.Counted.update(with:) not maintaining the cached count BitSet.Counted caches its size in `_count` so that `count` is O(1). Every mutating operation is responsible for keeping that cache in sync with the underlying bit set. `update(with:)` forwarded straight to `BitSet.update(with:)` without adjusting `_count`. That method inserts the member when it isn't already present (it is defined as `insert(newMember).inserted ? nil : newMember`), so inserting a new member this way left the cached count one too low. The stale count is observable in a few ways: - `count` under-reports the number of members. - `isEmpty` is `_count == 0`, so a set that still contains members can report itself as empty. - The `Collection` conformance promises `count` elements but iteration yields more, which breaks the standard library. `Array(set)` traps with "Fatal error: invalid Collection: more than 'count' elements in collection". In builds with COLLECTIONS_INTERNAL_CHECKS this also violates `_checkInvariants()`, which asserts `_count == _bits.count`. Mirror what `insert(_:)` already does and bump `_count` when a new member was inserted. `update(with:)` returns nil exactly in that case. `BitSet` has had a `test_update` for this all along; `BitSet.Counted` did not, which is how this went unnoticed. Add the matching test, following the same combinatorial style and using `Set` as the oracle. --- .../BitSet/BitSet.Counted.swift | 4 +++- .../BitSet.Counted Tests.swift | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Sources/BitCollections/BitSet/BitSet.Counted.swift b/Sources/BitCollections/BitSet/BitSet.Counted.swift index d57aae8c..5016bc9b 100644 --- a/Sources/BitCollections/BitSet/BitSet.Counted.swift +++ b/Sources/BitCollections/BitSet/BitSet.Counted.swift @@ -338,7 +338,9 @@ extension BitSet.Counted: SetAlgebra { @discardableResult public mutating func update(with newMember: Int) -> Int? { - _bits.update(with: newMember) + let old = _bits.update(with: newMember) + if old == nil { _count += 1 } + return old } @discardableResult diff --git a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift index ff5216e5..4e9d36e3 100644 --- a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift +++ b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift @@ -237,6 +237,30 @@ final class BitSetCountedTests: CollectionTestCase { expectFalse(a.isStrictSuperset(of: Set(c))) } + func test_update() { + let count = 100 + withEvery("seed", in: 0 ..< 10) { seed in + var rng = RepeatableRandomNumberGenerator(seed: seed) + var actual: BitSet.Counted = [] + var expected: Set = [] + let input = (0 ..< count).shuffled(using: &rng) + withEvery("i", in: input.indices) { i in + let v = input[i] + // Inserting a new member. + expectEqual(actual.update(with: v), expected.update(with: v)) + expectEqual(actual.count, expected.count) + // Updating an existing member must not change the count. + expectEqual(actual.update(with: v), expected.update(with: v)) + expectEqual(actual.count, expected.count) + if i % 25 == 0 { + expectEqualElements(actual, expected.sorted()) + } + } + expectEqualElements(actual, expected.sorted()) + expectEqual(actual.count, expected.count) + } + } + func test_isDisjoint() { let a: BitSet.Counted = [1, 2, 3, 4]