Skip to content

Commit

Permalink
Refactor to use getters
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh committed Aug 25, 2023
1 parent 17604fb commit 977d7dd
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 468 deletions.
2 changes: 1 addition & 1 deletion Sources/ARTreeModule/InternalNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ extension InternalNode {
let slf: Node4<Spec> = selfRef as! Node4<Spec>
var node: any InternalNode<Spec> = newValue.toInternalNode()
node.partialBytes.shiftRight()
node.partialBytes[0] = slf.withBody { k, _ in k[0] }
node.partialBytes[0] = slf.keys[0]
node.partialLength += 1
}
return .replaceWith(newValue)
Expand Down
93 changes: 45 additions & 48 deletions Sources/ARTreeModule/Node+StringConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,15 @@ extension Node4: NodePrettyPrinter {
func prettyPrint(depth: Int) -> String {
let addr = Const.testPrintAddr ? " \(_addressString(for: self.rawNode.buf))" : " "
var output = "Node4\(addr){childs=\(count), partial=\(partial)}\n"
withBody { keys, childs in
for idx in 0..<count {
let key = keys[idx]
let last = idx == count - 1
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: idx)!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}

for idx in 0..<count {
let key = keys[idx]
let last = idx == count - 1
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: idx)!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}
}

Expand All @@ -133,16 +132,15 @@ extension Node16: NodePrettyPrinter {
func prettyPrint(depth: Int) -> String {
let addr = Const.testPrintAddr ? " \(_addressString(for: self.rawNode.buf))" : " "
var output = "Node16\(addr){childs=\(count), partial=\(partial)}\n"
withBody { keys, childs in
for idx in 0..<count {
let key = keys[idx]
let last = idx == count - 1
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: idx)!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}

for idx in 0..<count {
let key = keys[idx]
let last = idx == count - 1
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: idx)!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}
}

Expand All @@ -156,20 +154,19 @@ extension Node48: NodePrettyPrinter {
let addr = Const.testPrintAddr ? " \(_addressString(for: self.rawNode.buf))" : " "
var output = "Node48\(addr){childs=\(count), partial=\(partial)}\n"
var total = 0
withBody { keys, childs in
for (key, slot) in keys.enumerated() {
if slot >= 0xFF {
continue
}

total += 1
let last = total == count
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: Int(slot))!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}

for (key, slot) in keys.enumerated() {
if slot >= 0xFF {
continue
}

total += 1
let last = total == count
output += indent(depth, last: last)
output += String(key) + ": "
output += child(at: Int(slot))!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}
}

Expand All @@ -183,22 +180,22 @@ extension Node256: NodePrettyPrinter {
let addr = Const.testPrintAddr ? " \(_addressString(for: self.rawNode.buf))" : " "
var output = "Node256\(addr){childs=\(count), partial=\(partial)}\n"
var total = 0
withBody { childs in
for (key, child) in childs.enumerated() {
if child == nil {
continue
}

total += 1
let last = total == count
output += indent(depth, last: last)
output += String(key) + ": "
output += child!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}

for (key, child) in childs.enumerated() {
if child == nil {
continue
}

total += 1
let last = total == count
output += indent(depth, last: last)
output += String(key) + ": "
output += child!.prettyPrint(depth: depth + 1, with: Spec.self)
if !last {
output += "\n"
}
}

return output
}
}
Loading

0 comments on commit 977d7dd

Please sign in to comment.