mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +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
```
151 lines
3.4 KiB
Swift
151 lines
3.4 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
|
|
import ContainersPreview
|
|
#endif
|
|
|
|
#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
|
|
@available(SwiftStdlib 5.0, *)
|
|
@frozen
|
|
public struct RigidSet<Element: GeneralizedHashable & ~Copyable>: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
package var _members: UnsafeMutablePointer<Element>?
|
|
|
|
@_alwaysEmitIntoClient
|
|
package var _table: _HTable
|
|
|
|
@inlinable
|
|
@_transparent
|
|
package init(
|
|
_table: consuming _HTable
|
|
) {
|
|
assert(_table.isEmpty)
|
|
if _table.capacity == 0 {
|
|
self._members = nil
|
|
} else {
|
|
self._members = .allocate(capacity: _table.storageCapacity)
|
|
}
|
|
self._table = _table
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
deinit {
|
|
_dispose()
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
internal func _dispose() {
|
|
if !isEmpty {
|
|
let storage = _memberBuf
|
|
var it = _table.makeBucketIterator()
|
|
while let range = it.nextOccupiedRegion() {
|
|
storage._extracting(unchecked: range._offsets).deinitialize()
|
|
}
|
|
}
|
|
_members?.deallocate()
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidSet where Element: ~Copyable {
|
|
@inlinable
|
|
@inline(__always)
|
|
public var count: Int {
|
|
_assumeNonNegative(_table.count)
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public var capacity: Int {
|
|
_assumeNonNegative(_table.capacity)
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public var isEmpty: Bool {
|
|
count == 0
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public var isFull: Bool {
|
|
count == capacity
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public var freeCapacity: Int {
|
|
_assumeNonNegative(capacity &- count)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidSet where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
internal var _memberBuf: UnsafeMutableBufferPointer<Element> {
|
|
.init(start: _members, count: Int(bitPattern: _table.bucketCount))
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
internal func _memberPtr(
|
|
at bucket: _HTable.Bucket
|
|
) -> UnsafeMutablePointer<Element> {
|
|
assert(_table.isValid(bucket))
|
|
return _members.unsafelyUnwrapped.advanced(by: bucket.offset)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidSet where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
internal var _seed: Int {
|
|
#if COLLECTIONS_DETERMINISTIC_HASHING
|
|
Int(_table.scale)
|
|
#else
|
|
Int(bitPattern: _members)
|
|
#endif
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
internal borrowing func _hashValue(
|
|
at bucket: _HTable.Bucket
|
|
) -> Int {
|
|
assert(bucket.offset >= 0 && bucket.offset < _table.storageCapacity)
|
|
return _hashValue(for: _members.unsafelyUnwrapped[bucket.offset])
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
internal borrowing func _hashValue(
|
|
for item: borrowing Element
|
|
) -> Int {
|
|
item._rawHashValue(seed: _seed)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidSet where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package mutating func _take() -> Self {
|
|
let r = self
|
|
self = .init()
|
|
return r
|
|
}
|
|
}
|
|
|
|
#endif
|