mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
Merge pull request #847 from sandeepvijayarao09/fix-bitset-members-int-max
[BitCollections] Don't trap on BitSet[members:] closed ranges ending at Int.max
This commit is contained in:
3 files changed
+41
-1
No files matched your search
@@ -143,11 +143,26 @@ extension BitSet {
|
||||
///
|
||||
/// - Complexity: Equivalent to two invocations of `index(after:)`.
|
||||
public subscript(members bounds: some RangeExpression<Int>) -> Slice<BitSet> {
|
||||
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.
|
||||
///
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user