summaryrefslogtreecommitdiff
path: root/nasmlib/rbtree.c
diff options
context:
space:
mode:
Diffstat (limited to 'nasmlib/rbtree.c')
-rw-r--r--nasmlib/rbtree.c36
1 files changed, 26 insertions, 10 deletions
diff --git a/nasmlib/rbtree.c b/nasmlib/rbtree.c
index 8b74c0c4..510f34b1 100644
--- a/nasmlib/rbtree.c
+++ b/nasmlib/rbtree.c
@@ -208,17 +208,36 @@ _rb_insert(struct rbtree *tree, struct rbtree *node)
return tree;
}
+struct rbtree *rb_first(const struct rbtree *tree)
+{
+ if (unlikely(!tree))
+ return NULL;
+
+ while (!(tree->m.flags & RBTREE_NODE_PRED))
+ tree = tree->m.left;
+
+ return (struct rbtree *)tree;
+}
+
+struct rbtree *rb_last(const struct rbtree *tree)
+{
+ if (unlikely(!tree))
+ return NULL;
+
+ while (!(tree->m.flags & RBTREE_NODE_SUCC))
+ tree = tree->m.right;
+
+ return (struct rbtree *)tree;
+}
+
struct rbtree *rb_prev(const struct rbtree *node)
{
struct rbtree *np = node->m.left;
if (node->m.flags & RBTREE_NODE_PRED)
return np;
-
- while (!(np->m.flags & RBTREE_NODE_SUCC))
- np = np->m.right;
-
- return np;
+ else
+ return rb_last(np);
}
struct rbtree *rb_next(const struct rbtree *node)
@@ -227,9 +246,6 @@ struct rbtree *rb_next(const struct rbtree *node)
if (node->m.flags & RBTREE_NODE_SUCC)
return np;
-
- while (!(np->m.flags & RBTREE_NODE_PRED))
- np = np->m.left;
-
- return np;
+ else
+ return rb_first(np);
}