Files
swift-collections/Sources/PersistentCollections/Node/_Node+Debugging.swift
T
Karoy Lorentey 0fe7f080c0 [PersistentCollections] offset → slot
(Internally) standardize on using the word “slot” to refer to an id
for a location within a contiguous storage buffer that can hold a
single item of some kind.

“Offset” is sometimes used in public APIs (to refer to integer
distances within collections, relative to some start position),
and using the same term here causes internal naming difficulties.

A “bucket” is therefore a more specific name for a slot within a
hash table.
2022-09-16 01:30:06 -07:00

114 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
import _CollectionsUtilities
extension _Node {
@usableFromInline
internal func dump(
iterationOrder: Bool = false,
limit: Int = Int.max,
firstPrefix: String = "",
restPrefix: String = "",
depth: Int = 0
) {
read {
$0.dump(
iterationOrder: iterationOrder,
limit: limit,
extra: "count: \(count), ",
firstPrefix: firstPrefix,
restPrefix: restPrefix,
depth: depth)
}
}
}
extension _Node.Storage {
@usableFromInline
final internal func dump(iterationOrder: Bool = false) {
UnsafeHandle.read(self) { $0.dump(iterationOrder: iterationOrder) }
}
}
extension _Node.UnsafeHandle {
internal func _itemString(at slot: _Slot) -> String {
let item = self[item: slot]
let hash = _Hash(item.key).description
return "hash: \(hash), key: \(item.key), value: \(item.value)"
}
@usableFromInline
internal func dump(
iterationOrder: Bool = false,
limit: Int = .max,
extra: String = "",
firstPrefix: String = "",
restPrefix: String = "",
depth: Int = 0
) {
var firstPrefix = firstPrefix
var restPrefix = restPrefix
if iterationOrder && depth == 0 {
firstPrefix += "@"
restPrefix += "@"
}
if iterationOrder {
firstPrefix += " "
}
print("""
\(firstPrefix)\(isCollisionNode ? "CollisionNode" : "Node")(\
at: \(_addressString(for: _header)), \
\(extra)\
byteCapacity: \(byteCapacity), \
freeBytes: \(bytesFree))
""")
guard limit > 0 else { return }
if iterationOrder {
for slot in stride(from: .zero, to: itemEnd, by: 1) {
print(" \(restPrefix)[\(slot)] \(_itemString(at: slot))")
}
for slot in stride(from: .zero, to: childEnd, by: 1) {
self[child: slot].dump(
iterationOrder: true,
limit: limit - 1,
firstPrefix: " \(restPrefix).\(slot)",
restPrefix: " \(restPrefix).\(slot)",
depth: depth + 1)
}
}
else if isCollisionNode {
for slot in stride(from: .zero, to: itemEnd, by: 1) {
print("\(restPrefix)[\(slot)] \(_itemString(at: slot))")
}
} else {
var itemSlot: _Slot = .zero
var childSlot: _Slot = .zero
for b in 0 ..< UInt(_Bitmap.capacity) {
let bucket = _Bucket(b)
let bucketStr = "#\(String(b, radix: _Bitmap.capacity, uppercase: true))"
if itemMap.contains(bucket) {
print("\(restPrefix) \(bucketStr) \(_itemString(at: itemSlot))")
itemSlot = itemSlot.next()
} else if childMap.contains(bucket) {
self[child: childSlot].dump(
iterationOrder: false,
limit: limit - 1,
firstPrefix: "\(restPrefix) \(bucketStr) ",
restPrefix: "\(restPrefix) ",
depth: depth + 1)
childSlot = childSlot.next()
}
}
}
}
}