diff --git a/Sources/BitCollections/BitSet/BitSet+Extras.swift b/Sources/BitCollections/BitSet/BitSet+Extras.swift index 629afdba..838955e1 100644 --- a/Sources/BitCollections/BitSet/BitSet+Extras.swift +++ b/Sources/BitCollections/BitSet/BitSet+Extras.swift @@ -143,11 +143,26 @@ extension BitSet { /// /// - Complexity: Equivalent to two invocations of `index(after:)`. public subscript(members bounds: some RangeExpression) -> Slice { - let bounds = bounds.relative(to: Int.min ..< Int.max) + let bounds = bounds.relative(to: _AllIntegers()) return self[members: bounds] } } +/// All `Int` values as a collection, for resolving range expressions in +/// `BitSet[members:]`. Unlike `Int.min ..< Int.max`, stepping past `Int.max` +/// doesn't trap, so closed ranges like `0 ... Int.max` resolve to +/// `0 ..< Int.max`. `Int.max` can never be a member, so that loses nothing. +internal struct _AllIntegers: Collection { + internal var startIndex: Int { Int.min } + internal var endIndex: Int { Int.max } + + internal subscript(position: Int) -> Int { position } + + internal func index(after i: Int) -> Int { + i == Int.max ? Int.max : i + 1 + } +} + extension BitSet { /// Removes and returns the element at the specified position. /// diff --git a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift index ff287ee7..1c68b5a6 100644 --- a/Tests/BitCollectionsTests/BitSet.Counted Tests.swift +++ b/Tests/BitCollectionsTests/BitSet.Counted Tests.swift @@ -29,6 +29,13 @@ extension BitSet.Counted: SetAPIExtras { extension BitSet.Counted: SortedCollectionAPIChecker {} final class BitSetCountedTests: CollectionTestCase { + func test_member_range_subscript() { + let bits: BitSet.Counted = [2, 5, 6, 8, 9] + expectEqualElements(bits[members: 3 ..< 7], [5, 6]) + expectEqualElements(bits[members: ...Int.max], [2, 5, 6, 8, 9]) + expectEqualElements(bits[members: 6 ... Int.max], [6, 8, 9]) + } + func test_collection_strides() { withEvery("stride", in: [1, 2, 3, 5, 7, 8, 11, 13, 63, 79, 300]) { stride in withEvery("count", in: [0, 1, 2, 5, 10, 20, 25]) { count in diff --git a/Tests/BitCollectionsTests/BitSetTests.swift b/Tests/BitCollectionsTests/BitSetTests.swift index 9991e0a5..704d4997 100644 --- a/Tests/BitCollectionsTests/BitSetTests.swift +++ b/Tests/BitCollectionsTests/BitSetTests.swift @@ -446,6 +446,24 @@ final class BitSetTest: CollectionTestCase { expectEqualElements(h, []) expectEqual(h.startIndex, bits.endIndex) expectEqual(h.endIndex, bits.endIndex) + + // Closed ranges ending at `Int.max` + let i = bits[members: ...Int.max] + expectEqualElements(i, [2, 5, 6, 8, 9]) + expectEqual(i.startIndex, bits.startIndex) + expectEqual(i.endIndex, bits.endIndex) + + let j = bits[members: 6 ... Int.max] + expectEqualElements(j, [6, 8, 9]) + expectEqual(j.endIndex, bits.endIndex) + + let k = bits[members: Int.min ... Int.max] + expectEqualElements(k, [2, 5, 6, 8, 9]) + + let l = bits[members: Int.max ... Int.max] + expectEqualElements(l, []) + expectEqual(l.startIndex, bits.endIndex) + expectEqual(l.endIndex, bits.endIndex) } func test_member_range_subscript_exhaustive() {