1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 2024 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
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
@usableFromInline
internal typealias _UnsafeHashTable = _HashTable.UnsafeHandle
extension _HashTable {
/// A non-owning handle to hash table storage, implementing higher-level
/// table operations.
///
/// - Warning: `_UnsafeHashTable` values do not have ownership of their
/// underlying storage buffer. You must not escape these handles outside
/// the closure call that produced them.
@usableFromInline
@frozen
internal struct UnsafeHandle {
@usableFromInline
internal typealias Bucket = _HashTable.Bucket
/// A pointer to the table header.
@usableFromInline
internal var _header: UnsafeMutablePointer<Header>
/// A pointer to bucket storage.
@usableFromInline
internal var _buckets: UnsafeMutablePointer<UInt64>
#if DEBUG
/// True when this handle does not support table mutations.
/// (This is only checked in debug builds.)
@usableFromInline
internal let _readonly: Bool
#endif
/// Initialize a new hash table handle for storage at the supplied locations.
@inlinable
@inline(__always)
internal init(
header: UnsafeMutablePointer<Header>,
buckets: UnsafeMutablePointer<UInt64>,
readonly: Bool
) {
self._header = header
self._buckets = buckets
#if DEBUG
self._readonly = readonly
#endif
}
/// Check that this handle supports mutating operations.
/// Every member that mutates table data must start by calling this function.
/// This helps preventing COW violations.
///
/// Note that this is a noop in release builds.
@inlinable
@inline(__always)
func assertMutable() {
#if DEBUG
assert(!_readonly, "Attempt to mutate a hash table through a read-only handle")
#endif
}
}
}
extension _HashTable.UnsafeHandle {
/// The scale of the hash table. A table of scale *n* holds 2^*n* buckets,
/// each of which contain an *n*-bit value.
@inlinable
@inline(__always)
internal var scale: Int { _header.pointee.scale }
/// The scale corresponding to the last call to `reserveCapacity`.
/// We store this to make sure we don't shrink the table below its reserved size.
@inlinable
@inline(__always)
internal var reservedScale: Int { _header.pointee.reservedScale }
/// The hasher seed to use within this hash table.
@inlinable
@inline(__always)
internal var seed: Int { _header.pointee.seed }
/// A bias value that needs to be added to buckets to convert them into offsets
/// into element storage. (This allows O(1) insertions at the front when the
/// underlying storage supports it.)
@inlinable
@inline(__always)
internal var bias: Int {
get { _header.pointee.bias }
nonmutating set { _header.pointee.bias = newValue }
}
/// The number of buckets within this hash table. This is always a power of two.
@inlinable
@inline(__always)
internal var bucketCount: Int { 1 &<< scale }
@inlinable
@inline(__always)
internal var bucketMask: UInt64 { UInt64(truncatingIfNeeded: bucketCount) - 1 }
/// The number of bits used to store all the buckets in this hash table.
/// Each bucket holds a value that is `scale` bits wide.
@inlinable
@inline(__always)
internal var bitCount: Int { scale &<< scale }
/// The number of 64-bit words that are available in the storage buffer,
/// rounded up to the nearest whole number if necessary.
@inlinable
@inline(__always)
internal var wordCount: Int { (bitCount + UInt64.bitWidth - 1) / UInt64.bitWidth }
/// The maximum number of items that can fit into this table.
@inlinable
@inline(__always)
internal var capacity: Int { _HashTable.maximumCapacity(forScale: scale) }
/// Return the bucket logically following `bucket` in this hash table.
/// The buckets form a cycle, so the last bucket is logically followed by the first.
@inlinable
@inline(__always)
func bucket(after bucket: Bucket) -> Bucket {
var offset = bucket.offset + 1
if offset == bucketCount {
offset = 0
}
return Bucket(offset: offset)
}
/// Return the bucket logically preceding `bucket` in this hash table.
/// The buckets form a cycle, so the first bucket is logically preceded by the last.
@inlinable
@inline(__always)
func bucket(before bucket: Bucket) -> Bucket {
let offset = (bucket.offset == 0 ? bucketCount : bucket.offset) - 1
return Bucket(offset: offset)
}
/// Return the index of the word logically following `word` in this hash table.
/// The buckets form a cycle, so the last word is logically followed by the first.
///
/// Note that the last word may be only partially filled if `scale` is less than 6.
@inlinable
@inline(__always)
func word(after word: Int) -> Int {
var result = word + 1
if result == wordCount {
result = 0
}
return result
}
/// Return the index of the word logically preceding `word` in this hash table.
/// The buckets form a cycle, so the first word is logically preceded by the first.
///
/// Note that the last word may be only partially filled if `scale` is less than 6.
@inlinable
@inline(__always)
func word(before word: Int) -> Int {
if word == 0 {
return wordCount - 1
}
return word - 1
}
/// Return the index of the 64-bit storage word that holds the first bit
/// corresponding to `bucket`, along with its bit position within the word.
@inlinable
internal func position(of bucket: Bucket) -> (word: Int, bit: Int) {
let start = bucket.offset &* scale
return (start &>> 6, start & 0x3F)
}
}
extension _HashTable.UnsafeHandle {
/// Decode and return the logical value corresponding to the specified bucket value.
///
/// The nil value is represented by an all-zero bit pattern.
/// Other values are stored as the complement of the lowest `scale` bits
/// after taking `bias` into account.
/// The range of representable values is `0 ..< bucketCount - 1`.
/// (Note that the value `bucketCount - 1` is missing from this range, as its
/// encoding is used for `nil`. This isn't an issue, because the maximum load
/// factor guarantees that the hash table will never be completely full.)
@inlinable
func _value(forBucketContents bucketContents: UInt64) -> Int? {
let mask = bucketMask
assert(bucketContents <= mask)
guard bucketContents != 0 else { return nil }
let v = (bucketContents ^ mask) &+ UInt64(truncatingIfNeeded: bias)
return Int(truncatingIfNeeded: v >= mask ? v - mask : v)
}
/// Encodes the specified logical value into a `scale`-bit bit pattern suitable
/// for storing into a bucket.
///
/// The nil value is represented by an all-zero bit pattern.
/// Other values are stored as the complement of their lowest `scale` bits.
/// The range of representable values is `0 ..< bucketCount - 1`.
/// (Note that the value `bucketCount - 1` is missing from this range, as it
/// its encoding is used for `nil`. This isn't an issue, because the maximum
/// load factor guarantees that the hash table will never be completely full.)
@inlinable
func _bucketContents(for value: Int?) -> UInt64 {
guard var value = value else { return 0 }
let mask = Int(truncatingIfNeeded: bucketMask)
assert(value >= 0 && value < mask)
value &-= bias
if value < 0 { value += mask }
assert(value >= 0 && value < mask)
return UInt64(truncatingIfNeeded: value ^ mask)
}
@inlinable
subscript(word word: Int) -> UInt64 {
@inline(__always) get {
assert(word >= 0 && word < bucketCount)
return _buckets[word]
}
@inline(__always) nonmutating set {
assert(word >= 0 && word < bucketCount)
assertMutable()
_buckets[word] = newValue
}
}
@inlinable
subscript(raw bucket: Bucket) -> UInt64 {
get {
assert(bucket.offset < bucketCount)
let (word, bit) = position(of: bucket)
var value = self[word: word] &>> bit
let extractedBits = 64 - bit
if extractedBits < scale {
let word2 = self.word(after: word)
value &= (1 &<< extractedBits) - 1
value |= self[word: word2] &<< extractedBits
}
return value & bucketMask
}
nonmutating set {
assertMutable()
assert(bucket.offset < bucketCount)
let mask = bucketMask
assert(newValue <= mask)
let (word, bit) = position(of: bucket)
self[word: word] &= ~(mask &<< bit)
self[word: word] |= newValue &<< bit
let extractedBits = 64 - bit
if extractedBits < scale {
let word2 = self.word(after: word)
self[word: word2] &= ~((1 &<< (scale - extractedBits)) - 1)
self[word: word2] |= newValue &>> extractedBits
}
}
}
@inlinable
@inline(__always)
func isOccupied(_ bucket: Bucket) -> Bool {
self[raw: bucket] != 0
}
/// Return or update the current value stored in the specified bucket.
/// A nil value indicates that the bucket is empty.
@inlinable
internal subscript(bucket: Bucket) -> Int? {
get {
let contents = self[raw: bucket]
return _value(forBucketContents: contents)
}
nonmutating set {
assertMutable()
let v = _bucketContents(for: newValue)
self[raw: bucket] = v
}
}
}
extension _UnsafeHashTable {
@inlinable
internal func _find<Base: RandomAccessCollection>(
_ item: Base.Element,
in elements: Base
) -> (index: Int?, bucket: Bucket)
where Base.Element: Hashable {
let start = idealBucket(for: item)
var (iterator, value) = startFind(start)
while let index = value {
if elements[_offset: index] == item {
return (index, iterator.currentBucket)
}
value = iterator.findNext()
}
return (nil, iterator.currentBucket)
}
}
extension _UnsafeHashTable {
@usableFromInline
internal func firstOccupiedBucketInChain(with bucket: Bucket) -> Bucket {
var bucket = bucket
repeat {
bucket = self.bucket(before: bucket)
} while isOccupied(bucket)
return self.bucket(after: bucket)
}
@inlinable
internal func delete(
bucket: Bucket,
hashValueGenerator: (Int, Int) -> Int // (offset, seed) -> hashValue
) {
assertMutable()
var it = bucketIterator(startingAt: bucket)
assert(it.isOccupied)
it.advance()
guard it.isOccupied else {
// Fast path: Don't get the start bucket when there's nothing to do.
self[bucket] = nil
return
}
// If we've put a hole in the middle of a collision chain, some element after
// the hole may belong where the new hole is.
// Find the first bucket in the collision chain that contains the entry we've just deleted.
let start = firstOccupiedBucketInChain(with: bucket)
var hole = bucket
while it.isOccupied {
let hash = hashValueGenerator(it.currentValue!, seed)
let candidate = idealBucket(forHashValue: hash)
// Does this element belong between start and hole? We need two
// separate tests depending on whether [start, hole] wraps around the
// end of the storage.
let c0 = candidate.offset >= start.offset
let c1 = candidate.offset <= hole.offset
if start.offset <= hole.offset ? (c0 && c1) : (c0 || c1) {
// Fill the hole. Here we are mutating table contents behind the back of
// the iterator; this is okay since we know we are never going to revisit
// `hole` with it.
self[hole] = it.currentValue
hole = it.currentBucket
}
it.advance()
}
self[hole] = nil
}
}
extension _UnsafeHashTable {
@inlinable
internal func adjustContents<Base: RandomAccessCollection>(
preparingForInsertionOfElementAtOffset offset: Int,
in elements: Base
) where Base.Element: Hashable {
assertMutable()
let index = elements._index(at: offset)
if offset < elements.count / 2 {
self.bias += 1
if offset <= capacity / 3 {
var i = 1
for item in elements[..<index] {
var it = bucketIterator(for: item)
it.advance(until: i)
it.currentValue! -= 1
i += 1
}
} else {
var it = bucketIterator(startingAt: Bucket(offset: 0))
repeat {
if let value = it.currentValue, value <= offset {
it.currentValue = value - 1
}
it.advance()
} while it.currentBucket.offset != 0
}
} else {
if elements.count - offset - 1 <= capacity / 3 {
var i = offset
for item in elements[index...] {
var it = bucketIterator(for: item)
it.advance(until: i)
it.currentValue! += 1
i += 1
}
} else {
var it = bucketIterator(startingAt: Bucket(offset: 0))
repeat {
if let value = it.currentValue, value >= offset {
it.currentValue = value + 1
}
it.advance()
} while it.currentBucket.offset != 0
}
}
}
}
extension _UnsafeHashTable {
@inlinable
@inline(__always)
internal func adjustContents<Base: RandomAccessCollection>(
preparingForRemovalOf index: Base.Index,
in elements: Base
) where Base.Element: Hashable {
let next = elements.index(after: index)
adjustContents(preparingForRemovalOf: index ..< next, in: elements)
}
@inlinable
internal func adjustContents<Base: RandomAccessCollection>(
preparingForRemovalOf bounds: Range<Base.Index>,
in elements: Base
) where Base.Element: Hashable {
assertMutable()
let startOffset = elements._offset(of: bounds.lowerBound)
let endOffset = elements._offset(of: bounds.upperBound)
let c = endOffset - startOffset
guard c > 0 else { return }
let remainingCount = elements.count - c
if startOffset >= remainingCount / 2 {
let tailCount = elements.count - endOffset
if tailCount < capacity / 3 {
var i = endOffset
for item in elements[bounds.upperBound...] {
var it = self.bucketIterator(for: item)
it.advance(until: i)
it.currentValue = i - c
i += 1
}
} else {
var it = bucketIterator(startingAt: Bucket(offset: 0))
repeat {
if let value = it.currentValue {
if value >= endOffset {
it.currentValue = value - c
} else {
assert(value < startOffset)
}
}
it.advance()
} while it.currentBucket.offset != 0
}
} else {
if startOffset < capacity / 3 {
var i = 0
for item in elements[..<bounds.lowerBound] {
var it = self.bucketIterator(for: item)
it.advance(until: i)
it.currentValue = i + c
i += 1
}
} else {
var it = bucketIterator(startingAt: Bucket(offset: 0))
repeat {
if let value = it.currentValue {
if value < startOffset {
it.currentValue = value + c
} else {
assert(value >= endOffset)
}
}
it.advance()
} while it.currentBucket.offset != 0
}
self.bias -= c
}
}
}
extension _UnsafeHashTable {
@usableFromInline
internal func clear() {
assertMutable()
_buckets.update(repeating: 0, count: wordCount)
}
}
extension _UnsafeHashTable {
/// Fill an empty hash table by populating it with data from `elements`.
///
/// - Parameter elements: A random-access collection for which this table is being generated.
@inlinable
internal func fill<C: RandomAccessCollection>(
uncheckedUniqueElements elements: C
) where C.Element: Hashable {
assertMutable()
assert(elements.count <= capacity)
// Iterate over elements and insert their offset into the hash table.
var offset = 0
for index in elements.indices {
// Find the insertion position. We know that we're inserting a new item,
// so there is no need to compare it with any of the existing ones.
var it = bucketIterator(for: elements[index])
it.advanceToNextUnoccupiedBucket()
it.currentValue = offset
offset += 1
}
}
/// Fill an empty hash table by populating it with data from `elements`.
///
/// - Parameter elements: A random-access collection for which this table is being generated.
/// - Parameter stoppingOnFirstDuplicateValue: If true, check for duplicate values and stop inserting items when one is found.
/// - Returns: `(success, index)` where `success` is a boolean value indicating that every value in `elements` was successfully inserted. A false success indicates that duplicate elements have been found; in this case `index` points to the first duplicate value; otherwise `index` is set to `elements.endIndex`.
@inlinable
internal func fill<C: RandomAccessCollection>(
untilFirstDuplicateIn elements: C
) -> (success: Bool, end: C.Index)
where C.Element: Hashable {
assertMutable()
assert(elements.count <= capacity)
// Iterate over elements and insert their offset into the hash table.
var offset = 0
for index in elements.indices {
// Find the insertion position. We know that we're inserting a new item,
// so there is no need to compare it with any of the existing ones.
var it = bucketIterator(for: elements[index])
while let offset = it.currentValue {
guard elements[_offset: offset] != elements[index] else {
return (false, index)
}
it.advance()
}
it.currentValue = offset
offset += 1
}
return (true, elements.endIndex)
}
}
|