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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 - 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
//
//===----------------------------------------------------------------------===//
/// A type-erased node in a hash tree. This doesn't know about the user data
/// stored in the tree, but it has access to subtree counts and it can be used
/// to freely navigate within the tree structure.
///
/// This construct is powerful enough to implement APIs such as `index(after:)`,
/// `distance(from:to:)`, `index(_:offsetBy:)` in non-generic code.
@usableFromInline
@frozen
internal struct _RawHashNode {
@usableFromInline
internal var storage: _RawHashStorage
@usableFromInline
internal var count: Int
@inlinable
internal init(storage: _RawHashStorage, count: Int) {
self.storage = storage
self.count = count
}
}
extension _RawHashNode {
@inline(__always)
internal func read<R>(_ body: (UnsafeHandle) -> R) -> R {
storage.withUnsafeMutablePointers { header, elements in
body(UnsafeHandle(header, UnsafeRawPointer(elements)))
}
}
}
extension _RawHashNode {
@inlinable @inline(__always)
internal var unmanaged: _UnmanagedHashNode {
_UnmanagedHashNode(storage)
}
@inlinable @inline(__always)
internal func isIdentical(to other: _UnmanagedHashNode) -> Bool {
other.ref.toOpaque() == Unmanaged.passUnretained(storage).toOpaque()
}
}
extension _RawHashNode {
@usableFromInline
internal func validatePath(_ path: _UnsafePath) {
var l = _HashLevel.top
var n = self.unmanaged
while l < path.level {
let slot = path.ancestors[l]
precondition(slot < n.childrenEndSlot)
n = n.unmanagedChild(at: slot)
l = l.descend()
}
precondition(n == path.node)
if path._isItem {
precondition(path.nodeSlot < n.itemsEndSlot)
} else {
precondition(path.nodeSlot <= n.childrenEndSlot)
}
}
}
|