Skip to content

Commit 821e3e3

Browse files
committed
Add implementation of deficiency handling algorithms for removals.
Key Changes: - Implement deficiency correction methods - Remove inlining from _Node.ensureUnique to better inline closures - Add new computed properties to _Node.UnsafeHandle Minor changes - Added missing mutability checks to certain _Node.UnsafeHandle methods - Remove unneeded _Node.UnsafeHandle.findValue(key:) method - Allow _Node.UnsafeHandle.removeElement(at:) to be called on non-leaves - Expand doc comments and fix typos
1 parent 9cf2cda commit 821e3e3

File tree

6 files changed

+412
-75
lines changed

6 files changed

+412
-75
lines changed

Sources/SortedCollections/BTree/_BTree.swift

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ extension _BTree {
165165
}
166166

167167
/// Removes the key-value pair corresponding to the first found instance of the key.
168+
///
168169
/// This may not be the first instance of the key. This is marginally more efficient for trees
169170
/// that do not have duplicates.
170171
///
@@ -177,55 +178,18 @@ extension _BTree {
177178
internal mutating func removeAny(key: Key) -> Element? {
178179
invalidateIndices()
179180

180-
func deleteWithin(node handle: Node.UnsafeHandle) -> Element? {
181-
let slot = handle.firstSlot(for: key)
182-
183-
// if slot < handle.numElements && handle[keyAt: slot] == key {
184-
// // We have found the key
185-
// if handle.isLeaf {
186-
// // Check if removing the element will make the element undersized
187-
// if handle.numElements <= handle.minCapacity {
188-
// // Undersized node
189-
// return handle.
190-
// } else {
191-
// return handle.removeElement(at: slot)
192-
// }
193-
// } else {
194-
// if handle.numElements <= handle.minCapacity {
195-
// // Undersized node.
196-
// } else {
197-
// // Swap with the
198-
// }
199-
// }
200-
// } else {
201-
// if handle.isLeaf {
202-
// // If we're in a leaf node and didn't find the key, it does
203-
// // not exist.
204-
// return nil
205-
// } else {
206-
// // Sanity-check
207-
// assert(slot < handle.numChildren, "Attempt to remove from invalid child.")
208-
// assert()
209-
//
210-
// let childElement = handle[childAt: slot].update { deleteWithin(node: $0) }
211-
//
212-
// //
213-
// }
214-
// }
215-
216-
return nil
217-
}
218-
219181
// TODO: Don't create CoW copy until needed.
220-
return self.root.update { deleteWithin(node: $0) }
182+
// TODO: Handle root deletion
183+
return self.root.update { $0.removeAny(key: key) }
221184
}
222185
}
223186

224187
// MARK: Read Operations
225188
extension _BTree {
226-
/// Returns the value corresponding to the first found instance of the key. This may
227-
/// not be the first instance of the key. This is marginally more efficient for trees
228-
/// that do not have duplicates.
189+
/// Returns the value corresponding to the first found instance of the key.
190+
///
191+
/// This may not be the first instance of the key. This is marginally more efficient
192+
/// for trees that do not have duplicates.
229193
///
230194
/// - Parameter key: The key to search for
231195
/// - Returns: `nil` if the key was not found. Otherwise, the previous value.

Sources/SortedCollections/BTree/_Node.Splinter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ extension _Node {
2828
@usableFromInline
2929
internal var rightChild: _Node<Key, Value>
3030

31+
/// Converts the splinter object to a node.
32+
/// - Parameters:
33+
/// - node: The node generating the splinter. Becomes the returned
34+
/// node's left child.
35+
/// - capacity: The desired capacity of the new node.
36+
/// - Returns: A new node of `capacity` with a single element.
3137
@inlinable
3238
@inline(__always)
3339
internal func toNode(from node: _Node<Key, Value>, withCapacity capacity: Int) -> _Node {

0 commit comments

Comments
 (0)