mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
_resizingShiftRight(by:) copied the surviving suffix down to index 0 and then removed `count &- amount` trailing bits instead of `amount`, so it truncated the result to `amount` stale bits rather than keeping `count - amount` valid ones. resizingShiftLeft(by: -n) was affected via the same path, and the API had no test coverage at all. Also fix the doc examples, which demonstrated the resizing shifts with the masking shift function names (and, in one case, a result whose bit count contradicted the documented growth by `amount`). Add regression tests asserting parity with the documented semantics (dropFirst / prepending false values) across word-boundary counts, negative amounts, boundary `amount` values, and the doc examples verbatim.
165 lines
5.4 KiB
Swift
165 lines
5.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2022 - 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
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
|
|
/// at the beginning.
|
|
///
|
|
/// If `amount` is negative, this is equivalent to shifting `-amount`
|
|
/// places to the right.
|
|
///
|
|
/// var bits: BitArray = "1110110"
|
|
/// bits.resizingShiftLeft(by: 2)
|
|
/// // bits is now 111011000
|
|
/// bits.resizingShiftLeft(by: -4)
|
|
/// // bits is now 11101
|
|
/// bits.resizingShiftLeft(by: 8)
|
|
/// // bits is now 1110100000000
|
|
public mutating func resizingShiftLeft(by amount: Int) {
|
|
guard amount != 0 else { return }
|
|
if amount > 0 {
|
|
_resizingShiftLeft(by: amount)
|
|
} else {
|
|
_resizingShiftRight(by: -amount)
|
|
}
|
|
}
|
|
|
|
/// Shift the bits in this array by the specified number of places to the
|
|
/// right (towards the start of the array), by removing `amount` existing
|
|
/// values from the front of the array.
|
|
///
|
|
/// If `amount` is negative, then this is equivalent to shifting `-amount`
|
|
/// places to the left. If amount is greater than or equal to `count`,
|
|
/// then the resulting bit array will be empty.
|
|
///
|
|
/// var bits: BitArray = "1110110"
|
|
/// bits.resizingShiftRight(by: 2)
|
|
/// // bits is now 11101
|
|
/// bits.resizingShiftRight(by: -4)
|
|
/// // bits is now 111010000
|
|
/// bits.resizingShiftRight(by: 10)
|
|
/// // bits is now empty
|
|
///
|
|
/// If `amount` is between 0 and `count`, then this has the same effect as
|
|
/// `removeFirst(amount)`.
|
|
public mutating func resizingShiftRight(by amount: Int) {
|
|
guard amount != 0 else { return }
|
|
if amount > 0 {
|
|
_resizingShiftRight(by: amount)
|
|
} else {
|
|
_resizingShiftLeft(by: -amount)
|
|
}
|
|
}
|
|
|
|
internal mutating func _resizingShiftLeft(by amount: Int) {
|
|
assert(amount > 0)
|
|
_extend(by: amount, with: false)
|
|
maskingShiftLeft(by: amount)
|
|
}
|
|
|
|
internal mutating func _resizingShiftRight(by amount: Int) {
|
|
assert(amount > 0)
|
|
guard amount < count else {
|
|
self = .init()
|
|
return
|
|
}
|
|
self._copy(from: unsafe Range(uncheckedBounds: (amount, count)), to: 0)
|
|
self._removeLast(amount)
|
|
}
|
|
}
|
|
|
|
extension BitArray {
|
|
// FIXME: Add maskingShiftRight(by:in:) and maskingShiftLeft(by:in:) for shifting slices.
|
|
|
|
/// Shift the bits in this array by the specified number of places to the
|
|
/// left (towards the end of the array), without changing
|
|
/// its count.
|
|
///
|
|
/// Values that are shifted off the array are discarded. Values that are
|
|
/// shifted in are all set to false.
|
|
///
|
|
/// If `amount` is negative, this is equivalent to shifting `-amount`
|
|
/// places to the right. If `amount` is greater than or equal to `count`,
|
|
/// then all values are set to false.
|
|
///
|
|
/// var bits: BitArray = "1110110"
|
|
/// bits.maskingShiftLeft(by: 2)
|
|
/// // bits is now 1011000
|
|
/// bits.maskingShiftLeft(by: -4)
|
|
/// // bits is now 0000101
|
|
/// bits.maskingShiftLeft(by: 8)
|
|
/// // bits is now 0000000
|
|
public mutating func maskingShiftLeft(by amount: Int) {
|
|
guard amount != 0 else { return }
|
|
unsafe _update {
|
|
if amount > 0 {
|
|
unsafe $0._maskingShiftLeft(by: amount)
|
|
} else {
|
|
unsafe $0._maskingShiftRight(by: -amount)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Shift the bits in this array by the specified number of places to the
|
|
/// right (towards the beginning of the array), without changing
|
|
/// its count.
|
|
///
|
|
/// Values that are shifted off the array are discarded. Values that are
|
|
/// shifted in are all set to false.
|
|
///
|
|
/// If `amount` is negative, this is equivalent to shifting `-amount`
|
|
/// places to the left. If `amount` is greater than or equal to `count`,
|
|
/// then all values are set to false.
|
|
///
|
|
/// var bits: BitArray = "1110110"
|
|
/// bits.maskingShiftRight(by: 2)
|
|
/// // bits is now 0011101
|
|
/// bits.maskingShiftRight(by: -3)
|
|
/// // bits is now 1101000
|
|
/// bits.maskingShiftRight(by: 8)
|
|
/// // bits is now 0000000
|
|
public mutating func maskingShiftRight(by amount: Int) {
|
|
guard amount != 0 else { return }
|
|
unsafe _update {
|
|
if amount > 0 {
|
|
unsafe $0._maskingShiftRight(by: amount)
|
|
} else {
|
|
unsafe $0._maskingShiftLeft(by: -amount)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension BitArray._UnsafeHandle {
|
|
internal mutating func _maskingShiftLeft(by amount: Int) {
|
|
assert(amount > 0)
|
|
let d = Swift.min(amount, self.count)
|
|
if d == amount {
|
|
let range = unsafe Range(uncheckedBounds: (0, self.count &- d))
|
|
unsafe self.copy(from: range, to: d)
|
|
}
|
|
unsafe self.clear(in: Range(uncheckedBounds: (0, d)))
|
|
}
|
|
|
|
internal mutating func _maskingShiftRight(by amount: Int) {
|
|
assert(amount > 0)
|
|
let d = Swift.min(amount, self.count)
|
|
if d == amount {
|
|
let range = unsafe Range(uncheckedBounds: (d, self.count))
|
|
unsafe self.copy(from: range, to: 0)
|
|
}
|
|
unsafe self.clear(in: Range(uncheckedBounds: (self.count &- d, self.count)))
|
|
}
|
|
}
|