Skip to content

Commit

Permalink
Merge branch 'feature/rbtree-absraction' into feature/membership-package
Browse files Browse the repository at this point in the history
  • Loading branch information
Menno Pruijssers committed Dec 16, 2016
2 parents fb81d89 + a4d8488 commit a0468ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions hashring/rbtree.go
Expand Up @@ -261,28 +261,28 @@ func (n *redBlackNode) search(key keytype) (valuetype, bool) {
return nil, false
}

// traverseUntil traverses the nodes in the tree in-order invoking `traverse` for each node. If the
// traverse function returns `false`, traversal is stopped and no more nodes will be
// traverseWhile traverses the nodes in the tree in-order invoking the `condition`-function argument for each node.
// If the condition-function returns `false` traversal is stopped and no more nodes will be
// visited. Returns `true` if all nodes are visited; `false` if not.
func (n *redBlackNode) traverseUntil(traverse func(*redBlackNode) bool) bool {
func (n *redBlackNode) traverseWhile(condition func(*redBlackNode) bool) bool {
if n == nil {
// the end of the tree does not signal the end of walking, but we can't
// walk this node (nil) nor left or right anymore
return true
}

// walk left first
if !n.left.traverseUntil(traverse) {
if !n.left.traverseWhile(condition) {
// stop if walker indicated to break
return false
}
// now visit this node
if !traverse(n) {
if !condition(n) {
// stop if walker indicated to break
return false
}
// lastly visit right
if !n.right.traverseUntil(traverse) {
if !n.right.traverseWhile(condition) {
// stop if walker indicated to break
return false
}
Expand Down
4 changes: 2 additions & 2 deletions hashring/rbtree_test.go
Expand Up @@ -650,7 +650,7 @@ func TestTraverseOrder(t *testing.T) {

var last keytype

tree.root.traverseUntil(func(n *redBlackNode) bool {
tree.root.traverseWhile(func(n *redBlackNode) bool {
current := n.key
if last != nil {
assert.True(t, current.Compare(last) > 0, "expected walk to walk the nodes in natural order as dictated by the Compare function")
Expand All @@ -673,7 +673,7 @@ func TestTraverseEscape(t *testing.T) {

var visited []int

tree.root.traverseUntil(func(n *redBlackNode) bool {
tree.root.traverseWhile(func(n *redBlackNode) bool {
number := int(n.key.(treeTestInt))
visited = append(visited, number)
// stop at node 5, it should exit on a left, right and visiting node,
Expand Down

0 comments on commit a0468ac

Please sign in to comment.