Files
swift-collections/Sources/PersistentCollections/Node/_RawNode+UnsafeHandle.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

85 lines
1.9 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
//
//===----------------------------------------------------------------------===//
extension _RawNode {
@usableFromInline
@frozen
internal struct UnsafeHandle {
@usableFromInline
internal let _header: UnsafePointer<_StorageHeader>
@usableFromInline
internal let _memory: UnsafeRawPointer
@inlinable
internal init(
_ header: UnsafePointer<_StorageHeader>,
_ memory: UnsafeRawPointer
) {
self._header = header
self._memory = memory
}
}
}
extension _RawNode.UnsafeHandle {
@inline(__always)
internal var isCollisionNode: Bool {
_header.pointee.isCollisionNode
}
@inline(__always)
internal var hasChildren: Bool {
_header.pointee.hasChildren
}
@inline(__always)
internal var childCount: Int {
_header.pointee.childCount
}
@inline(__always)
internal var childEnd: _Slot {
_header.pointee.childEnd
}
@inline(__always)
internal var hasItems: Bool {
_header.pointee.hasItems
}
@inline(__always)
internal var itemCount: Int {
_header.pointee.itemCount
}
@inline(__always)
internal var itemEnd: _Slot {
_header.pointee.itemEnd
}
@inline(__always)
internal var _childrenStart: UnsafePointer<_RawNode> {
_memory.assumingMemoryBound(to: _RawNode.self)
}
internal subscript(child slot: _Slot) -> _RawNode {
unsafeAddress {
assert(slot < childEnd)
return _childrenStart + slot.value
}
}
internal var _children: UnsafeBufferPointer<_RawNode> {
UnsafeBufferPointer(start: _childrenStart, count: childCount)
}
}