[PersistentCollections] Internal doc updates

This commit is contained in:
Karoy Lorentey committed 2022-09-16 14:49:37 -07:00
1 parent 52e3ea6ca9
commit 6cf128e6ea
9 files changed
+195 -45

No files matched your search

@@ -9,6 +9,14 @@
//
//===----------------------------------------------------------------------===//
/// A collection of slot values logically addressing a particular node in a
/// hash tree. The collection is (logically) extended with zero slots up to
/// the maximum depth of the tree -- to unambiguously address a single node,
/// this therefore needs to be augmented with a `_Level` value.
///
/// This construct can only be used to identify a particular node in the tree;
/// it does not necessarily have room to include an item offset in the addressed
/// node. (See `_Path` if you need to address a particular item.)
@usableFromInline
@frozen
struct _AncestorSlots {
@@ -34,6 +42,9 @@ extension _AncestorSlots {
}
extension _AncestorSlots {
/// Return or set the slot value at the specified level.
/// If this is used to mutate the collection, then the original value
/// on the given level must be zero.
@inlinable @inline(__always)
internal subscript(_ level: _Level) -> _Slot {
get {
@@ -47,6 +58,15 @@ extension _AncestorSlots {
}
}
/// Clear the slot at the specified level, by setting it to zero.
@inlinable
internal mutating func clear(_ level: _Level) {
guard level.shift < UInt.bitWidth else { return }
path &= ~(_Bucket.bitMask &<< level.shift)
}
/// Truncate this path to the specified level.
/// Slots at or beyond the specified level are cleared.
@inlinable
internal func truncating(to level: _Level) -> _AncestorSlots {
assert(level.shift <= UInt.bitWidth)
@@ -54,18 +74,16 @@ extension _AncestorSlots {
return _AncestorSlots(path & ((1 &<< level.shift) &- 1))
}
@inlinable
internal mutating func clear(_ level: _Level) {
guard level.shift < UInt.bitWidth else { return }
path &= ~(_Bucket.bitMask &<< level.shift)
}
/// Returns true if this path contains non-zero slots at or beyond the
/// specified level, otherwise returns false.
@inlinable
internal func hasDataBelow(_ level: _Level) -> Bool {
guard level.shift < UInt.bitWidth else { return false }
return (path &>> level.shift) != 0
}
/// Compares this path to `other` up to but not including the specified level.
/// Returns true if the path prefixes compare equal, otherwise returns false.
@inlinable
internal func isEqual(to other: Self, upTo level: _Level) -> Bool {
if level.isAtRoot { return true }
@@ -9,9 +9,16 @@
//
//===----------------------------------------------------------------------===//
/// Identifies a particular level within the hash tree.
///
/// Hash trees have a maximum depth of ⎡`UInt.bitWidth / _Bucket.bitWidth`⎤, so
/// the level always fits in an `UInt8` value.
@usableFromInline
@frozen
internal struct _Level {
/// The bit position within a hash value that begins the hash slice that is
/// associated with this level. For collision nodes, this can be larger than
/// `UInt.bitWidth`.
@usableFromInline
internal var _shift: UInt8
@@ -43,6 +50,9 @@ extension _Level {
_Level(shift: 0)
}
/// The bit position within a hash value that begins the hash slice that is
/// associated with this level. For collision nodes, this can be larger than
/// `UInt.bitWidth`.
@inlinable @inline(__always)
internal var shift: UInt { UInt(truncatingIfNeeded: _shift) }
@@ -10,6 +10,16 @@
//===----------------------------------------------------------------------===//
extension _Node {
/// An unsafe view of the data stored inside a node in the hash tree, hiding
/// the mechanics of accessing storage from the code that uses it.
///
/// 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.
///
/// A handle can be either read-only or mutable, depending on the method used
/// to access it. In debug builds, methods that modify data trap at runtime if
/// they're called on a read-only view.
@usableFromInline
@frozen
internal struct UnsafeHandle {
@@ -10,6 +10,18 @@
//===----------------------------------------------------------------------===//
extension _RawNode {
/// 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 `_Node.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 {
@@ -9,6 +9,12 @@
//
//===----------------------------------------------------------------------===//
/// 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 _RawNode {
@@ -9,7 +9,13 @@
//
//===----------------------------------------------------------------------===//
/// Identifies a position within a contiguous storage region within a hash tree
/// node. Hash tree nodes have two storage regions, one for items and one for
/// children; the same `_Slot` type is used to refer to positions within both.
///
/// We use the term "slot" to refer to internal storage entries, to avoid
/// confusion with terms that sometimes appear in public API, such as
/// "index", "position" or "offset".
@usableFromInline
@frozen
internal struct _Slot {
@@ -9,6 +9,9 @@
//
//===----------------------------------------------------------------------===//
/// The storage header in a hash tree node. This includes data about the
/// current size and capacity of the node's storage region, as well as
/// information about the currently occupied hash table buckets.
@usableFromInline
@frozen
internal struct _StorageHeader {
@@ -11,6 +11,12 @@
import _CollectionsUtilities
/// An unsafe, unowned, type-erased reference to a hash tree node; essentially
/// just a lightweight wrapper around `Unmanaged<_RawStorage>`.
///
/// Because such a reference may outlive the underlying object, use sites must
/// be extraordinarily careful to never dereference an invalid `_UnmanagedNode`.
/// Doing so results in undefined behavior.
@usableFromInline
@frozen
internal struct _UnmanagedNode {
@@ -24,6 +30,12 @@ internal struct _UnmanagedNode {
}
extension _UnmanagedNode: Equatable {
/// Indicates whether two unmanaged node references are equal.
///
/// This function is safe to call even if one or both of its arguments are
/// invalid -- however, it may incorrectly return true in this case.
/// (This can happen when a destroyed node's memory region is later reused for
/// a newly created node.)
@inlinable
internal static func ==(left: Self, right: Self) -> Bool {
left.ref.toOpaque() == right.ref.toOpaque()
@@ -9,6 +9,30 @@
//
//===----------------------------------------------------------------------===//
/// A non-owning, mutable construct representing a path to an item or child node
/// within a hash tree (or the virtual slot addressing the end of the
/// items or children region within a node).
///
/// Path values provide mutating methods to freely navigate around in the tree,
/// including basics such as descending into a child node, ascending to a
/// parent or selecting a particular item within the current node; as well as
/// more complicated methods such as finding the next/previous item in a
/// preorder walk of the tree.
///
/// Paths are, for the most part, represented by a series of slot values
/// identifying a particular branch within each level in the tree up to and
/// including the final node on the path.
///
/// However, to speed up common operations, path values also include a single
/// `_UnmanagedNode` reference to their final node. This reference does not
/// keep the targeted node alive -- it is the use site's responsibility to
/// ensure that the path is still valid before calling most of its operations.
///
/// Note: paths only have a direct reference to their final node. This means
/// that ascending to the parent node requires following the path from the root
/// node down. (Paths could also store references to every node alongside them
/// in a fixed-size array; this would speed up walking over the tree, but it
/// would considerably embiggen the size of the path construct.)
@usableFromInline
internal struct _UnsafePath {
@usableFromInline
@@ -139,10 +163,8 @@ extension _UnsafePath {
/// Returns true if this path addresses an item in the tree; otherwise returns
/// false.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@inlinable @inline(__always)
internal var isOnItem: Bool {
// Note: this may be true even if nodeSlot == itemCount (insertion paths).
@@ -153,6 +175,9 @@ extension _UnsafePath {
/// valid item. Such paths can represent the place of an item that might be
/// inserted later; they do not occur while simply iterating over existing
/// items.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal var isPlaceholder: Bool {
_isItem && nodeSlot.value == node.itemCount
}
@@ -160,10 +185,8 @@ extension _UnsafePath {
/// Returns true if this path addresses a node in the tree; otherwise returns
/// false.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal var isOnChild: Bool {
!_isItem && nodeSlot.value < node.childCount
}
@@ -171,14 +194,14 @@ extension _UnsafePath {
/// Returns true if this path addresses an empty slot within a node in a tree;
/// otherwise returns false.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal var isOnNodeEnd: Bool {
!_isItem && nodeSlot.value == node.childCount
}
/// Returns true if this path addresses an item on the leftmost branch of the
/// tree, or the empty slot at the end of an empty tree.
@inlinable
internal var isOnLeftmostItem: Bool {
// We are on the leftmost item in the tree if we are currently
@@ -191,25 +214,25 @@ extension _UnsafePath {
/// Returns an unmanaged reference to the child node this path is currently
/// addressing.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal var currentChild: _UnmanagedNode {
assert(isOnChild)
return node.unmanagedChild(at: nodeSlot)
}
/// Returns the chid slot in this path corresponding to the specified level.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal func childSlot(at level: _Level) -> _Slot {
assert(level < self.level)
return ancestors[level]
}
/// Returns the slot of the currently addressed item.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@inlinable @inline(__always)
internal var currentItemSlot: _Slot {
assert(isOnItem)
@@ -221,10 +244,8 @@ extension _UnsafePath {
/// Positions this path on the item with the specified slot within its
/// current node.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@inlinable
internal mutating func selectItem(at slot: _Slot) {
// As a special exception, this allows slot to equal the item count.
@@ -238,10 +259,8 @@ extension _UnsafePath {
/// Positions this path on the child with the specified slot within its
/// current node, without descending into it.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@inlinable
internal mutating func selectChild(at slot: _Slot) {
// As a special exception, this allows slot to equal the child count.
@@ -253,10 +272,8 @@ extension _UnsafePath {
/// Positions this path on the empty slot at the end of its current node.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@usableFromInline
@_effects(releasenone)
internal mutating func selectEnd() {
@@ -266,13 +283,11 @@ extension _UnsafePath {
/// Descend onto the first path within the currently selected child.
/// (Either the first item if it exists, or the first child. If the child
/// is an empty node (which should not happen in a hash tree), then this
/// is an empty node (which should not happen in a valid hash tree), then this
/// selects the empty slot at the end of it.
///
/// - Note: This method needs to resolve the unmanaged node reference
/// that is stored in the path. It is up to the caller to ensure this will
/// never get called when the node is no longer valid; otherwise this will
/// trigger undefined behavior.
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@usableFromInline
@_effects(releasenone)
internal mutating func descend() {
@@ -283,6 +298,14 @@ extension _UnsafePath {
self.level = level.descend()
}
/// Ascend to the nearest ancestor for which the `test` predicate returns
/// true. Because paths do not contain references to every node on them,
/// you need to manually supply a valid reference to the root node. This
/// method visits every node between the root and the current final node on
/// the path.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal mutating func ascendToNearestAncestor(
under root: _RawNode,
where test: (_UnmanagedNode, _Slot) -> Bool
@@ -303,7 +326,13 @@ extension _UnsafePath {
self = best
return true
}
/// Ascend to the parent node of this path. Because paths do not contain
/// references to every node on them, you need to manually supply a valid
/// reference to the root node. This method visits every node
/// between the root and the current final node on the path.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal mutating func ascend(under root: _RawNode) {
assert(!self.level.isAtRoot)
var n = root.unmanaged
@@ -324,6 +353,13 @@ extension _UnsafePath {
}
extension _UnsafePath {
/// Given a path that is on an item, advance it to the next item within its
/// current node, and return true. If there is no next item, position the path
/// on the first child, and return false. If there is no children, position
/// the path on the node's end position, and return false.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
mutating func selectNextItem() -> Bool {
assert(isOnItem)
nodeSlot = nodeSlot.next()
@@ -333,6 +369,12 @@ extension _UnsafePath {
return false
}
/// Given a path that is on a child node, advance it to the next child within
/// its current node, and return true. If there is no next child, position
/// the path on the node's end position, and return false.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
mutating func selectNextChild() -> Bool {
assert(!isOnItem)
let childEnd = node.childEnd
@@ -343,6 +385,13 @@ extension _UnsafePath {
}
extension _UnsafePath {
/// If this path addresses a child node, descend into the leftmost item
/// within the subtree under it (i.e., the first item that would be visited
/// by a preorder walk within that subtree). Do nothing if the path addresses
/// an item or the end position.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
@usableFromInline
@_effects(releasenone)
internal mutating func descendToLeftMostItem() {
@@ -351,6 +400,13 @@ extension _UnsafePath {
}
}
/// Given a path addressing a child node, descend into the rightmost item
/// within the subtree under it (i.e., the last item that would be visited
/// by a preorder walk within that subtree). Do nothing if the path addresses
/// an item or the end position.
///
/// - Note: It is undefined behavior to call this on a path that is no longer
/// valid.
internal mutating func descendToRightMostItem() {
assert(isOnChild)
while true {
@@ -364,6 +420,9 @@ extension _UnsafePath {
selectItem(at: itemEnd.previous())
}
/// Find the next item in a preorder walk in the tree following the currently
/// addressed item, and return true. Return false and do nothing if the
/// path does not currently address an item.
@usableFromInline
@_effects(releasenone)
internal mutating func findSuccessorItem(under root: _RawNode) -> Bool {
@@ -388,6 +447,9 @@ extension _UnsafePath {
return true
}
/// Find the previous item in a preorder walk in the tree preceding the
/// currently addressed position, and return true.
/// Return false if there is no previous item.
@usableFromInline
@_effects(releasenone)
internal mutating func findPredecessorItem(under root: _RawNode) -> Bool {
@@ -427,6 +489,11 @@ extension _UnsafePath {
}
extension _RawNode {
/// Return the integer position of the item addressed by the given path
/// within a preorder walk of the tree. If the path addresses the end
/// position, then return the number of items in the tree.
///
/// This method must only be called on the root node.
internal func preorderPosition(
_ level: _Level, of path: _UnsafePath
) -> Int {
@@ -446,6 +513,10 @@ extension _RawNode {
return path.nodeSlot.value
}
/// Return the number of steps between two paths within a preorder walk of the
/// tree. The two paths must not address a child node.
///
/// This method must only be called on the root node.
@usableFromInline
@_effects(releasenone)
internal func distance(
@@ -534,6 +605,8 @@ extension _UnmanagedNode {
}
extension _Node {
/// Return the path to the given key in this tree if it exists; otherwise
/// return nil.
@inlinable
internal func path(
to key: Key, hash: _Hash