mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
Sources: Add unsafe keywords where needed
This commit is contained in:
301 files changed
+6051
-5498
No files matched your search
@@ -157,14 +157,14 @@ extension BitArray {
|
||||
|
||||
extension BitArray {
|
||||
public mutating func toggleAll() {
|
||||
_update { handle in
|
||||
let w = handle._mutableWords
|
||||
for i in 0 ..< handle._words.count {
|
||||
w[i].formComplement()
|
||||
unsafe _update { handle in
|
||||
let w = unsafe handle._mutableWords
|
||||
for i in unsafe 0 ..< handle._words.count {
|
||||
unsafe w[i].formComplement()
|
||||
}
|
||||
let p = handle.end
|
||||
if p.bit > 0 {
|
||||
w[p.word].subtract(_Word(upTo: p.bit).complement())
|
||||
unsafe w[p.word].subtract(_Word(upTo: p.bit).complement())
|
||||
}
|
||||
}
|
||||
_checkInvariants()
|
||||
@@ -173,22 +173,22 @@ extension BitArray {
|
||||
public mutating func toggleAll(in range: Range<Int>) {
|
||||
precondition(range.upperBound <= count, "Range out of bounds")
|
||||
guard !range.isEmpty else { return }
|
||||
_update { handle in
|
||||
let words = handle._mutableWords
|
||||
unsafe _update { handle in
|
||||
let words = unsafe handle._mutableWords
|
||||
let start = _BitPosition(range.lowerBound)
|
||||
let end = _BitPosition(range.upperBound)
|
||||
if start.word == end.word {
|
||||
let bits = _Word(from: start.bit, to: end.bit)
|
||||
words[start.word].formSymmetricDifference(bits)
|
||||
unsafe words[start.word].formSymmetricDifference(bits)
|
||||
return
|
||||
}
|
||||
words[start.word].formSymmetricDifference(
|
||||
unsafe words[start.word].formSymmetricDifference(
|
||||
_Word(upTo: start.bit).complement())
|
||||
for i in stride(from: start.word + 1, to: end.word, by: 1) {
|
||||
words[i].formComplement()
|
||||
unsafe words[i].formComplement()
|
||||
}
|
||||
if end.bit > 0 {
|
||||
words[end.word].formSymmetricDifference(_Word(upTo: end.bit))
|
||||
unsafe words[end.word].formSymmetricDifference(_Word(upTo: end.bit))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,14 @@
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
@unsafe
|
||||
internal struct _ChunkedBitsForwardIterator {
|
||||
internal typealias _BitPosition = _UnsafeBitSet.Index
|
||||
|
||||
internal let words: UnsafeBufferPointer<_Word>
|
||||
@safe
|
||||
internal let end: _BitPosition
|
||||
@safe
|
||||
internal var position: _BitPosition
|
||||
|
||||
internal init(
|
||||
@@ -28,7 +31,7 @@ internal struct _ChunkedBitsForwardIterator {
|
||||
) {
|
||||
assert(range.lowerBound >= 0)
|
||||
assert(range.upperBound <= words.count * _Word.capacity)
|
||||
self.words = words
|
||||
unsafe self.words = words
|
||||
self.end = _BitPosition(range.upperBound)
|
||||
self.position = _BitPosition(range.lowerBound)
|
||||
}
|
||||
@@ -38,7 +41,7 @@ internal struct _ChunkedBitsForwardIterator {
|
||||
let (w, b) = position.split
|
||||
if w == end.word {
|
||||
position = end
|
||||
return (
|
||||
return unsafe (
|
||||
bits: words[w]
|
||||
.intersection(_Word(upTo: end.bit))
|
||||
.shiftedDown(by: b),
|
||||
@@ -46,15 +49,18 @@ internal struct _ChunkedBitsForwardIterator {
|
||||
}
|
||||
let c = _Word._capacity - b
|
||||
position.value += c
|
||||
return (bits: words[w].shiftedDown(by: b), count: c)
|
||||
return unsafe (bits: words[w].shiftedDown(by: b), count: c)
|
||||
}
|
||||
}
|
||||
|
||||
@unsafe
|
||||
internal struct _ChunkedBitsBackwardIterator {
|
||||
internal typealias _BitPosition = _UnsafeBitSet.Index
|
||||
|
||||
internal let words: UnsafeBufferPointer<_Word>
|
||||
@safe
|
||||
internal let start: _BitPosition
|
||||
@safe
|
||||
internal var position: _BitPosition
|
||||
|
||||
internal init(
|
||||
@@ -63,7 +69,7 @@ internal struct _ChunkedBitsBackwardIterator {
|
||||
) {
|
||||
assert(range.lowerBound >= 0)
|
||||
assert(range.upperBound <= words.count * _Word.capacity)
|
||||
self.words = words
|
||||
unsafe self.words = words
|
||||
self.start = _BitPosition(range.lowerBound)
|
||||
self.position = _BitPosition(range.upperBound)
|
||||
}
|
||||
@@ -73,7 +79,7 @@ internal struct _ChunkedBitsBackwardIterator {
|
||||
let (w, b) = position.endSplit
|
||||
if w == start.word {
|
||||
position = start
|
||||
return (
|
||||
return unsafe (
|
||||
bits: words[w]
|
||||
.intersection(_Word(upTo: b))
|
||||
.shiftedDown(by: start.bit),
|
||||
@@ -81,7 +87,7 @@ internal struct _ChunkedBitsBackwardIterator {
|
||||
}
|
||||
let c = b
|
||||
position.value -= c
|
||||
return (bits: words[w].intersection(_Word(upTo: b)), count: c)
|
||||
return unsafe (bits: words[w].intersection(_Word(upTo: b)), count: c)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -176,14 +176,14 @@ extension BitArray: RandomAccessCollection, MutableCollection {
|
||||
public subscript(position: Int) -> Bool {
|
||||
get {
|
||||
precondition(position >= 0 && position < _count, "Index out of bounds")
|
||||
return _read { handle in
|
||||
handle[position]
|
||||
return unsafe _read { handle in
|
||||
unsafe handle[position]
|
||||
}
|
||||
}
|
||||
set {
|
||||
precondition(position >= 0 && position < _count, "Index out of bounds")
|
||||
return _update { handle in
|
||||
handle[position] = newValue
|
||||
return unsafe _update { handle in
|
||||
unsafe handle[position] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ extension BitArray {
|
||||
from range: Range<Int>,
|
||||
to target: Int
|
||||
) {
|
||||
_update { handle in
|
||||
handle.copy(from: range, to: target)
|
||||
unsafe _update { handle in
|
||||
unsafe handle.copy(from: range, to: target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ extension BitArray {
|
||||
in source: UnsafeBufferPointer<_Word>,
|
||||
to target: Int
|
||||
) {
|
||||
_update { handle in
|
||||
handle.copy(from: range, in: source, to: target)
|
||||
unsafe _update { handle in
|
||||
unsafe handle.copy(from: range, in: source, to: target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ extension BitArray {
|
||||
) {
|
||||
let range = source._bounds
|
||||
source.base._storage.withUnsafeBufferPointer { words in
|
||||
self._copy(from: range, in: words, to: target)
|
||||
unsafe self._copy(from: range, in: words, to: target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ extension BitArray {
|
||||
from source: S,
|
||||
to range: Range<Int>
|
||||
) where S.Element == Bool {
|
||||
_update { $0.copy(from: source, to: range) }
|
||||
unsafe _update { unsafe $0.copy(from: source, to: range) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,21 +66,21 @@ extension BitArray._UnsafeHandle {
|
||||
) {
|
||||
assert(count <= _Word.capacity)
|
||||
assert(count == _Word.capacity || bits.shiftedDown(by: count).isEmpty)
|
||||
assert(target.value + count <= _count)
|
||||
assert(target.value + count <= self.count)
|
||||
let start = target.split
|
||||
let end = _BitPosition(target.value + count).endSplit
|
||||
let words = _mutableWords
|
||||
let words = unsafe _mutableWords
|
||||
if start.word == end.word {
|
||||
let mask = _Word(from: start.bit, to: end.bit)
|
||||
words[start.word].formIntersection(mask.complement())
|
||||
words[start.word].formUnion(bits.shiftedUp(by: start.bit))
|
||||
unsafe words[start.word].formIntersection(mask.complement())
|
||||
unsafe words[start.word].formUnion(bits.shiftedUp(by: start.bit))
|
||||
return
|
||||
}
|
||||
assert(start.word + 1 == end.word)
|
||||
words[start.word].formIntersection(_Word(upTo: start.bit))
|
||||
words[start.word].formUnion(bits.shiftedUp(by: start.bit))
|
||||
words[end.word].formIntersection(_Word(upTo: end.bit).complement())
|
||||
words[end.word].formUnion(
|
||||
unsafe words[start.word].formIntersection(_Word(upTo: start.bit))
|
||||
unsafe words[start.word].formUnion(bits.shiftedUp(by: start.bit))
|
||||
unsafe words[end.word].formIntersection(_Word(upTo: end.bit).complement())
|
||||
unsafe words[end.word].formUnion(
|
||||
bits.shiftedDown(by: _Word._capacity &- start.bit))
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ extension BitArray._UnsafeHandle {
|
||||
assert(
|
||||
range.lowerBound >= 0 && range.upperBound <= self.count,
|
||||
"Source range out of bounds")
|
||||
copy(from: range, in: _words, to: target)
|
||||
unsafe copy(from: range, in: _words, to: target)
|
||||
}
|
||||
|
||||
internal mutating func copy(
|
||||
@@ -114,17 +114,17 @@ extension BitArray._UnsafeHandle {
|
||||
let upperSource = _BitPosition(range.upperBound).endSplit
|
||||
|
||||
|
||||
let targetPtr = _words._ptr(at: target.word)
|
||||
let lowerSourcePtr = source._ptr(at: lowerSource.word)
|
||||
let upperSourcePtr = source._ptr(at: upperSource.word)
|
||||
let targetPtr = unsafe _words._ptr(at: target.word)
|
||||
let lowerSourcePtr = unsafe source._ptr(at: lowerSource.word)
|
||||
let upperSourcePtr = unsafe source._ptr(at: upperSource.word)
|
||||
|
||||
if targetPtr < lowerSourcePtr || targetPtr > upperSourcePtr {
|
||||
if unsafe targetPtr < lowerSourcePtr || targetPtr > upperSourcePtr {
|
||||
return true
|
||||
}
|
||||
if targetPtr == lowerSourcePtr, target.bit < lowerSource.bit {
|
||||
if unsafe targetPtr == lowerSourcePtr, target.bit < lowerSource.bit {
|
||||
return true
|
||||
}
|
||||
if targetPtr == upperSourcePtr, target.bit >= upperSource.bit {
|
||||
if unsafe targetPtr == upperSourcePtr, target.bit >= upperSource.bit {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -132,20 +132,20 @@ extension BitArray._UnsafeHandle {
|
||||
|
||||
if goForward() {
|
||||
// Copy forward from a disjoint or following overlapping range.
|
||||
var src = _ChunkedBitsForwardIterator(words: source, range: range)
|
||||
var src = unsafe _ChunkedBitsForwardIterator(words: source, range: range)
|
||||
var dst = _BitPosition(target)
|
||||
while let (bits, count) = src.next() {
|
||||
_copy(bits: bits, count: count, to: dst)
|
||||
while let (bits, count) = unsafe src.next() {
|
||||
unsafe _copy(bits: bits, count: count, to: dst)
|
||||
dst.value += count
|
||||
}
|
||||
assert(dst.value == target + range.count)
|
||||
} else {
|
||||
// Copy backward from a non-following overlapping range.
|
||||
var src = _ChunkedBitsBackwardIterator(words: source, range: range)
|
||||
var src = unsafe _ChunkedBitsBackwardIterator(words: source, range: range)
|
||||
var dst = _BitPosition(target + range.count)
|
||||
while let (bits, count) = src.next() {
|
||||
while let (bits, count) = unsafe src.next() {
|
||||
dst.value -= count
|
||||
_copy(bits: bits, count: count, to: dst)
|
||||
unsafe _copy(bits: bits, count: count, to: dst)
|
||||
}
|
||||
assert(dst.value == target)
|
||||
}
|
||||
@@ -163,14 +163,14 @@ extension BitArray._UnsafeHandle {
|
||||
if pos.bit > 0 {
|
||||
let (bits, count) = it._nextChunk(
|
||||
maxCount: _Word._capacity - pos.bit)
|
||||
_copy(bits: bits, count: count, to: pos)
|
||||
unsafe _copy(bits: bits, count: count, to: pos)
|
||||
pos.value += count
|
||||
}
|
||||
while true {
|
||||
let (bits, count) = it._nextChunk()
|
||||
guard count > 0 else { break }
|
||||
assert(pos.bit == 0)
|
||||
_copy(bits: bits, count: count, to: pos)
|
||||
unsafe _copy(bits: bits, count: count, to: pos)
|
||||
pos.value += count
|
||||
}
|
||||
precondition(pos.value == range.upperBound)
|
||||
|
||||
@@ -66,14 +66,14 @@ extension BitArray {
|
||||
var result: String
|
||||
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
|
||||
result = String(unsafeUninitializedCapacity: self.count + 2) { target in
|
||||
target.initializeElement(at: count + 1, to: ._asciiGT)
|
||||
unsafe target.initializeElement(at: count + 1, to: ._asciiGT)
|
||||
var i = count
|
||||
for v in self {
|
||||
target.initializeElement(at: i, to: v ? ._ascii1 : ._ascii0)
|
||||
unsafe target.initializeElement(at: i, to: v ? ._ascii1 : ._ascii0)
|
||||
i &-= 1
|
||||
}
|
||||
assert(i == 0)
|
||||
target.initializeElement(at: 0, to: ._asciiLT)
|
||||
unsafe target.initializeElement(at: 0, to: ._asciiLT)
|
||||
return count + 2
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -16,7 +16,7 @@ extension BitArray {
|
||||
///
|
||||
/// - Parameter value: The Boolean value to which to set the array's elements.
|
||||
public mutating func fill(with value: Bool = true) {
|
||||
fill(in: Range(uncheckedBounds: (0, count)), with: value)
|
||||
fill(in: unsafe Range(uncheckedBounds: (0, count)), with: value)
|
||||
}
|
||||
|
||||
/// Set every bit of this array within the specified range to `value`
|
||||
@@ -25,11 +25,11 @@ extension BitArray {
|
||||
/// - Parameter range: The range whose elements to overwrite.
|
||||
/// - Parameter value: The Boolean value to which to set the array's elements.
|
||||
public mutating func fill(in range: Range<Int>, with value: Bool = true) {
|
||||
_update { handle in
|
||||
unsafe _update { handle in
|
||||
if value {
|
||||
handle.fill(in: range)
|
||||
unsafe handle.fill(in: range)
|
||||
} else {
|
||||
handle.clear(in: range)
|
||||
unsafe handle.clear(in: range)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,13 +75,13 @@ extension BitArray {
|
||||
isSigned: Bool,
|
||||
body: (Int, UInt) -> Bool
|
||||
) -> Bool {
|
||||
self._read {
|
||||
guard $0._words.count > 0 else { return false }
|
||||
unsafe self._read {
|
||||
guard unsafe $0._words.count > 0 else { return false }
|
||||
|
||||
var isNegative = false
|
||||
let end = $0.end.endSplit
|
||||
assert(end.bit > 0)
|
||||
let last = $0._words[end.word]
|
||||
let last = unsafe $0._words[end.word]
|
||||
if isSigned, last.contains(end.bit - 1) {
|
||||
// Sign extend last word
|
||||
isNegative = true
|
||||
@@ -92,7 +92,7 @@ extension BitArray {
|
||||
return isNegative
|
||||
}
|
||||
for i in stride(from: end.word - 1, through: 0, by: -1) {
|
||||
if !body(i, $0._words[i].value) { return isNegative }
|
||||
if unsafe !body(i, $0._words[i].value) { return isNegative }
|
||||
}
|
||||
return isNegative
|
||||
}
|
||||
|
||||
@@ -38,8 +38,8 @@ extension BitArray: LosslessStringConvertible {
|
||||
if digits.utf8.first == ._asciiLT, digits.utf8.last == ._asciiGT {
|
||||
digits = digits.dropFirst().dropLast()
|
||||
}
|
||||
let bits: BitArray? = digits.utf8.withContiguousStorageIfAvailable { buffer in
|
||||
Self(_utf8Digits: buffer)
|
||||
let bits: BitArray? = unsafe digits.utf8.withContiguousStorageIfAvailable { buffer in
|
||||
unsafe Self(_utf8Digits: buffer)
|
||||
} ?? Self(_utf8Digits: description.utf8)
|
||||
guard let bits = bits else {
|
||||
return nil
|
||||
@@ -51,10 +51,10 @@ extension BitArray: LosslessStringConvertible {
|
||||
let c = utf8.count
|
||||
self.init(repeating: false, count: c)
|
||||
var i = c &- 1
|
||||
let success = _update { handle in
|
||||
let success = unsafe _update { handle in
|
||||
for byte in utf8 {
|
||||
if byte == ._ascii1 {
|
||||
handle.set(at: i)
|
||||
unsafe handle.set(at: i)
|
||||
} else {
|
||||
guard byte == ._ascii0 else { return false }
|
||||
}
|
||||
|
||||
@@ -251,8 +251,8 @@ extension BitArray {
|
||||
}
|
||||
_count += 1
|
||||
if newElement {
|
||||
_update { handle in
|
||||
handle._mutableWords[word].value |= 1 &<< bit
|
||||
unsafe _update { handle in
|
||||
unsafe handle._mutableWords[word].value |= 1 &<< bit
|
||||
}
|
||||
}
|
||||
_checkInvariants()
|
||||
@@ -288,7 +288,7 @@ extension BitArray {
|
||||
maxCount: _Word._capacity - pos.bit)
|
||||
guard count > 0 else { return }
|
||||
_count += count
|
||||
_update { $0._copy(bits: bits, count: count, to: pos) }
|
||||
unsafe _update { unsafe $0._copy(bits: bits, count: count, to: pos) }
|
||||
pos.value += count
|
||||
}
|
||||
while true {
|
||||
@@ -297,7 +297,7 @@ extension BitArray {
|
||||
assert(pos.bit == 0)
|
||||
_storage.append(.empty)
|
||||
_count += count
|
||||
_update { $0._copy(bits: bits, count: count, to: pos) }
|
||||
unsafe _update { unsafe $0._copy(bits: bits, count: count, to: pos) }
|
||||
pos.value += count
|
||||
}
|
||||
_checkInvariants()
|
||||
@@ -363,9 +363,9 @@ extension BitArray {
|
||||
}
|
||||
let c = count
|
||||
_count += 1
|
||||
_update { handle in
|
||||
handle.copy(from: i ..< c, to: i + 1)
|
||||
handle[i] = newElement
|
||||
unsafe _update { handle in
|
||||
unsafe handle.copy(from: i ..< c, to: i + 1)
|
||||
unsafe handle[i] = newElement
|
||||
}
|
||||
_checkInvariants()
|
||||
}
|
||||
@@ -484,7 +484,7 @@ extension BitArray {
|
||||
range.lowerBound >= 0 && range.upperBound <= count,
|
||||
"Bounds out of range")
|
||||
_copy(
|
||||
from: Range(uncheckedBounds: (range.upperBound, count)),
|
||||
from: unsafe Range(uncheckedBounds: (range.upperBound, count)),
|
||||
to: range.lowerBound)
|
||||
_removeLast(range.count)
|
||||
_checkInvariants()
|
||||
|
||||
@@ -74,7 +74,7 @@ extension BitArray {
|
||||
self = .init()
|
||||
return
|
||||
}
|
||||
self._copy(from: Range(uncheckedBounds: (amount, count)), to: 0)
|
||||
self._copy(from: unsafe Range(uncheckedBounds: (amount, count)), to: 0)
|
||||
self._removeLast(count &- amount)
|
||||
}
|
||||
}
|
||||
@@ -102,11 +102,11 @@ extension BitArray {
|
||||
/// // bits is now 0000000
|
||||
public mutating func maskingShiftLeft(by amount: Int) {
|
||||
guard amount != 0 else { return }
|
||||
_update {
|
||||
unsafe _update {
|
||||
if amount > 0 {
|
||||
$0._maskingShiftLeft(by: amount)
|
||||
unsafe $0._maskingShiftLeft(by: amount)
|
||||
} else {
|
||||
$0._maskingShiftRight(by: -amount)
|
||||
unsafe $0._maskingShiftRight(by: -amount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,11 +131,11 @@ extension BitArray {
|
||||
/// // bits is now 0000000
|
||||
public mutating func maskingShiftRight(by amount: Int) {
|
||||
guard amount != 0 else { return }
|
||||
_update {
|
||||
unsafe _update {
|
||||
if amount > 0 {
|
||||
$0._maskingShiftRight(by: amount)
|
||||
unsafe $0._maskingShiftRight(by: amount)
|
||||
} else {
|
||||
$0._maskingShiftLeft(by: -amount)
|
||||
unsafe $0._maskingShiftLeft(by: -amount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,19 +146,19 @@ extension BitArray._UnsafeHandle {
|
||||
assert(amount > 0)
|
||||
let d = Swift.min(amount, self.count)
|
||||
if d == amount {
|
||||
let range = Range(uncheckedBounds: (0, self.count &- d))
|
||||
self.copy(from: range, to: d)
|
||||
let range = unsafe Range(uncheckedBounds: (0, self.count &- d))
|
||||
unsafe self.copy(from: range, to: d)
|
||||
}
|
||||
self.clear(in: Range(uncheckedBounds: (0, 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 = Range(uncheckedBounds: (d, self.count))
|
||||
self.copy(from: range, to: 0)
|
||||
let range = unsafe Range(uncheckedBounds: (d, self.count))
|
||||
unsafe self.copy(from: range, to: 0)
|
||||
}
|
||||
self.clear(in: Range(uncheckedBounds: (self.count &- d, self.count)))
|
||||
unsafe self.clear(in: Range(uncheckedBounds: (self.count &- d, self.count)))
|
||||
}
|
||||
}
|
||||
@@ -20,24 +20,29 @@ extension BitArray {
|
||||
/// primitives.
|
||||
@usableFromInline
|
||||
@frozen
|
||||
@unsafe
|
||||
internal struct _UnsafeHandle {
|
||||
@usableFromInline
|
||||
internal typealias _BitPosition = _UnsafeBitSet.Index
|
||||
|
||||
@usableFromInline
|
||||
@unsafe
|
||||
internal let _words: UnsafeBufferPointer<_Word>
|
||||
|
||||
@usableFromInline
|
||||
@unsafe // Setter
|
||||
internal var _count: UInt
|
||||
|
||||
#if DEBUG
|
||||
/// True when this handle does not support table mutations.
|
||||
/// (This is only checked in debug builds.)
|
||||
@usableFromInline
|
||||
@safe
|
||||
internal let _mutable: Bool
|
||||
#endif
|
||||
|
||||
@inline(__always)
|
||||
@safe
|
||||
internal func ensureMutable() {
|
||||
#if DEBUG
|
||||
assert(_mutable)
|
||||
@@ -46,7 +51,7 @@ extension BitArray {
|
||||
|
||||
internal var _mutableWords: UnsafeMutableBufferPointer<_Word> {
|
||||
ensureMutable()
|
||||
return UnsafeMutableBufferPointer(mutating: _words)
|
||||
return unsafe UnsafeMutableBufferPointer(mutating: _words)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -58,8 +63,8 @@ extension BitArray {
|
||||
) {
|
||||
assert(count <= words.count * _Word.capacity)
|
||||
assert(count > (words.count - 1) * _Word.capacity)
|
||||
self._words = words
|
||||
self._count = count
|
||||
unsafe self._words = words
|
||||
unsafe self._count = count
|
||||
#if DEBUG
|
||||
self._mutable = mutable
|
||||
#endif
|
||||
@@ -72,7 +77,7 @@ extension BitArray {
|
||||
count: UInt,
|
||||
mutable: Bool
|
||||
) {
|
||||
self.init(
|
||||
unsafe self.init(
|
||||
words: UnsafeBufferPointer(words),
|
||||
count: count,
|
||||
mutable: mutable)
|
||||
@@ -81,42 +86,45 @@ extension BitArray {
|
||||
}
|
||||
|
||||
extension BitArray._UnsafeHandle {
|
||||
@safe
|
||||
@_transparent
|
||||
internal var count: Int {
|
||||
Int(_count)
|
||||
unsafe Int(_count)
|
||||
}
|
||||
|
||||
@safe
|
||||
internal var end: _BitPosition {
|
||||
_BitPosition(_count)
|
||||
unsafe _BitPosition(_count)
|
||||
}
|
||||
|
||||
internal func set(at position: Int) {
|
||||
ensureMutable()
|
||||
assert(position >= 0 && position < _count)
|
||||
assert(position >= 0 && position < count)
|
||||
let (word, bit) = _BitPosition(UInt(position)).split
|
||||
_mutableWords[word].insert(bit)
|
||||
unsafe _mutableWords[word].insert(bit)
|
||||
}
|
||||
|
||||
internal func clear(at position: Int) {
|
||||
ensureMutable()
|
||||
assert(position >= 0 && position < _count)
|
||||
assert(position >= 0 && position < count)
|
||||
let (word, bit) = _BitPosition(UInt(position)).split
|
||||
_mutableWords[word].remove(bit)
|
||||
unsafe _mutableWords[word].remove(bit)
|
||||
}
|
||||
|
||||
internal subscript(position: Int) -> Bool {
|
||||
get {
|
||||
assert(position >= 0 && position < _count)
|
||||
assert(position >= 0 && position < count)
|
||||
let (word, bit) = _BitPosition(UInt(position)).split
|
||||
return _words[word].contains(bit)
|
||||
return unsafe _words[word].contains(bit)
|
||||
}
|
||||
set {
|
||||
ensureMutable()
|
||||
assert(position >= 0 && position < _count)
|
||||
assert(position >= 0 && position < count)
|
||||
let (word, bit) = _BitPosition(UInt(position)).split
|
||||
if newValue {
|
||||
_mutableWords[word].insert(bit)
|
||||
unsafe _mutableWords[word].insert(bit)
|
||||
} else {
|
||||
_mutableWords[word].remove(bit)
|
||||
unsafe _mutableWords[word].remove(bit)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,16 +139,16 @@ extension BitArray._UnsafeHandle {
|
||||
guard range.count > 0 else { return }
|
||||
let (lw, lb) = _BitPosition(range.lowerBound).split
|
||||
let (uw, ub) = _BitPosition(range.upperBound).endSplit
|
||||
let words = _mutableWords
|
||||
let words = unsafe _mutableWords
|
||||
guard lw != uw else {
|
||||
words[lw].formUnion(_Word(from: lb, to: ub))
|
||||
unsafe words[lw].formUnion(_Word(from: lb, to: ub))
|
||||
return
|
||||
}
|
||||
words[lw].formUnion(_Word(upTo: lb).complement())
|
||||
unsafe words[lw].formUnion(_Word(upTo: lb).complement())
|
||||
for w in lw + 1 ..< uw {
|
||||
words[w] = _Word.allBits
|
||||
unsafe words[w] = _Word.allBits
|
||||
}
|
||||
words[uw].formUnion(_Word(upTo: ub))
|
||||
unsafe words[uw].formUnion(_Word(upTo: ub))
|
||||
}
|
||||
|
||||
internal mutating func clear(in range: Range<Int>) {
|
||||
@@ -151,15 +159,15 @@ extension BitArray._UnsafeHandle {
|
||||
guard range.count > 0 else { return }
|
||||
let (lw, lb) = _BitPosition(range.lowerBound).split
|
||||
let (uw, ub) = _BitPosition(range.upperBound).endSplit
|
||||
let words = _mutableWords
|
||||
let words = unsafe _mutableWords
|
||||
guard lw != uw else {
|
||||
words[lw].subtract(_Word(from: lb, to: ub))
|
||||
unsafe words[lw].subtract(_Word(from: lb, to: ub))
|
||||
return
|
||||
}
|
||||
words[lw].subtract(_Word(upTo: lb).complement())
|
||||
unsafe words[lw].subtract(_Word(upTo: lb).complement())
|
||||
for w in lw + 1 ..< uw {
|
||||
words[w] = _Word.empty
|
||||
unsafe words[w] = _Word.empty
|
||||
}
|
||||
words[uw].subtract(_Word(upTo: ub))
|
||||
unsafe words[uw].subtract(_Word(upTo: ub))
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import InternalCollectionsUtilities
|
||||
/// See `BitSet` for an alternative form of the same underlying data
|
||||
/// structure, treating it as a set of nonnegative integers corresponding to
|
||||
/// `true` bits.
|
||||
@safe
|
||||
public struct BitArray {
|
||||
@usableFromInline
|
||||
internal typealias _BitPosition = _UnsafeBitSet.Index
|
||||
@@ -58,9 +59,9 @@ extension BitArray {
|
||||
_ body: (_UnsafeHandle) throws -> R
|
||||
) rethrows -> R {
|
||||
try _storage.withUnsafeBufferPointer { words in
|
||||
let handle = _UnsafeHandle(
|
||||
let handle = unsafe _UnsafeHandle(
|
||||
words: words, count: _count, mutable: false)
|
||||
return try body(handle)
|
||||
return unsafe try body(handle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +73,8 @@ extension BitArray {
|
||||
_checkInvariants()
|
||||
}
|
||||
return try _storage.withUnsafeMutableBufferPointer { words in
|
||||
var handle = _UnsafeHandle(words: words, count: _count, mutable: true)
|
||||
return try body(&handle)
|
||||
var handle = unsafe _UnsafeHandle(words: words, count: _count, mutable: true)
|
||||
return unsafe try body(&handle)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user