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

68 lines
1.7 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
//
//===----------------------------------------------------------------------===//
@usableFromInline
@frozen
internal struct _RawNode {
@usableFromInline
internal var storage: _RawStorage
@usableFromInline
internal var count: Int
@inlinable
internal init(storage: _RawStorage, count: Int) {
self.storage = storage
self.count = count
}
}
extension _RawNode {
@inline(__always)
internal func read<R>(_ body: (UnsafeHandle) -> R) -> R {
storage.withUnsafeMutablePointers { header, elements in
body(UnsafeHandle(header, UnsafeRawPointer(elements)))
}
}
}
extension _RawNode {
@inline(__always)
internal var unmanaged: _UnmanagedNode {
_UnmanagedNode(storage)
}
@inlinable @inline(__always)
internal func isIdentical(to other: _UnmanagedNode) -> Bool {
other.ref.toOpaque() == Unmanaged.passUnretained(storage).toOpaque()
}
}
extension _RawNode {
@usableFromInline
internal func validatePath(_ path: _UnsafePath) {
var l = _Level.top
var n = self.unmanaged
while l < path.level {
let slot = path.ancestors[l]
precondition(slot < n.childEnd)
n = n.unmanagedChild(at: slot)
l = l.descend()
}
precondition(n == path.node)
if path._isItem {
precondition(path.nodeSlot < n.itemEnd)
} else {
precondition(path.nodeSlot <= n.childEnd)
}
}
}