mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
Sources: Add unsafe keywords where needed
This commit is contained in:
301 files changed
+6051
-5498
No files matched your search
@@ -18,6 +18,7 @@ import InternalCollectionsUtilities
|
||||
extension _HTable {
|
||||
@usableFromInline
|
||||
@frozen
|
||||
@safe
|
||||
package struct Bitmap: ~Copyable, ~Escapable {
|
||||
@usableFromInline
|
||||
package typealias Bucket = _HTable.Bucket
|
||||
@@ -28,7 +29,7 @@ extension _HTable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow table)
|
||||
package init(table: borrowing _HTable) {
|
||||
self._words = .init(start: table._bitmap, count: table.wordCount)
|
||||
unsafe self._words = .init(start: table._bitmap, count: table.wordCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,42 +40,43 @@ extension _HTable.Bitmap {
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
@safe
|
||||
package func isValid(_ bucket: Bucket) -> Bool {
|
||||
bucket.word < _words.count
|
||||
unsafe bucket.word < _words.count
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
package func isOccupied(_ bucket: Bucket) -> Bool {
|
||||
assert(isValid(bucket))
|
||||
return _words[bucket.word].contains(bucket.bit)
|
||||
return unsafe _words[bucket.word].contains(bucket.bit)
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
package func setOccupied(_ bucket: Bucket) {
|
||||
assert(isValid(bucket))
|
||||
_words[bucket.word].set(bucket.bit)
|
||||
unsafe _words[bucket.word].set(bucket.bit)
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
package func clearOccupied(_ bucket: Bucket) {
|
||||
assert(isValid(bucket))
|
||||
_words[bucket.word].clear(bucket.bit)
|
||||
unsafe _words[bucket.word].clear(bucket.bit)
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
package func clearAll() {
|
||||
_words.update(repeating: .empty)
|
||||
unsafe _words.update(repeating: .empty)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
package func occupiedCount() -> Int {
|
||||
var c = 0
|
||||
for i in 0 ..< _words.count {
|
||||
c += _words[i].count
|
||||
for i in unsafe 0 ..< _words.count {
|
||||
unsafe c += _words[i].count
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -84,7 +86,7 @@ extension _HTable.Bitmap {
|
||||
guard isValid(start) else { return nil }
|
||||
|
||||
var word = start.word
|
||||
var bits = _words[word]
|
||||
var bits = unsafe _words[word]
|
||||
bits.removeAll(upTo: start.bit)
|
||||
|
||||
while true {
|
||||
@@ -92,10 +94,10 @@ extension _HTable.Bitmap {
|
||||
return Bucket(word: word, bit: bit)
|
||||
}
|
||||
word &+= 1
|
||||
if word >= _words.count {
|
||||
if unsafe word >= _words.count {
|
||||
return nil
|
||||
}
|
||||
bits = _words[word]
|
||||
unsafe bits = _words[word]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,16 +105,16 @@ extension _HTable.Bitmap {
|
||||
package func firstOccupiedBucket(
|
||||
from start: Bucket, limit: Bucket
|
||||
) -> Bucket {
|
||||
assert(isValid(start) && start <= limit && limit.word <= _words.count)
|
||||
assert(unsafe isValid(start) && start <= limit && limit.word <= _words.count)
|
||||
var word = start.word
|
||||
var bits = _words[word]
|
||||
var bits = unsafe _words[word]
|
||||
bits.removeAll(upTo: start.bit)
|
||||
while word < limit.word {
|
||||
if let bit = bits.firstMember {
|
||||
return Bucket(word: word, bit: bit)
|
||||
}
|
||||
word &+= 1
|
||||
bits = word < _words.count ? _words[word] : .empty
|
||||
unsafe bits = word < _words.count ? _words[word] : .empty
|
||||
}
|
||||
bits.removeAll(from: limit.bit)
|
||||
if let bit = bits.firstMember {
|
||||
@@ -127,7 +129,7 @@ extension _HTable.Bitmap {
|
||||
package func collisionChainEnd(from start: Bucket) -> Bucket {
|
||||
assert(isOccupied(start))
|
||||
var word = start.word
|
||||
var bits = _words[word]
|
||||
var bits = unsafe _words[word]
|
||||
bits.insertAll(upTo: start.bit)
|
||||
var wrapped = false
|
||||
while true {
|
||||
@@ -136,12 +138,12 @@ extension _HTable.Bitmap {
|
||||
return Bucket(word: word, bit: bit)
|
||||
}
|
||||
word &+= 1
|
||||
if word >= _words.count {
|
||||
if unsafe word >= _words.count {
|
||||
precondition(!wrapped, "Corrupt hash table")
|
||||
wrapped = true
|
||||
word = 0
|
||||
}
|
||||
bits = _words[word]
|
||||
unsafe bits = _words[word]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,9 +155,9 @@ extension _HTable.Bitmap {
|
||||
) -> Bucket {
|
||||
assert(isValid(start))
|
||||
var word = start.word
|
||||
var bits = _words[word]
|
||||
var bits = unsafe _words[word]
|
||||
bits.insertAll(upTo: start.bit)
|
||||
var remainder = Swift.min(
|
||||
var remainder = unsafe Swift.min(
|
||||
UInt(bitPattern: maxCount) &+ start.bit,
|
||||
UInt(bitPattern: (_words.count &- start.word) &* Word.capacity))
|
||||
while true {
|
||||
@@ -169,8 +171,8 @@ extension _HTable.Bitmap {
|
||||
word &+= 1
|
||||
remainder &-= Word._capacity
|
||||
guard remainder > 0 else { break }
|
||||
assert(word < _words.count)
|
||||
bits = _words[word]
|
||||
assert(unsafe word < _words.count)
|
||||
unsafe bits = _words[word]
|
||||
}
|
||||
return Bucket(word: word, bit: remainder)
|
||||
}
|
||||
@@ -187,7 +189,7 @@ extension _HTable.Bitmap {
|
||||
let end = self._nextOccupiedChunkEnd(
|
||||
from: bucket,
|
||||
maxCount: Swift.min(maxCount, limit.offset &- bucket.offset))
|
||||
let result = Range(uncheckedBounds: (bucket, end))
|
||||
let result = unsafe Range(uncheckedBounds: (bucket, end))
|
||||
if isValid(end), !isOccupied(end) {
|
||||
bucket = firstOccupiedBucket(from: end, limit: limit)
|
||||
} else {
|
||||
|
||||
@@ -21,6 +21,7 @@ extension _HTable {
|
||||
/// to a function that expects a word index, or vice versa.
|
||||
@usableFromInline
|
||||
@frozen
|
||||
@safe
|
||||
package struct Bucket {
|
||||
@usableFromInline
|
||||
internal typealias Word = _HTable.Word
|
||||
@@ -127,11 +128,11 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
||||
package subscript(bucket: _HTable.Bucket) -> Element {
|
||||
@_transparent
|
||||
unsafeAddress {
|
||||
return .init(_ptr(at: bucket.offset))
|
||||
return unsafe .init(_ptr(at: bucket.offset))
|
||||
}
|
||||
@_transparent
|
||||
nonmutating unsafeMutableAddress {
|
||||
return _ptr(at: bucket.offset)
|
||||
return unsafe _ptr(at: bucket.offset)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,7 +141,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
||||
package func _ptr(
|
||||
at bucket: _HTable.Bucket
|
||||
) -> UnsafeMutablePointer<Element> {
|
||||
_ptr(at: bucket.offset)
|
||||
unsafe _ptr(at: bucket.offset)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -149,13 +150,13 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
||||
at bucket: _HTable.Bucket,
|
||||
to value: consuming Element
|
||||
) {
|
||||
initializeElement(at: bucket.offset, to: value)
|
||||
unsafe initializeElement(at: bucket.offset, to: value)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
package func _extracting(_ buckets: Range<_HTable.Bucket>) -> Self {
|
||||
_extracting(
|
||||
unsafe _extracting(
|
||||
uncheckedFrom: buckets.lowerBound.offset,
|
||||
to: buckets.upperBound.offset)
|
||||
}
|
||||
@@ -166,7 +167,7 @@ extension Range where Bound == _HTable.Bucket {
|
||||
package var _offsets: Range<Int> {
|
||||
@_transparent
|
||||
get {
|
||||
.init(uncheckedBounds: (lowerBound.offset, upperBound.offset))
|
||||
unsafe .init(uncheckedBounds: (lowerBound.offset, upperBound.offset))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,13 +17,13 @@ extension _HTable {
|
||||
swapper: (Bucket) -> Void,
|
||||
) -> Bucket {
|
||||
assert(isSmall)
|
||||
if _count > 0 {
|
||||
if self.count > 0 {
|
||||
// Scramble order by swapping the new item in the middle of the table.
|
||||
swapper(Bucket(offset: _count / 2))
|
||||
swapper(Bucket(offset: self.count / 2))
|
||||
}
|
||||
let r = Bucket(offset: _count)
|
||||
_count &+= 1
|
||||
_maxProbeLength = _count
|
||||
let r = Bucket(offset: self.count)
|
||||
unsafe _count &+= 1
|
||||
_maxProbeLength = self.count
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ extension _HTable {
|
||||
probeLength &+= 1
|
||||
}
|
||||
bitmap.setOccupied(b)
|
||||
_count &+= 1
|
||||
unsafe _count &+= 1
|
||||
if probeLength > _maxProbeLength {
|
||||
_maxProbeLength = probeLength
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ extension _HTable {
|
||||
/// iterator values outside the closure call that produced the original
|
||||
/// hash table.
|
||||
@usableFromInline
|
||||
@safe
|
||||
package struct BucketIterator: ~Escapable {
|
||||
@usableFromInline
|
||||
package typealias Bucket = _HTable.Bucket
|
||||
@@ -72,17 +73,17 @@ extension _HTable {
|
||||
assert(table.isValid(bucket) || bucket == table.endBucket)
|
||||
assert(table.endBucket.bit == 0 || table.endBucket.word == 0) // We rely on this throughout the code below
|
||||
self._bucket = bucket
|
||||
if let bitmap = table._bitmap {
|
||||
self._words = .init(bitmap)
|
||||
if let bitmap = unsafe table._bitmap {
|
||||
unsafe self._words = .init(bitmap)
|
||||
self._endBucket = table.endBucket
|
||||
self._nextBits = bitmap[_bucket.word].shiftedDown(by: _bucket.bit)
|
||||
self._nextBits = unsafe bitmap[_bucket.word].shiftedDown(by: _bucket.bit)
|
||||
if table.endBucket._offset < Word.capacity {
|
||||
self._nextBitCount = UInt8(table.endBucket.bit &- _bucket.bit)
|
||||
} else {
|
||||
self._nextBitCount = UInt8(Word._capacity &- _bucket.bit)
|
||||
}
|
||||
} else {
|
||||
self._words = nil
|
||||
unsafe self._words = nil
|
||||
self._endBucket = Bucket(offset: table.count) // Note: not capacity!
|
||||
self._nextBits = .empty
|
||||
self._nextBitCount = 0
|
||||
@@ -108,7 +109,7 @@ extension _HTable.BucketIterator {
|
||||
@_transparent
|
||||
package var isOccupied: Bool {
|
||||
assert(!isAtEnd)
|
||||
if _words == nil { return true }
|
||||
if unsafe _words == nil { return true }
|
||||
return _nextBits.contains(0)
|
||||
}
|
||||
|
||||
@@ -117,8 +118,8 @@ extension _HTable.BucketIterator {
|
||||
package mutating func restart() {
|
||||
_wrapped = true
|
||||
_bucket._offset = 0
|
||||
if let words = _words {
|
||||
_nextBits = words[_bucket.word]
|
||||
if let words = unsafe _words {
|
||||
_nextBits = unsafe words[_bucket.word]
|
||||
_nextBitCount = UInt8(Swift.min(Word._capacity, _endBucket._offset))
|
||||
}
|
||||
}
|
||||
@@ -148,8 +149,8 @@ extension _HTable.BucketIterator {
|
||||
advanceToEnd()
|
||||
return false
|
||||
}
|
||||
if let words = _words{
|
||||
_nextBits = words[_bucket.word]
|
||||
if let words = unsafe _words {
|
||||
_nextBits = unsafe words[_bucket.word]
|
||||
_nextBitCount = UInt8(Word._capacity)
|
||||
}
|
||||
return true
|
||||
@@ -161,8 +162,8 @@ extension _HTable.BucketIterator {
|
||||
_bucket.advanceToNextWord()
|
||||
if isAtEnd {
|
||||
_wrap()
|
||||
} else if let words = _words {
|
||||
_nextBits = words[_bucket.word]
|
||||
} else if let words = unsafe _words {
|
||||
_nextBits = unsafe words[_bucket.word]
|
||||
_nextBitCount = UInt8(Word._capacity)
|
||||
}
|
||||
}
|
||||
@@ -177,14 +178,14 @@ extension _HTable.BucketIterator {
|
||||
advanceToEnd()
|
||||
return false
|
||||
}
|
||||
guard let words = _words else { return true }
|
||||
guard let words = unsafe _words else { return true }
|
||||
assert(_nextBitCount > 0)
|
||||
_nextBitCount &-= 1
|
||||
if _nextBitCount > 0 {
|
||||
_nextBits = _nextBits.shiftedDown(by: 1)
|
||||
} else {
|
||||
assert(_bucket.bit == 0)
|
||||
_nextBits = words[_bucket.word]
|
||||
_nextBits = unsafe words[_bucket.word]
|
||||
_nextBitCount = UInt8(Word._capacity)
|
||||
}
|
||||
return true
|
||||
@@ -198,13 +199,13 @@ extension _HTable.BucketIterator {
|
||||
_wrap()
|
||||
return
|
||||
}
|
||||
guard let words = _words else { return }
|
||||
guard let words = unsafe _words else { return }
|
||||
assert(_nextBitCount > 0)
|
||||
_nextBitCount &-= 1
|
||||
if _nextBitCount > 0 {
|
||||
_nextBits = _nextBits.shiftedDown(by: 1)
|
||||
} else {
|
||||
_nextBits = words[_bucket.word]
|
||||
_nextBits = unsafe words[_bucket.word]
|
||||
_nextBitCount = UInt8(Word._capacity)
|
||||
}
|
||||
}
|
||||
@@ -215,7 +216,7 @@ extension _HTable.BucketIterator {
|
||||
@_lifetime(self: copy self)
|
||||
package mutating func advanceToOccupied() -> Bool {
|
||||
if isAtEnd { return false }
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
return true
|
||||
}
|
||||
while true {
|
||||
@@ -239,7 +240,7 @@ extension _HTable.BucketIterator {
|
||||
) -> Bool {
|
||||
var remainder = UInt(bitPattern: maxCount)
|
||||
if isAtEnd { return false }
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
let delta = Swift.min(_endBucket._offset &- _bucket._offset, remainder)
|
||||
_bucket._offset &+= delta
|
||||
return true
|
||||
@@ -269,7 +270,7 @@ extension _HTable.BucketIterator {
|
||||
@usableFromInline
|
||||
@_lifetime(self: copy self)
|
||||
package mutating func wrapToOccupied() {
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
wrapToNextBit()
|
||||
return
|
||||
}
|
||||
@@ -290,7 +291,7 @@ extension _HTable.BucketIterator {
|
||||
@_lifetime(self: copy self)
|
||||
package mutating func advanceToUnoccupied() -> Bool {
|
||||
assert(!isAtEnd)
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
_bucket = _endBucket
|
||||
return false
|
||||
}
|
||||
@@ -317,7 +318,7 @@ extension _HTable.BucketIterator {
|
||||
assert(maxCount > 0)
|
||||
assert(!isAtEnd)
|
||||
var remainder = UInt(bitPattern: maxCount)
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
let delta = Swift.min(_endBucket._offset &- _bucket._offset, remainder)
|
||||
_bucket._offset &+= delta
|
||||
return false
|
||||
@@ -348,7 +349,7 @@ extension _HTable.BucketIterator {
|
||||
@usableFromInline
|
||||
@_lifetime(self: copy self)
|
||||
package mutating func wrapToUnoccupied() {
|
||||
if _words == nil {
|
||||
if unsafe _words == nil {
|
||||
_bucket = _endBucket
|
||||
return
|
||||
}
|
||||
@@ -374,7 +375,7 @@ extension _HTable.BucketIterator {
|
||||
let start = self.currentBucket
|
||||
self.advanceToUnoccupied(maxCount: maxCount)
|
||||
let end = self.currentBucket
|
||||
return Range(uncheckedBounds: (start, end))
|
||||
return unsafe Range(uncheckedBounds: (start, end))
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
@@ -388,6 +389,6 @@ extension _HTable.BucketIterator {
|
||||
let start = self.currentBucket
|
||||
self.advanceToOccupied(maxCount: maxCount)
|
||||
let end = self.currentBucket
|
||||
return Range(uncheckedBounds: (start, end))
|
||||
return unsafe Range(uncheckedBounds: (start, end))
|
||||
}
|
||||
}
|
||||
@@ -17,11 +17,12 @@ extension _HTable {
|
||||
/// is created, the table remains in an inconsistent state until one of
|
||||
/// `resolveHole` or `finalizeHole` is called on the same bucket.
|
||||
@usableFromInline
|
||||
@unsafe
|
||||
package mutating func createHole(
|
||||
at bucket: Bucket
|
||||
) {
|
||||
assert(isOccupied(bucket))
|
||||
_count &-= 1
|
||||
unsafe _count &-= 1
|
||||
}
|
||||
|
||||
/// Restore hash table invariants after a hole was created by `createHole`.
|
||||
@@ -29,13 +30,14 @@ extension _HTable {
|
||||
/// reach the end of the affected chain of occupied buckets.
|
||||
@usableFromInline
|
||||
@discardableResult
|
||||
@unsafe
|
||||
package mutating func resolveHole(
|
||||
at bucket: Bucket,
|
||||
hashGenerator: (Bucket) -> Int,
|
||||
mover: (Bucket, Bucket) -> Void,
|
||||
) -> Bucket {
|
||||
if isSmall {
|
||||
return finalizeHole(at: bucket, mover: mover)
|
||||
return unsafe finalizeHole(at: bucket, mover: mover)
|
||||
}
|
||||
assert(isOccupied(bucket))
|
||||
|
||||
@@ -88,8 +90,8 @@ extension _HTable {
|
||||
// incremental updates here (like the original thesis suggests), but it's
|
||||
// unclear if the benefits of that would outweigh the costs.)
|
||||
// (FIXME: Try it and see.)
|
||||
if _maxProbeLength > _count {
|
||||
_maxProbeLength = _count
|
||||
if _maxProbeLength > self.count {
|
||||
_maxProbeLength = self.count
|
||||
}
|
||||
|
||||
return hole
|
||||
@@ -102,14 +104,15 @@ extension _HTable {
|
||||
/// does enough to allow the hash table to get resized.
|
||||
@discardableResult
|
||||
@usableFromInline
|
||||
@unsafe
|
||||
package mutating func finalizeHole(
|
||||
at bucket: Bucket,
|
||||
mover: (Bucket, Bucket) -> Void,
|
||||
) -> Bucket {
|
||||
assert(isValid(bucket))
|
||||
if isSmall {
|
||||
let last = Bucket(offset: _count) // Note: _count was decreased by createHole
|
||||
_maxProbeLength = _count
|
||||
let last = Bucket(offset: self.count) // Note: _count was decreased by createHole
|
||||
_maxProbeLength = self.count
|
||||
guard bucket < last else { return bucket }
|
||||
mover(last, bucket)
|
||||
return last
|
||||
|
||||
@@ -25,7 +25,7 @@ extension _HTable {
|
||||
// Let's reverse the order of members, to emphasize that this is not an
|
||||
// ordered container.
|
||||
let c = source.count
|
||||
self._count = c
|
||||
unsafe self._count = c
|
||||
self._maxProbeLength = c
|
||||
var dst = Bucket(offset: c)
|
||||
var it = source.makeBucketIterator()
|
||||
|
||||
@@ -18,6 +18,7 @@ import InternalCollectionsUtilities
|
||||
extension _HTable {
|
||||
/// The minimum hash table scale.
|
||||
@_alwaysEmitIntoClient
|
||||
@safe
|
||||
package static var minimumScale: UInt8 {
|
||||
@_effects(readnone)
|
||||
@_transparent
|
||||
@@ -28,6 +29,7 @@ extension _HTable {
|
||||
|
||||
/// The maximum hash table scale.
|
||||
@_alwaysEmitIntoClient
|
||||
@safe
|
||||
package static var maximumScale: UInt8 {
|
||||
@_effects(readnone)
|
||||
@_transparent
|
||||
@@ -38,6 +40,7 @@ extension _HTable {
|
||||
|
||||
/// The maximum number of items for which we do not create a hash table.
|
||||
@usableFromInline
|
||||
@safe
|
||||
package static var maximumUnhashedCount: Int {
|
||||
@_effects(readnone)
|
||||
get {
|
||||
@@ -47,6 +50,7 @@ extension _HTable {
|
||||
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
@safe
|
||||
package static func wordCount(forScale scale: UInt8) -> Int {
|
||||
guard scale > 0 else { return 0 }
|
||||
let shift = Swift.max(UInt(scale), Word.wordShift) - Word.wordShift
|
||||
@@ -74,24 +78,29 @@ extension _HTable {
|
||||
|
||||
/// The numerator of the maximum hash table load factor.
|
||||
@_transparent
|
||||
@safe
|
||||
internal static var _maxLFNum: UInt { 7 }
|
||||
|
||||
/// The denominator of the maximum hash table load factor.
|
||||
@_transparent
|
||||
@safe
|
||||
internal static var _maxLFDenom: UInt { 8 }
|
||||
|
||||
/// The numerator of the minimum hash table load factor.
|
||||
@_transparent
|
||||
@safe
|
||||
internal static var _minLFNum: UInt { 1 }
|
||||
|
||||
/// The denominator of minimum hash table load factor.
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
internal static var _minLFDenom: UInt { 8 }
|
||||
|
||||
/// The minimum number of items that can be held in a hash table of the given scale.
|
||||
@usableFromInline
|
||||
@_effects(readnone)
|
||||
@safe
|
||||
package static func minimumCapacity(forScale scale: UInt8) -> Int {
|
||||
guard scale >= minimumScale else { return 0 }
|
||||
precondition(scale <= maximumScale)
|
||||
@@ -102,6 +111,7 @@ extension _HTable {
|
||||
/// The maximum number of items that can be held in a hash table of the given scale.
|
||||
@usableFromInline
|
||||
@_effects(readnone)
|
||||
@safe
|
||||
package static func maximumCapacity(forScale scale: UInt8) -> Int {
|
||||
let scale = Swift.max(scale, minimumScale &- 1)
|
||||
let bucketCount: UInt = 1 &<< scale
|
||||
@@ -112,6 +122,7 @@ extension _HTable {
|
||||
/// The minimum hash table scale that can hold the specified number of elements.
|
||||
@usableFromInline
|
||||
@_effects(readnone)
|
||||
@safe
|
||||
package static func minimumScale(forCapacity capacity: Int) -> UInt8 {
|
||||
guard capacity > maximumUnhashedCount else { return 0 }
|
||||
let capacity = UInt(truncatingIfNeeded: Swift.max(capacity, 1))
|
||||
@@ -132,6 +143,7 @@ extension _HTable {
|
||||
|
||||
@usableFromInline
|
||||
@_effects(readnone)
|
||||
@safe
|
||||
package static func dynamicStorageParameters(
|
||||
minimumCapacity: Int
|
||||
) -> (scale: UInt8, capacity: Int) {
|
||||
|
||||
@@ -17,28 +17,35 @@ import InternalCollectionsUtilities
|
||||
|
||||
@usableFromInline
|
||||
@frozen
|
||||
@safe
|
||||
package struct _HTable: ~Copyable {
|
||||
@usableFromInline
|
||||
package typealias Word = _UnsafeBitSet._Word
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@unsafe // FIXME: Only mutations are unsafe
|
||||
package var _count: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@unsafe // FIXME: Only mutations are unsafe
|
||||
package var _capacity: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@unsafe
|
||||
package let _bitmap: UnsafeMutablePointer<Word>?
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@safe
|
||||
package var _maxProbeLength: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@safe
|
||||
package let scale: UInt8
|
||||
|
||||
// FIXME: Add a reservedScale for UniqueSet/UniqueDictonary
|
||||
|
||||
@inlinable
|
||||
@safe
|
||||
internal init(
|
||||
_capacity: Int,
|
||||
scale: UInt8
|
||||
@@ -46,22 +53,23 @@ package struct _HTable: ~Copyable {
|
||||
assert(
|
||||
_capacity >= Self.minimumCapacity(forScale: scale)
|
||||
&& _capacity <= Self.maximumCapacity(forScale: scale))
|
||||
self._count = 0
|
||||
self._capacity = _capacity
|
||||
unsafe self._count = 0
|
||||
unsafe self._capacity = _capacity
|
||||
self._maxProbeLength = 0
|
||||
self.scale = scale
|
||||
if scale == 0 {
|
||||
self._bitmap = nil
|
||||
unsafe self._bitmap = nil
|
||||
} else {
|
||||
assert(scale >= Self.minimumScale && scale <= Self.maximumScale)
|
||||
let wordCount = _HTable.wordCount(forScale: scale)
|
||||
let bitmap = UnsafeMutablePointer<Word>.allocate(capacity: wordCount)
|
||||
bitmap.initialize(repeating: .empty, count: wordCount)
|
||||
self._bitmap = bitmap
|
||||
unsafe bitmap.initialize(repeating: .empty, count: wordCount)
|
||||
unsafe self._bitmap = bitmap
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@safe
|
||||
internal init(capacity: Int) {
|
||||
assert(capacity >= 0)
|
||||
self.init(
|
||||
@@ -70,6 +78,7 @@ package struct _HTable: ~Copyable {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@safe
|
||||
internal init(minimumCapacity: Int) {
|
||||
precondition(minimumCapacity >= 0, "Capacity must be nonnegative")
|
||||
let p = Self.dynamicStorageParameters(minimumCapacity: minimumCapacity)
|
||||
@@ -79,44 +88,50 @@ package struct _HTable: ~Copyable {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
deinit {
|
||||
_bitmap?.deallocate()
|
||||
unsafe _bitmap?.deallocate()
|
||||
}
|
||||
}
|
||||
|
||||
extension _HTable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var isSmall: Bool {
|
||||
_bitmap == nil
|
||||
unsafe _bitmap == nil
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var count: Int {
|
||||
_count
|
||||
unsafe _assumeNonNegative(_count)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var capacity: Int {
|
||||
_capacity
|
||||
unsafe _assumeNonNegative(_capacity)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var isEmpty: Bool {
|
||||
_count == 0
|
||||
count == 0
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var isFull: Bool {
|
||||
_count == capacity
|
||||
count == capacity
|
||||
}
|
||||
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var wordCount: Int {
|
||||
if isSmall { return 0 }
|
||||
return Word.wordCount(forBitCount: bucketCount)
|
||||
@@ -126,18 +141,21 @@ extension _HTable {
|
||||
extension _HTable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var startBucket: Bucket {
|
||||
Bucket(offset: 0)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var endBucket: Bucket {
|
||||
Bucket(offset: bucketCount)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var storageCapacity: Int {
|
||||
if isSmall { return capacity }
|
||||
return 1 &<< scale
|
||||
@@ -145,18 +163,21 @@ extension _HTable {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
@safe
|
||||
package var bucketCount: UInt {
|
||||
UInt(bitPattern: storageCapacity)
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
@safe
|
||||
package func isValid(_ bucket: Bucket) -> Bool {
|
||||
bucket._offset < bucketCount
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
@safe
|
||||
package func wrapBucket(after bucket: inout Bucket) {
|
||||
assert(isValid(bucket))
|
||||
bucket._offset &+= 1
|
||||
@@ -167,12 +188,14 @@ extension _HTable {
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
@safe
|
||||
package func formBucket(after bucket: inout Bucket) {
|
||||
assert(isValid(bucket))
|
||||
bucket._offset &+= 1
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@safe
|
||||
package var bitmap: Bitmap {
|
||||
@_lifetime(borrow self)
|
||||
get {
|
||||
@@ -182,20 +205,23 @@ extension _HTable {
|
||||
|
||||
@_transparent
|
||||
@inlinable
|
||||
@safe
|
||||
package func isOccupied(_ bucket: Bucket) -> Bool {
|
||||
if isSmall { return bucket._offset < _count }
|
||||
if isSmall { return bucket._offset < count }
|
||||
return bitmap.isOccupied(bucket)
|
||||
}
|
||||
}
|
||||
|
||||
extension _HTable {
|
||||
@_transparent
|
||||
@safe
|
||||
internal func idealBucket(forHashValue hashValue: Int) -> Bucket {
|
||||
let hashValue = UInt(truncatingIfNeeded: hashValue)
|
||||
return Bucket(offset: hashValue & (bucketCount &- 1))
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@safe
|
||||
internal func probeLength(
|
||||
from start: Bucket,
|
||||
to end: Bucket
|
||||
@@ -207,6 +233,7 @@ extension _HTable {
|
||||
}
|
||||
|
||||
@_transparent
|
||||
@safe
|
||||
internal func probeLength(
|
||||
forHashValue hashValue: Int,
|
||||
in bucket: Bucket
|
||||
@@ -218,8 +245,9 @@ extension _HTable {
|
||||
|
||||
extension _HTable {
|
||||
@usableFromInline
|
||||
@safe
|
||||
package mutating func clear() {
|
||||
self._count = 0
|
||||
unsafe self._count = 0
|
||||
self.bitmap.clearAll()
|
||||
self._maxProbeLength = 0
|
||||
}
|
||||
@@ -227,8 +255,9 @@ extension _HTable {
|
||||
|
||||
extension _HTable {
|
||||
@usableFromInline
|
||||
@safe
|
||||
package func isTriviallyIdentical(to other: borrowing Self) -> Bool {
|
||||
self._count == other._count
|
||||
unsafe self._count == other._count
|
||||
&& self._capacity == other._capacity
|
||||
&& self._bitmap == other._bitmap
|
||||
&& self._maxProbeLength == other._maxProbeLength
|
||||
|
||||
Reference in new issue
Block a user