Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mb/early traversal termination #7

Merged
merged 3 commits into from
Jan 25, 2022
Merged

Conversation

MBauerDC
Copy link
Contributor

Allow early termination of tree-traversals in visitors via throwing EarlyTraversalTerminationException in visitor-function and catching it in the caller to TreeNodeVisitor->visit[Order]Order.

Can be used like:

$someIdToTerminateOn = 'foo';
$exists = false;
$earlyTerminatingFunc = static function(TreeNode $n) use ($someIdToTerminateOn, $exists) {
    if ($someIdToTerminateOn === $n->getId()) {
        $exists = true;
        throw new EarlyTraversalTerminationException();
    }
} 
$visitor = new GenericTreeNodeVisitor($earlyTerminatingFunc);
try {
    $visitor->visitPreOrder($tree);
} catch (EarlyTraversalTerminationException) {}

For large trees and/or visitor-functions which take a lot of time per visited node, this will significantly increase performance.

NOTE: Long term, a better solution is to refactor the methods to return null|EarlyTraversalTermination (refactor exception to empty response class) - but that requires chaning the signature, which should be for another task.

@MBauerDC MBauerDC merged commit 8c072eb into master Jan 25, 2022
@MBauerDC MBauerDC deleted the mb/early-traversal-termination branch January 25, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant