Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite21db14

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

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

‎src/backend/lib/rbtree.c

Lines changed: 24 additions & 22 deletions
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
caseNextStepBegin:
790-
iter->last_visited=iter->rb->root;
793+
current=iter->rb->root;
791794
iter->next_step=NextStepLeft;
792795
gotoloop;
793796

794797
caseNextStepLeft:
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
gotoloop;
800803

801804
caseNextStepRight:
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
gotoloop;
807810
}
@@ -810,30 +813,29 @@ rb_inverted_iterator(RBTreeIterator *iter)
810813
break;
811814

812815
caseNextStepUp:
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+
elseif (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
gotoloop;
832834
}
833-
break;
834835
}
835836

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

839841
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp