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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
extension _RawHashNode {
/// An unsafe, non-generic view of the data stored inside a node in the
/// hash tree, hiding the mechanics of accessing storage from the code that
/// uses it.
///
/// This is the non-generic equivalent of `_HashNode.UnsafeHandle`, sharing some
/// of its functionality, but it only provides read-only access to the tree
/// structure (incl. subtree counts) -- it doesn't provide any ways to mutate
/// the underlying data or to access user payload.
///
/// Handles do not own the storage they access -- it is the client's
/// responsibility to ensure that handles (and any pointer values generated
/// by them) do not escape the closure call that received them.
@usableFromInline
@frozen
internal struct UnsafeHandle {
@usableFromInline
internal let _header: UnsafePointer<_HashNodeHeader>
@usableFromInline
internal let _memory: UnsafeRawPointer
@inlinable
internal init(
_ header: UnsafePointer<_HashNodeHeader>,
_ memory: UnsafeRawPointer
) {
self._header = header
self._memory = memory
}
}
}
extension _RawHashNode.UnsafeHandle {
@inlinable @inline(__always)
static func read<R>(
_ node: _UnmanagedHashNode,
_ body: (Self) throws -> R
) rethrows -> R {
try node.ref._withUnsafeGuaranteedRef { storage in
try storage.withUnsafeMutablePointers { header, elements in
try body(Self(header, UnsafeRawPointer(elements)))
}
}
}
}
extension _RawHashNode.UnsafeHandle {
@inline(__always)
internal var isCollisionNode: Bool {
_header.pointee.isCollisionNode
}
@inline(__always)
internal var collisionHash: _Hash {
assert(isCollisionNode)
return _memory.load(as: _Hash.self)
}
@inline(__always)
internal var hasChildren: Bool {
_header.pointee.hasChildren
}
@inline(__always)
internal var childCount: Int {
_header.pointee.childCount
}
@inline(__always)
internal var childrenEndSlot: _HashSlot {
_header.pointee.childrenEndSlot
}
@inline(__always)
internal var hasItems: Bool {
_header.pointee.hasItems
}
@inline(__always)
internal var itemCount: Int {
_header.pointee.itemCount
}
@inline(__always)
internal var itemsEndSlot: _HashSlot {
_header.pointee.itemsEndSlot
}
@inline(__always)
internal var _childrenStart: UnsafePointer<_RawHashNode> {
_memory.assumingMemoryBound(to: _RawHashNode.self)
}
internal subscript(child slot: _HashSlot) -> _RawHashNode {
unsafeAddress {
assert(slot < childrenEndSlot)
return _childrenStart + slot.value
}
}
internal var children: UnsafeBufferPointer<_RawHashNode> {
UnsafeBufferPointer(start: _childrenStart, count: childCount)
}
}
|