Files
swift-collections/Sources/SortedCollections/BTree/_Node.Splinter.swift
T
Vihan Bhargava 98f278545d Continue applying refactors and optimizations.
Key Changes:
 - _BTree.init() dynamically computes capacity from stride
 - _BTree.UnsafePath uses Unmanaged +0 references.

Minor Changes:
 - Fix _BTree.Iterator bug on single-leaf trees.
2021-08-09 19:13:50 -07:00

57 lines
2.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 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 _Node {
/// Represents the result of a overfilled node's split.
@usableFromInline
internal struct Splinter {
@inlinable
@inline(__always)
internal init(element: Element, rightChild: _Node<Key, Value>) {
self.element = element
self.rightChild = rightChild
}
/// The former median element which should be propogated upward.
@usableFromInline
internal let element: Element
/// The right product of the node split.
@usableFromInline
internal var rightChild: _Node<Key, Value>
/// Converts the splinter object to a node.
/// - Parameters:
/// - node: The node generating the splinter. Becomes the returned
/// node's left child.
/// - capacity: The desired capacity of the new node.
/// - Returns: A new node of `capacity` with a single element.
@inlinable
@inline(__always)
internal func toNode(leftChild: _Node<Key, Value>, capacity: Int) -> _Node {
var newNode = _Node(withCapacity: capacity, isLeaf: false)
newNode.update { handle in
handle.keys.initialize(to: element.key)
handle.values.initialize(to: element.value)
handle.children.unsafelyUnwrapped.initialize(to: leftChild)
handle.children.unsafelyUnwrapped.advanced(by: 1).initialize(to: self.rightChild)
handle.elementCount = 1
handle.subtreeCount = 1 +
leftChild.storage.header.subtreeCount +
self.rightChild.storage.header.subtreeCount
}
return newNode
}
}
}