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

188 lines
4.6 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 _BTree {
@usableFromInline
internal struct SubSequence {
@usableFromInline
internal let _base: _BTree
@usableFromInline
internal var _startIndex: Index
@usableFromInline
internal var _endIndex: Index
@inlinable
@inline(__always)
internal init(base: _BTree, bounds: Range<Index>) {
self._base = base
self._startIndex = bounds.lowerBound
self._endIndex = bounds.upperBound
}
/// The underlying collection of the subsequence.
@inlinable
@inline(__always)
internal var base: _BTree { _base }
}
}
extension _BTree.SubSequence: Sequence {
@usableFromInline
internal typealias Element = _BTree.Element
@usableFromInline
internal struct Iterator: IteratorProtocol {
@usableFromInline
internal typealias Element = SubSequence.Element
@usableFromInline
internal var _iterator: _BTree.Iterator
@usableFromInline
internal var distanceRemaining: Int
@inlinable
@inline(__always)
internal init(_iterator: _BTree.Iterator, distance: Int) {
self._iterator = _iterator
self.distanceRemaining = distance
}
@inlinable
@inline(__always)
internal mutating func next() -> Element? {
if distanceRemaining == 0 {
return nil
} else {
distanceRemaining -= 1
return _iterator.next()
}
}
}
@inlinable
@inline(__always)
internal func makeIterator() -> Iterator {
let it = _BTree.Iterator(forTree: _base, startingAt: _startIndex)
let distance = _base.distance(from: _startIndex, to: _endIndex)
return Iterator(_iterator: it, distance: distance)
}
}
extension _BTree.SubSequence: BidirectionalCollection {
@usableFromInline
internal typealias Index = _BTree.Index
@usableFromInline
internal typealias SubSequence = Self
@inlinable
@inline(__always)
internal var startIndex: Index { _startIndex }
@inlinable
@inline(__always)
internal var endIndex: Index { _endIndex }
@inlinable
@inline(__always)
internal var count: Int { _base.distance(from: _startIndex, to: _endIndex) }
@inlinable
@inline(__always)
internal func distance(from start: Index, to end: Index) -> Int {
_base.distance(from: start, to: end)
}
@inlinable
@inline(__always)
internal func index(before i: Index) -> Index {
_base.index(before: i)
}
@inlinable
@inline(__always)
internal func formIndex(before i: inout Index) {
_base.formIndex(before: &i)
}
@inlinable
@inline(__always)
internal func index(after i: Index) -> Index {
_base.index(after: i)
}
@inlinable
@inline(__always)
internal func formIndex(after i: inout Index) {
_base.formIndex(after: &i)
}
@inlinable
@inline(__always)
internal func index(_ i: Index, offsetBy distance: Int) -> Index {
_base.index(i, offsetBy: distance)
}
@inlinable
@inline(__always)
internal func formIndex(_ i: inout Index, offsetBy distance: Int) {
_base.formIndex(&i, offsetBy: distance)
}
@inlinable
@inline(__always)
internal func index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
_base.index(i, offsetBy: distance, limitedBy: limit)
}
@inlinable
@inline(__always)
internal func formIndex(_ i: inout Index, offsetBy distance: Int, limitedBy limit: Self.Index) -> Bool {
_base.formIndex(&i, offsetBy: distance, limitedBy: limit)
}
@inlinable
@inline(__always)
internal subscript(position: Index) -> Element {
_failEarlyRangeCheck(position, bounds: startIndex..<endIndex)
return _base[position]
}
@inlinable
public subscript(bounds: Range<Index>) -> SubSequence {
_failEarlyRangeCheck(bounds, bounds: startIndex..<endIndex)
return _base[bounds]
}
// TODO: implement optimized `var indices`
@inlinable
@inline(__always)
public func _failEarlyRangeCheck(_ index: Index, bounds: Range<Index>) {
_base._failEarlyRangeCheck(index, bounds: bounds)
}
@inlinable
@inline(__always)
public func _failEarlyRangeCheck(_ range: Range<Index>, bounds: Range<Index>) {
_base._failEarlyRangeCheck(range, bounds: bounds)
}
}
// TODO: implement partial RangeReplaceableCollection methods