Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e21db14

Browse files
committed
Clarify the new Red-Black post-order traversal code a bit.
Coverity complained about the for(;;) loop, because it never actually iterated. It was used just to be able to use "break" to exit it early. I agree with Coverity, that's a bit confusing, so refactor the code to use if-else instead. While we're at it, use a local variable to hold the "current" node. That's shorter and clearer than referring to "iter->last_visited" all the time.
1 parent 6591f42 commit e21db14

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/backend/lib/rbtree.c

+24-22
Original file line numberDiff line numberDiff line change
@@ -781,27 +781,30 @@ static RBNode *
781781
rb_inverted_iterator(RBTreeIterator *iter)
782782
{
783783
RBNode *came_from;
784+
RBNode *current;
785+
786+
current = iter->last_visited;
784787

785788
loop:
786789
switch ((InvertedWalkNextStep) iter->next_step)
787790
{
788791
/* First call, begin from root */
789792
case NextStepBegin:
790-
iter->last_visited = iter->rb->root;
793+
current = iter->rb->root;
791794
iter->next_step = NextStepLeft;
792795
goto loop;
793796

794797
case NextStepLeft:
795-
while (iter->last_visited->left != RBNIL)
796-
iter->last_visited = iter->last_visited->left;
798+
while (current->left != RBNIL)
799+
current = current->left;
797800

798801
iter->next_step = NextStepRight;
799802
goto loop;
800803

801804
case NextStepRight:
802-
if (iter->last_visited->right != RBNIL)
805+
if (current->right != RBNIL)
803806
{
804-
iter->last_visited = iter->last_visited->right;
807+
current = current->right;
805808
iter->next_step = NextStepLeft;
806809
goto loop;
807810
}
@@ -810,30 +813,29 @@ rb_inverted_iterator(RBTreeIterator *iter)
810813
break;
811814

812815
case NextStepUp:
813-
for (;;)
816+
came_from = current;
817+
current = current->parent;
818+
if (current == NULL)
819+
{
820+
iter->is_over = true;
821+
break; /* end of iteration */
822+
}
823+
else if (came_from == current->right)
824+
{
825+
/* return current, then continue to go up */
826+
break;
827+
}
828+
else
814829
{
815-
came_from = iter->last_visited;
816-
iter->last_visited = iter->last_visited->parent;
817-
if (iter->last_visited == NULL)
818-
{
819-
iter->is_over = true;
820-
break; /* end of iteration */
821-
}
822-
823-
if (came_from == iter->last_visited->right)
824-
{
825-
/* return current, then continue to go up */
826-
break;
827-
}
828-
829830
/* otherwise we came from the left */
831+
Assert(came_from == current->left);
830832
iter->next_step = NextStepRight;
831833
goto loop;
832834
}
833-
break;
834835
}
835836

836-
return iter->last_visited;
837+
iter->last_visited = current;
838+
return current;
837839
}
838840

839841
/*

0 commit comments

Comments
 (0)