Skip to content

Commit

Permalink
revert: use stop var in TraverseInRange
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Apr 24, 2024
1 parent 7083c48 commit 1a6845c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions examples/gno.land/p/demo/avl/node.gno
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ func (node *Node) TraverseInRange(start, end string, ascending bool, leavesOnly
if node == nil {
return false
}

afterStart := (start == "" || start < node.key)
startOrAfter := (start == "" || start <= node.key)
beforeEnd := false
Expand All @@ -344,36 +343,43 @@ func (node *Node) TraverseInRange(start, end string, ascending bool, leavesOnly
}

// Run callback per inner/leaf node.
stop := false
if (!node.IsLeaf() && !leavesOnly) ||
(node.IsLeaf() && startOrAfter && beforeEnd) {
if cb(node) {
return true
stop = cb(node)
if stop {
return stop
}
}

if node.IsLeaf() {
return false
return stop
}

if ascending {
// check lower nodes, then higher
if afterStart && node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb) {
return true
if afterStart {
stop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)
}
if beforeEnd && node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb) {
return true
if stop {
return stop
}
if beforeEnd {
stop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)
}
} else {
// check the higher nodes first
if beforeEnd && node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb) {
return true
if beforeEnd {
stop = node.getRightNode().TraverseInRange(start, end, ascending, leavesOnly, cb)
}
if afterStart && node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb) {
return true
if stop {
return stop
}
if afterStart {
stop = node.getLeftNode().TraverseInRange(start, end, ascending, leavesOnly, cb)
}
}

return false
return stop
}

// TraverseByOffset traverses all nodes, including inner nodes.
Expand Down

0 comments on commit 1a6845c

Please sign in to comment.