Files
Vihan Bhargava e67460c942 Finish builder and
Key Changes:
 - Implement Equatable, Hashable for SubSequences
 - Implement SortedDictionary initializers
   - init(keysWithValues:) and init(sortedKeysAndValues:) with named and unnamed variants.
 - Expand benchmark suite for SortedDictionary
 - Commit _BTree.Builder

Minor Changes:
 - Update documentation
 - Expand test suits
 - Fix bug where SubSequences did not define subscript(bounds: Range<Index>)
 - _Node
   - Implement `depth` property for `_Node`
     - Packs both `depth`, `count`, and `capacity` within a single UInt64.
   - Implement `init(leftChild:seperator:rightChild:capacity:)` for `_Node`
   - Implement `concatenateWith(node:seperatedBy:)` to `_Node.UnsafeHandle`
   - Remove bad invariant check on `_Node.UnsafeHandle` deletion subroutines
 - _BTree
   - Add invariant checks as `_BTree.checkInvariants()`
   - Separate `_BTree.*capacity` into `*size` and `*capacity`
 - Make _FixedSizeArray top-level
   - Remove deque behavior.

Documentation:
 - Update complexity documentation style from 'n' to `self.count`

Testing:
 - Expand tests for SortedDictionary
 - Add tests for SortedDictionary.Keys
 - Add tests for _Node.join(...) routines
 - Add tests for _BTree.Builder
 - Restore checkBidirectionalCollection tests for _BTree
2021-08-15 00:22:39 -07:00

65 lines
2.2 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 {
/// Initialize a node from a list of key-value tuples.
@_spi(Testing)
public init<C: Collection>(
_keyValuePairs keyValuePairs: C,
children: [_Node<Key, Value>]? = nil,
capacity: Int
) where C.Element == Element {
precondition(keyValuePairs.count <= capacity, "Too many key-value pairs.")
self.init(withCapacity: capacity, isLeaf: children == nil)
self.update { handle in
let sortedKeyValuePairs = keyValuePairs.sorted(by: { $0.key < $1.key })
for (index, pair) in sortedKeyValuePairs.enumerated() {
let (key, value) = pair
handle.keys.advanced(by: index).initialize(to: key)
handle.values?.advanced(by: index).initialize(to: value)
}
if let children = children {
for (index, child) in children.enumerated() {
handle.children!.advanced(by: index).initialize(to: child)
}
handle.depth = handle[childAt: 0].storage.header.depth + 1
}
let totalChildElements = children?.reduce(0, { $0 + $1.read({ $0.subtreeCount }) })
handle.elementCount = keyValuePairs.count
handle.subtreeCount = keyValuePairs.count + (totalChildElements ?? 0)
}
}
/// Converts a node to a single array.
@_spi(Testing)
public func toArray() -> [Element] {
self.read { handle in
if handle.isLeaf {
return Array((0..<handle.elementCount).map { handle[elementAt: $0] })
} else {
var elements = [Element]()
for i in 0..<handle.elementCount {
elements.append(contentsOf: handle[childAt: i].toArray())
elements.append(handle[elementAt: i])
}
elements.append(contentsOf: handle[childAt: handle.elementCount].toArray())
return elements
}
}
}
}