diff --git a/Sources/BitCollections/BitArray/BitArray+Shifts.swift b/Sources/BitCollections/BitArray/BitArray+Shifts.swift index fe748f5c..e1924257 100644 --- a/Sources/BitCollections/BitArray/BitArray+Shifts.swift +++ b/Sources/BitCollections/BitArray/BitArray+Shifts.swift @@ -11,6 +11,19 @@ // //===----------------------------------------------------------------------===// +extension BitArray { + /// Returns the magnitude of a negative shift amount. + /// + /// `Int.min` has no positive counterpart, so it is clamped to `Int.max`. + /// This only suits shifts that discard values: those treat every amount + /// that is at least `count` the same way. + @inline(__always) + internal static func _magnitude(ofNegativeShift amount: Int) -> Int { + assert(amount < 0) + return Int(clamping: amount.magnitude) + } +} + extension BitArray { /// Shift the bits in this array by the specified number of places to the /// left (towards the end of the array), by inserting `amount` false values @@ -31,7 +44,7 @@ extension BitArray { if amount > 0 { _resizingShiftLeft(by: amount) } else { - _resizingShiftRight(by: -amount) + _resizingShiftRight(by: Self._magnitude(ofNegativeShift: amount)) } } @@ -58,6 +71,8 @@ extension BitArray { if amount > 0 { _resizingShiftRight(by: amount) } else { + // This direction inserts values, so `Int.min` cannot be clamped: no + // array can hold as many values as it asks for, and the negation traps. _resizingShiftLeft(by: -amount) } } @@ -106,7 +121,8 @@ extension BitArray { if amount > 0 { unsafe $0._maskingShiftLeft(by: amount) } else { - unsafe $0._maskingShiftRight(by: -amount) + unsafe $0._maskingShiftRight( + by: BitArray._magnitude(ofNegativeShift: amount)) } } } @@ -135,7 +151,8 @@ extension BitArray { if amount > 0 { unsafe $0._maskingShiftRight(by: amount) } else { - unsafe $0._maskingShiftLeft(by: -amount) + unsafe $0._maskingShiftLeft( + by: BitArray._magnitude(ofNegativeShift: amount)) } } } diff --git a/Tests/BitCollectionsTests/BitArrayTests.swift b/Tests/BitCollectionsTests/BitArrayTests.swift index 001ed7fe..db151202 100644 --- a/Tests/BitCollectionsTests/BitArrayTests.swift +++ b/Tests/BitCollectionsTests/BitArrayTests.swift @@ -1149,4 +1149,86 @@ final class BitArrayTests: CollectionTestCase { } } + func test_maskingShiftLeft() { + withEvery( + "count", in: [0, 1, 2, 31, 32, 33, 63, 64, 65, 127, 128, 129, 200] + ) { count in + let bools = randomBoolArray(count: count) + withEvery("amount", in: (0 - count - 2) ... (count + 2)) { amount in + var a = BitArray(bools) + a.maskingShiftLeft(by: amount) + // Bit `i` comes from bit `i - amount`, or is cleared if there is none. + let expected = (0 ..< count).map { i -> Bool in + let source = i - amount + return source >= 0 && source < count && bools[source] + } + expectEqualElements(a, expected) + } + } + } + + func test_maskingShiftRight() { + withEvery( + "count", in: [0, 1, 2, 31, 32, 33, 63, 64, 65, 127, 128, 129, 200] + ) { count in + let bools = randomBoolArray(count: count) + withEvery("amount", in: (0 - count - 2) ... (count + 2)) { amount in + var a = BitArray(bools) + a.maskingShiftRight(by: amount) + // Bit `i` comes from bit `i + amount`, or is cleared if there is none. + let expected = (0 ..< count).map { i -> Bool in + let source = i + amount + return source >= 0 && source < count && bools[source] + } + expectEqualElements(a, expected) + } + } + } + + func test_maskingShift_docExamples() { + do { + var bits: BitArray = "1110110" + bits.maskingShiftLeft(by: 2) + expectEqual("\(bits)", "<1011000>") + bits.maskingShiftLeft(by: -4) + expectEqual("\(bits)", "<0000101>") + bits.maskingShiftLeft(by: 8) + expectEqual("\(bits)", "<0000000>") + } + do { + var bits: BitArray = "1110110" + bits.maskingShiftRight(by: 2) + expectEqual("\(bits)", "<0011101>") + bits.maskingShiftRight(by: -3) + expectEqual("\(bits)", "<1101000>") + bits.maskingShiftRight(by: 8) + expectEqual("\(bits)", "<0000000>") + } + } + + func test_shift_byExtremeAmounts() { + withEvery("count", in: [0, 1, 63, 64, 65, 200]) { count in + let bools = randomBoolArray(count: count) + // An amount whose magnitude is at least `count` discards every value, + // whichever way it shifts. `Int.min` is the one that can't be negated. + withEvery("amount", in: [Int.min, Int.min + 1, Int.max]) { amount in + var a = BitArray(bools) + a.maskingShiftLeft(by: amount) + expectEqualElements(a, repeatElement(false, count: count)) + + var b = BitArray(bools) + b.maskingShiftRight(by: amount) + expectEqualElements(b, repeatElement(false, count: count)) + } + withEvery("amount", in: [Int.min, Int.min + 1]) { amount in + var a = BitArray(bools) + a.resizingShiftLeft(by: amount) + expectEqual("\(a)", "<>") + } + var c = BitArray(bools) + c.resizingShiftRight(by: Int.max) + expectEqual("\(c)", "<>") + } + } + }