Files
swift-collections/Sources/SortedCollections/BTree/_Node.UnsafeHandle+CustomDebugStringConvertible.swift
T
Vihan Bhargava 8de0b4ff7a Implement primary SortedSet methods and various refactors.
Key Changes:
 - SortedSet
   - Implement Partial RangeReplaceableCollection
   - Implement ExpressibleByArrayLiteral
   - Implement init<S: Sequence>(_: S)
   - Implement Set methods:
     - update(with:) / insert(_:) / contains(_:)
 - SortedDictionary
   - Implement SortedDictionary.Values: BidirectionalCollection
   - Implement SortedDictionary.Keys: BidirectionalCollection
 - _BTree
   - Implement Partial RangeReplaceableCollection
   - Implement Sequence
     - Add high performance linear iterator

Minor Changes:
 - Benchmarks
   - Clean up benchmarks
 - _Node.UnsafeHandle
   - @freeze enum InsertionResult
 - _Node.Storage
   - Use UnsafeMutablePointer instead of ManagedBuffer

Refactors:
 - Various refactors for method signatures and documentation.
2021-08-09 19:13:50 -07:00

142 lines
4.5 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
//
//===----------------------------------------------------------------------===//
// MARK: CustomDebugStringConvertible
extension _Node.UnsafeHandle: CustomDebugStringConvertible {
#if DEBUG
private enum PrintPosition { case start, end, middle }
private func indentDescription(_ node: _Node<Key, Value>.UnsafeHandle, position: PrintPosition) -> String {
let label = "(\(node.elementCount)/\(node.subtreeCount))"
let spaces = String(repeating: " ", count: label.count)
let lines = describeNode(node).split(separator: "\n")
return lines.enumerated().map({ index, line in
var lineToInsert = line
let middle = (lines.count - 1) / 2
if index < middle {
if position == .start {
return " " + spaces + lineToInsert
} else {
return "┃ " + spaces + lineToInsert
}
} else if index > middle {
if position == .end {
return " " + spaces + lineToInsert
} else {
return "┃ " + spaces + lineToInsert
}
} else {
switch line[line.startIndex] {
case "╺": lineToInsert.replaceSubrange(...line.startIndex, with: "━")
case "┗": lineToInsert.replaceSubrange(...line.startIndex, with: "┻")
case "┏": lineToInsert.replaceSubrange(...line.startIndex, with: "┳")
case "┣": lineToInsert.replaceSubrange(...line.startIndex, with: "╋")
default: break
}
switch position {
case .start: return "┏━\(label)━" + lineToInsert
case .middle: return "┣━\(label)━" + lineToInsert
case .end: return "┗━\(label)━" + lineToInsert
}
}
}).joined(separator: "\n")
}
/// A textual representation of this instance, suitable for debugging.
private func describeNode(_ node: _Node<Key, Value>.UnsafeHandle) -> String {
if node.elementCount == 0 {
var result = ""
if !node.isLeaf {
node[childAt: 0].read { handle in
result += indentDescription(handle, position: .start) + "\n"
}
result += "┗━ << EMPTY >>"
} else {
result = "╺━ << EMPTY >>"
}
return result
}
var result = ""
for slot in 0..<node.elementCount {
if !node.isLeaf {
let child = node[childAt: slot]
let childDescription = child.read {
indentDescription($0, position: slot == 0 ? .start : .middle)
}
result += childDescription + "\n"
}
if node.isLeaf {
if node.elementCount == 1 {
result += "╺━ "
} else if slot == node.elementCount - 1 {
result += "┗━ "
} else if slot == 0 {
result += "┏━ "
} else {
result += "┣━ "
}
} else {
result += "┣━ "
}
debugPrint(node[keyAt: slot], terminator: ": ", to: &result)
debugPrint(node[valueAt: slot], terminator: "", to: &result)
if !node.isLeaf && slot == node.elementCount - 1 {
let childDescription = node[childAt: slot + 1].read {
indentDescription($0, position: .end)
}
result += "\n" + childDescription
}
result += "\n"
}
return result
}
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
return indentDescription(self, position: .end)
}
#else
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
var result = "Node<\(Key.self), \(Value.self)>(["
var first = true
for slot in 0..<self.elementCount {
if first {
first = false
} else {
result += ", "
}
result += "("
debugPrint(self[keyAt: slot], terminator: ", ", to: &result)
debugPrint(self[valueAt: slot], terminator: ")", to: &result)
}
result += "], "
if let children = self.children {
debugPrint(Array(UnsafeBufferPointer(
start: children,
count: self.childCount
)), terminator: ")", to: &result)
} else {
result += "[])"
}
return result
}
#endif // DEBUG
}