Skip to content

Instantly share code, notes, and snippets.

@numist
Created April 10, 2020 19:08
Show Gist options
  • Save numist/dabb74898774a2a5da8f9507896ca766 to your computer and use it in GitHub Desktop.
Save numist/dabb74898774a2a5da8f9507896ca766 to your computer and use it in GitHub Desktop.
Internal type FrontierBiNode from the Club diffing algorithm's work queue
/*
FrontierBiNode is a quadtree node with two additional restrictions:
1) all insertions to the southwest are dropped
2) insertions to the northeast are not allowed
Restriction 2) is satisfied by performing all insertions in descending
order of (x+y), resulting in a binary tree structure (all children lie to
the northwest or southeast) containing only points representing edit paths
that have made novel progress.
*/
private class FrontierBiNode {
// Non-private property accessors are all read-only
let e: EditTreeNode
private var _nw: FrontierBiNode? = nil
private var _se: FrontierBiNode? = nil
init(_ pe: EditTreeNode) {
e = pe
}
func insert(_ n: FrontierBiNode) -> Bool {
if n.e.discountedX == e.discountedX && n.e.discountedY == e.discountedY {
// Do nothing
return false
}
let child: ReferenceWritableKeyPath<FrontierBiNode, FrontierBiNode?>
switch (n.e.discountedX > e.discountedX, n.e.discountedY > e.discountedY) {
case (false, false):
// insertion to the southwest: parameter is superceded by this node
return false
case (false, true):
child = \._nw
case (true, false):
child = \._se
case (true, true):
// insertion to the northeast requires higher cardinality, which breaks edit path frontier guarantees
preconditionFailure("Insertion order violated (must be of descending cardinality)")
}
if let c = self[keyPath: child] {
return c.insert(n)
}
self[keyPath: child] = n
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment