|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Collections open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +extension _BTree { |
| 13 | + /// Filters a B-Tree on a predicate, returning a new tree. |
| 14 | + /// |
| 15 | + /// - Complexity: O(`n log n`) where `n` is the number of key-value pairs in the |
| 16 | + /// sorted dictionary. |
| 17 | + @inlinable |
| 18 | + @inline(__always) |
| 19 | + public func filter( |
| 20 | + _ isIncluded: (Element) throws -> Bool |
| 21 | + ) rethrows -> _BTree { |
| 22 | + // TODO: optimize implementation to O(n) |
| 23 | + var newTree: _BTree = _BTree() |
| 24 | + for element in self where try isIncluded(element) { |
| 25 | + newTree.setAnyValue(element.value, forKey: element.key) |
| 26 | + } |
| 27 | + return newTree |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + // MARK: Last Removal |
| 32 | + |
| 33 | + /// Removes the first element of a tree, if it exists. |
| 34 | + /// |
| 35 | + /// - Returns: The moved first element of the tree. |
| 36 | + @inlinable |
| 37 | + @discardableResult |
| 38 | + internal mutating func popLast() -> Element? { |
| 39 | + invalidateIndices() |
| 40 | + |
| 41 | + if self.count == 0 { return nil } |
| 42 | + |
| 43 | + let removedElement = self.root.update { $0.popLastElement() } |
| 44 | + self._balanceRoot() |
| 45 | + return removedElement |
| 46 | + } |
| 47 | + |
| 48 | + @inlinable |
| 49 | + @inline(__always) |
| 50 | + @discardableResult |
| 51 | + public mutating func removeLast() -> Element { |
| 52 | + if let value = self.popLast() { |
| 53 | + return value |
| 54 | + } else { |
| 55 | + preconditionFailure("Can't remove last element from an empty collection") |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @inlinable |
| 60 | + @inline(__always) |
| 61 | + public mutating func removeLast(_ k: Int) { |
| 62 | + assert(0 <= k && k < self.count, "Can't remove more items from a collection than it contains") |
| 63 | + for _ in 0..<k { |
| 64 | + self.removeLast() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // MARK: First Removal |
| 69 | + /// Removes the first element of a tree, if it exists. |
| 70 | + /// |
| 71 | + /// - Returns: The moved first element of the tree. |
| 72 | + @inlinable |
| 73 | + @inline(__always) |
| 74 | + @discardableResult |
| 75 | + internal mutating func popFirst() -> Element? { |
| 76 | + invalidateIndices() |
| 77 | + |
| 78 | + if self.count == 0 { return nil } |
| 79 | + |
| 80 | + let removedElement = self.root.update { $0.popFirstElement() } |
| 81 | + self._balanceRoot() |
| 82 | + return removedElement |
| 83 | + } |
| 84 | + |
| 85 | + @inlinable |
| 86 | + @inline(__always) |
| 87 | + @discardableResult |
| 88 | + public mutating func removeFirst() -> Element { |
| 89 | + if let value = self.popFirst() { |
| 90 | + return value |
| 91 | + } else { |
| 92 | + preconditionFailure("Can't remove first element from an empty collection") |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @inlinable |
| 97 | + @inline(__always) |
| 98 | + public mutating func removeFirst(_ k: Int) { |
| 99 | + assert(0 <= k && k < self.count, "Can't remove more items from a collection than it contains") |
| 100 | + for _ in 0..<k { |
| 101 | + self.removeFirst() |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: Offset Removal |
| 106 | + /// Removes the element of a tree at a given offset. |
| 107 | + /// |
| 108 | + /// - Parameter offset: the offset which must be in-bounds. |
| 109 | + /// - Returns: The moved element of the tree |
| 110 | + @inlinable |
| 111 | + @inline(__always) |
| 112 | + @discardableResult |
| 113 | + internal mutating func remove(at offset: Int) -> Element { |
| 114 | + invalidateIndices() |
| 115 | + let removedElement = self.root.update { $0.remove(at: offset) } |
| 116 | + self._balanceRoot() |
| 117 | + return removedElement |
| 118 | + } |
| 119 | + |
| 120 | + @inlinable |
| 121 | + @inline(__always) |
| 122 | + internal mutating func removeAll() { |
| 123 | + invalidateIndices() |
| 124 | + // TODO: potentially use empty storage class. |
| 125 | + self.root = _Node(withCapacity: _BTree.defaultLeafCapacity, isLeaf: true) |
| 126 | + } |
| 127 | + |
| 128 | + // MARK: Index Removal |
| 129 | + /// Removes the element of a tree at a given index. |
| 130 | + /// |
| 131 | + /// - Parameter index: a valid index of the tree, not `endIndex` |
| 132 | + /// - Returns: The moved element of the tree |
| 133 | + @inlinable |
| 134 | + @inline(__always) |
| 135 | + @discardableResult |
| 136 | + internal mutating func remove(at index: Index) -> Element { |
| 137 | + invalidateIndices() |
| 138 | + guard let path = index.path else { preconditionFailure("Index out of bounds.") } |
| 139 | + return self.remove(at: path.offset) |
| 140 | + } |
| 141 | + |
| 142 | + /// Removes the elements in the specified subrange from the collection. |
| 143 | + @inlinable |
| 144 | + internal mutating func removeSubrange(_ bounds: Range<Index>) { |
| 145 | + guard let startPath = bounds.lowerBound.path else { preconditionFailure("Index out of bounds.") } |
| 146 | + guard let _ = bounds.upperBound.path else { preconditionFailure("Index out of bounds.") } |
| 147 | + |
| 148 | + let rangeSize = self.distance(from: bounds.lowerBound, to: bounds.upperBound) |
| 149 | + let startOffset = startPath.offset |
| 150 | + |
| 151 | + for _ in 0..<rangeSize { |
| 152 | + self.remove(at: startOffset) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments