mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
- Allow bitmap visualization to be included/omitted as needed
- Visualize chain length distribution for both successful and unsuccessful lookups
Check out this sample test output for Robin Hood hashing:
```
⌗ large@11 1536/1792/2048, maxProbeLength: 11, totalProbeLength: 3712, maxOccupiedRun: 68
├╴[00000000] □□■■■■□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■■■■■∙□□□■□■□□∙□□□■■□■■∙■■■■■■■■
├╴[00000064] □□■□□■■■∙■■■■□■□□∙□■■■■■■■∙■■■■■■■■∙■■□□□□■□∙■□■□■■■■∙■■■■■■□□∙■■■■■■■■
├╴[00000128] ■■■■■■■■∙■■■□□□■□∙■■□■□■■□∙□■■■■■□■∙■■■■■■■■∙□■■■■■■■∙■■■■□■□■∙■□■□□■□□
├╴[00000192] ■□■■□■■■∙■■■■□■□■∙□□□□□■■□∙■■■■■■■■∙■■■□■■□■∙□■□■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000256] ■■■■■■□□∙■■■■■■■□∙□■■■■■■□∙□■■■■■■□∙□□■■□□■□∙□□□■■■■□∙□■■■■□■■∙■■■■■■■■
├╴[00000320] ■■■■■■■■∙□■■■■■■■∙■■■■■■■□∙□■■□□■□□∙□□□■■□□■∙■■■■■□■□∙■□■■■■■□∙■■■■■■■■
├╴[00000384] ■■■■■■■■∙■■■■■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙□■■□□■□■∙□□■■□■■■
├╴[00000448] ■■■■□■■■∙■■■□■■■□∙□□□■□■■■∙■■■□□■■■∙■■■□□□□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00000512] ■■■■■□□□∙□□■■□■■■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■□∙□■■■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000576] □□□■■■□■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■■∙■■□■■■■■∙■■■■■■■■∙■■□□■□□□∙■■■□□□■□
├╴[00000640] □■■□□■■□∙□□□□■■■■∙□■■□■■■□∙□■■□■■■■∙■■■■■■■■∙■□□■■■■■∙■■■■■□□□∙■■■■■■■□
├╴[00000704] ■■■■■■■□∙■■■■□□□■∙■■□■□■■■∙■■■■■■■■∙■■■■□■■■∙□□□■□■■■∙□□□■□□□□∙□□□■□■■■
├╴[00000768] □■■■■■□■∙□■■□□□■■∙■■■■■■■□∙■■■■■■■■∙■■■■■■■□∙□■■■□■■□∙■□■■□■■■∙■■■■□■□■
├╴[00000832] ■□□■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■□□∙□□■■■■■■∙□□■■■■■■∙■■■■■■■■∙□■□■■■■■
├╴[00000896] ■■■■■■■■∙■■■■■□□■∙■■■□□□□■∙■■□□■■■■∙■□□■■■■□∙■■■□□■■□∙□□■■□■■■∙□■□□■■■■
├╴[00000960] ■■■■■□■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■□∙□■□■■■■■∙■■■■□□■■
├╴[00001024] ■■■■■■□□∙■■■■□■■□∙■■■■■■■□∙□□□■■■□□∙■■■□■■□■∙■□■■□□□■∙■■■■■■■■∙□□□□□□□□
├╴[00001088] ■■□□□□■□∙■■□□□□□■∙■□□■■■■■∙■■■□□□□■∙■■■■■■■■∙■■■■■■■■∙■■■■□□□□∙■■□■□■■■
├╴[00001152] ■■■□□■■□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■□■■■∙■□■■□■■□∙□■■■■□■■
├╴[00001216] ■■■■■■■■∙■■□■□■■■∙■■■■■■■■∙□□■■□□□■∙□□□□□□□□∙□□■■■■■■∙■■■■■■■■∙■■■□□■■■
├╴[00001280] ■■■■■■■□∙■■□■■■■■∙■■■■■■□□∙■□□■■■■■∙■■■■■■■■∙■□□■□■□■∙■■■■■■■■∙■■■■■■■■
├╴[00001344] ■■■■■■■■∙■□■□■■□□∙■■■■□■□□∙□■■□■□□■∙□■□■■■■■∙■■■□■■■□∙□□■■■■■■∙■■■■■■■■
├╴[00001408] ■■■■■■■■∙■■■■■■■■∙■■■■■□□■∙■■■□■■■■∙■■□■■■■■∙■■■□■■■■∙■■□□■□□□∙■□■■■■■■
├╴[00001472] ■■■■■□■□∙■■□□□□□■∙□□□□□■■□∙■□□■■□■□∙■■■■■■■■∙■■■■■■■■∙□□□□■■■■∙■□■□□■■■
├╴[00001536] ■■■■■■■■∙□□□■■□■□∙□■■□■■□□∙□■□■■□□□∙■■■■■□□■∙■□■■■■■■∙■■■■■■■■∙■□□■□□□□
├╴[00001600] ■■■■■■■■∙■■■■□□■□∙□□■■□■■■∙■■■■■■■□∙□■□□■■□■∙■□■■■□■■∙■■■■■■■■∙□□■■■■■■
├╴[00001664] ■■■■■■■■∙□■■■□□■■∙■■■□■□■□∙■■■■■■□■∙■■■□■■■■∙■■■■■■■□∙□□□□■■■■∙■■■■□■■■
├╴[00001728] ■■■■■■■■∙■□□■■■■■∙■■□□■■□□∙■■■■■■■■∙■■■□□□■■∙□□■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00001792] ■■■■■■■■∙■■■■■■■■∙■□□□■■□■∙□□■■□□□□∙□■■□■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■
├╴[00001856] ■■■■■■■□∙■■■■■■■□∙□■□■□■■□∙■□□□■■■■∙■□■■□■■□∙□□■□■□■■∙■□□□□■■■∙■■■■■■■■
├╴[00001920] ■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□□■□■□□
└╴[00001984] ■□□■□□■■∙□■■■■■■□∙□□□□■■■■∙■□□□■■■■∙■■■■■■■■∙■■□■□■■■∙■■■□■■□□∙■□□■■□■□
Chain length distribution (for successful lookups):
AVG: 2.416
1: **************************************************************************** 549
2: ******************************************************** 408
3: ************************************* 264
4: ******************** 142
5: ************** 96
6: ******* 51
7: *** 17
8: * 4
9: * 1
10: * 3
11: * 1
Chain length distribution (for unsuccessful lookups):
AVG: 4.428
0: **************************************************************************** 511
1: **************************************** 269
2: ***************************** 191
3: ******************* 129
4: ***************** 111
5: ************** 94
6: ************* 85
7: *********** 70
8: ********* 60
9: ********* 55
10: ******** 50
11: ************************************************************** 422
```
232 lines
4.9 KiB
Swift
232 lines
4.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !COLLECTIONS_SINGLE_MODULE
|
|
import InternalCollectionsUtilities
|
|
#endif
|
|
|
|
#if compiler(>=6.2)
|
|
@usableFromInline
|
|
@frozen
|
|
package struct _HTable: ~Copyable {
|
|
@usableFromInline
|
|
package typealias Word = _UnsafeBitSet._Word
|
|
|
|
@_alwaysEmitIntoClient
|
|
package var _count: Int
|
|
|
|
@_alwaysEmitIntoClient
|
|
package var _capacity: Int
|
|
|
|
@_alwaysEmitIntoClient
|
|
package let _bitmap: UnsafeMutablePointer<Word>?
|
|
|
|
@_alwaysEmitIntoClient
|
|
package var _totalProbeLength: Int
|
|
|
|
@_alwaysEmitIntoClient
|
|
package var _maxProbeLength: Int
|
|
|
|
@_alwaysEmitIntoClient
|
|
package let scale: UInt8
|
|
|
|
@inlinable
|
|
internal init(
|
|
_capacity: Int,
|
|
scale: UInt8
|
|
) {
|
|
assert(
|
|
_capacity >= Self.minimumCapacity(forScale: scale)
|
|
&& _capacity <= Self.maximumCapacity(forScale: scale))
|
|
self._count = 0
|
|
self._capacity = _capacity
|
|
self._totalProbeLength = 0
|
|
self._maxProbeLength = 0
|
|
self.scale = scale
|
|
if scale == 0 {
|
|
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
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
internal init(capacity: Int) {
|
|
assert(capacity >= 0)
|
|
self.init(
|
|
_capacity: capacity,
|
|
scale: Self.minimumScale(forCapacity: capacity))
|
|
}
|
|
|
|
@inlinable
|
|
internal init(minimumCapacity: Int) {
|
|
precondition(minimumCapacity >= 0, "Capacity must be nonnegative")
|
|
let p = Self.dynamicStorageParameters(minimumCapacity: minimumCapacity)
|
|
self.init(_capacity: p.capacity, scale: p.scale)
|
|
}
|
|
|
|
|
|
@_alwaysEmitIntoClient
|
|
deinit {
|
|
_bitmap?.deallocate()
|
|
}
|
|
}
|
|
|
|
extension _HTable {
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var isSmall: Bool {
|
|
_bitmap == nil
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var count: Int {
|
|
_count
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var capacity: Int {
|
|
_capacity
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var isEmpty: Bool {
|
|
_count == 0
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var isFull: Bool {
|
|
_count == capacity
|
|
}
|
|
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var wordCount: Int {
|
|
if isSmall { return 0 }
|
|
return Word.wordCount(forBitCount: bucketCount)
|
|
}
|
|
}
|
|
|
|
extension _HTable {
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var startBucket: Bucket {
|
|
Bucket(offset: 0)
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var endBucket: Bucket {
|
|
Bucket(offset: bucketCount)
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var storageCapacity: Int {
|
|
if isSmall { return capacity }
|
|
return 1 &<< scale
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package var bucketCount: UInt {
|
|
UInt(bitPattern: storageCapacity)
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func isValid(_ bucket: Bucket) -> Bool {
|
|
bucket._offset < bucketCount
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func wrapBucket(after bucket: inout Bucket) {
|
|
assert(isValid(bucket))
|
|
bucket._offset &+= 1
|
|
if bucket._offset == bucketCount {
|
|
bucket._offset = 0
|
|
}
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func formBucket(after bucket: inout Bucket) {
|
|
assert(isValid(bucket))
|
|
bucket._offset &+= 1
|
|
}
|
|
|
|
@inlinable
|
|
package var bitmap: Bitmap {
|
|
@_lifetime(borrow self)
|
|
get {
|
|
Bitmap(table: self)
|
|
}
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func isOccupied(_ bucket: Bucket) -> Bool {
|
|
if isSmall { return bucket._offset < _count }
|
|
return bitmap.isOccupied(bucket)
|
|
}
|
|
}
|
|
|
|
extension _HTable {
|
|
@_transparent
|
|
@inlinable
|
|
package func idealBucket(forHashValue hashValue: Int) -> Bucket {
|
|
let hashValue = UInt(truncatingIfNeeded: hashValue)
|
|
return Bucket(offset: hashValue & (bucketCount &- 1))
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func probeLength(
|
|
from start: Bucket,
|
|
to end: Bucket
|
|
) -> Int {
|
|
if start.offset <= end.offset {
|
|
return Int(end._offset &- start._offset &+ 1)
|
|
}
|
|
return Int(bucketCount &- start._offset &+ end._offset)
|
|
}
|
|
|
|
@_transparent
|
|
@inlinable
|
|
package func probeLength(
|
|
forHashValue hashValue: Int,
|
|
in bucket: Bucket
|
|
) -> Int {
|
|
let ideal = idealBucket(forHashValue: hashValue)
|
|
return probeLength(from: ideal, to: bucket)
|
|
}
|
|
}
|
|
|
|
extension _HTable {
|
|
@inlinable
|
|
package mutating func clear() {
|
|
self._count = 0
|
|
self.bitmap.clearAll()
|
|
self._totalProbeLength = 0
|
|
}
|
|
}
|
|
#endif
|