Skip to content

Commit 09f4e65

Browse files
authored
Lower branches in red-black tree again (#59)
This commit is similar with the one applied in insert_node, an indirect pointer can be used instead of checking left or right child nodes.
1 parent ff46dad commit 09f4e65

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

src/map.c

+14-26
Original file line numberDiff line numberDiff line change
@@ -580,38 +580,26 @@ void map_find(map_t obj, map_iter_t *it, void *key)
580580
}
581581

582582
/* Basically a repeat of insert */
583-
map_node_t *cur = obj->head;
583+
map_node_t **indirect = &obj->head;
584584

585585
/* binary search */
586-
while (1) {
587-
int res = obj->comparator(key, cur->key);
588-
if (res == 0) /* If the key matches, we hit the target */
586+
while (*indirect) {
587+
int res = obj->comparator(key, (*indirect)->key);
588+
if (res == 0)
589589
break;
590-
591-
if (res < 0) {
592-
if (!cur->left) {
593-
cur = NULL;
594-
break;
595-
}
596-
cur = cur->left;
597-
} else {
598-
if (!cur->right) {
599-
cur = NULL;
600-
break;
601-
}
602-
cur = cur->right;
603-
}
590+
indirect = res < 0 ? &(*indirect)->left : &(*indirect)->right;
604591
}
605592

606-
if (cur) {
607-
it->node = cur;
608-
609-
/* Generate a "prev" as well */
610-
map_iter_t tmp = *it;
611-
map_prev(obj, &tmp);
612-
it->prev = tmp.node;
613-
} else
593+
if (!*indirect) {
614594
it->node = NULL;
595+
return;
596+
}
597+
it->node = *indirect;
598+
599+
/* Generate a "prev" as well */
600+
map_iter_t tmp = *it;
601+
map_prev(obj, &tmp);
602+
it->prev = tmp.node;
615603
}
616604

617605
bool map_empty(map_t obj)

0 commit comments

Comments
 (0)