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
```
146 lines
4.5 KiB
Swift
146 lines
4.5 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)
|
|
extension _HTable {
|
|
/// The minimum hash table scale.
|
|
@_alwaysEmitIntoClient
|
|
package static var minimumScale: UInt8 {
|
|
@_effects(readnone)
|
|
@_transparent
|
|
get {
|
|
4
|
|
}
|
|
}
|
|
|
|
/// The maximum hash table scale.
|
|
@_alwaysEmitIntoClient
|
|
package static var maximumScale: UInt8 {
|
|
@_effects(readnone)
|
|
@_transparent
|
|
get {
|
|
Swift.min(UInt8(truncatingIfNeeded: Int.bitWidth) &- 1, 56)
|
|
}
|
|
}
|
|
|
|
/// The maximum number of items for which we do not create a hash table.
|
|
@usableFromInline
|
|
package static var maximumUnhashedCount: Int {
|
|
@_effects(readnone)
|
|
get {
|
|
maximumCapacity(forScale: 0)
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
package static func wordCount(forScale scale: UInt8) -> Int {
|
|
guard scale > 0 else { return 0 }
|
|
let shift = Swift.max(UInt(scale), Word.wordShift) - Word.wordShift
|
|
return 1 &<< shift
|
|
}
|
|
|
|
/// The numerator of the maximum hash table load factor.
|
|
@_transparent
|
|
internal static var _maxLFNum: UInt {
|
|
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
|
|
3
|
|
#else
|
|
7
|
|
#endif
|
|
}
|
|
|
|
/// The denominator of the maximum hash table load factor.
|
|
@_transparent
|
|
internal static var _maxLFDenom: UInt {
|
|
#if COLLECTIONS_NO_ROBIN_HOOD_HASHING
|
|
4
|
|
#else
|
|
8
|
|
#endif
|
|
}
|
|
|
|
/// The numerator of the minimum hash table load factor.
|
|
@_transparent
|
|
internal static var _minLFNum: UInt { 1 }
|
|
|
|
/// The denominator of minimum hash table load factor.
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
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)
|
|
package static func minimumCapacity(forScale scale: UInt8) -> Int {
|
|
guard scale >= minimumScale else { return 0 }
|
|
precondition(scale <= maximumScale)
|
|
let bucketCount: UInt = 1 &<< scale
|
|
return Int(bucketCount * _minLFNum / _minLFDenom)
|
|
}
|
|
|
|
/// The maximum number of items that can be held in a hash table of the given scale.
|
|
@usableFromInline
|
|
@_effects(readnone)
|
|
package static func maximumCapacity(forScale scale: UInt8) -> Int {
|
|
let scale = Swift.max(scale, minimumScale &- 1)
|
|
let bucketCount: UInt = 1 &<< scale
|
|
return Int(bucketCount * _maxLFNum / _maxLFDenom)
|
|
}
|
|
|
|
|
|
/// The minimum hash table scale that can hold the specified number of elements.
|
|
@usableFromInline
|
|
@_effects(readnone)
|
|
package static func minimumScale(forCapacity capacity: Int) -> UInt8 {
|
|
guard capacity > maximumUnhashedCount else { return 0 }
|
|
let capacity = UInt(truncatingIfNeeded: Swift.max(capacity, 1))
|
|
// Calculate the minimum number of entries we need to allocate to satisfy
|
|
// the maximum load factor.
|
|
let minimumEntries = Swift.max(
|
|
(capacity * _maxLFDenom + _maxLFNum - 1) / _maxLFNum, // (capacity / maxLoadFactor).rounded(.up)
|
|
capacity + 1)
|
|
// The actual number of entries we need to allocate is the lowest power of
|
|
// two greater than or equal to the minimum entry count. Calculate its
|
|
// exponent.
|
|
let scale = (Swift.max(minimumEntries, 2) - 1)._binaryLogarithm() + 1
|
|
assert(scale >= minimumScale && scale <= maximumScale)
|
|
// The scale is the exponent corresponding to the bucket count.
|
|
assert(self.maximumCapacity(forScale: UInt8(scale)) >= capacity)
|
|
return UInt8(truncatingIfNeeded: scale)
|
|
}
|
|
|
|
@usableFromInline
|
|
@_effects(readnone)
|
|
package static func dynamicStorageParameters(
|
|
minimumCapacity: Int
|
|
) -> (scale: UInt8, capacity: Int) {
|
|
let maximumUnhashedCount = self.maximumUnhashedCount
|
|
if minimumCapacity <= maximumUnhashedCount {
|
|
let c = Swift.min(
|
|
minimumCapacity._roundUpToPowerOfTwo(),
|
|
maximumUnhashedCount)
|
|
return (0, c)
|
|
}
|
|
let scale = minimumScale(forCapacity: minimumCapacity)
|
|
let capacity = maximumCapacity(forScale: scale)
|
|
assert(capacity >= minimumCapacity)
|
|
let storageCapacity: Int = 1 &<< scale
|
|
assert(capacity < storageCapacity) // We need at least one empty bucket.
|
|
return (scale, capacity)
|
|
}
|
|
}
|
|
#endif
|