Created
April 10, 2020 19:08
-
-
Save numist/dabb74898774a2a5da8f9507896ca766 to your computer and use it in GitHub Desktop.
Internal type FrontierBiNode from the Club diffing algorithm's work queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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