aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/rbtree.c
diff options
context:
space:
mode:
authorFlorin Coras <fcoras@cisco.com>2019-04-19 18:50:34 -0700
committerFlorin Coras <fcoras@cisco.com>2019-04-19 19:20:07 -0700
commitf7cda7a9bc399258959084154263c42c5a91e030 (patch)
treef1122bcc6da7b0d7a849092e564479ea124c9cd1 /src/vppinfra/rbtree.c
parent51cbbd2282f39ff1b64781c73efbbe3b332ed3d9 (diff)
rbtree: add successor and predecessor functions
Change-Id: I6934beaf5c08bae2d4f0bd3a6bb811810407c1f9 Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/vppinfra/rbtree.c')
-rw-r--r--src/vppinfra/rbtree.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/vppinfra/rbtree.c b/src/vppinfra/rbtree.c
index d99367a2c6f..3770c2304ff 100644
--- a/src/vppinfra/rbtree.c
+++ b/src/vppinfra/rbtree.c
@@ -227,6 +227,40 @@ rb_tree_max_subtree (rb_tree_t * rt, rb_node_t * x)
return x;
}
+rb_node_t *
+rb_tree_successor (rb_tree_t * rt, rb_node_t * x)
+{
+ rb_node_t *y;
+
+ if (x->right != RBTREE_TNIL_INDEX)
+ return rb_tree_min_subtree (rt, rb_node_right (rt, x));
+
+ y = rb_node_parent (rt, x);
+ while (!rb_node_is_tnil (rt, y) && y->right == rb_node_index (rt, x))
+ {
+ x = y;
+ y = rb_node_parent (rt, y);
+ }
+ return y;
+}
+
+rb_node_t *
+rb_tree_predecessor (rb_tree_t * rt, rb_node_t * x)
+{
+ rb_node_t *y;
+
+ if (x->left != RBTREE_TNIL_INDEX)
+ return rb_tree_max_subtree (rt, rb_node_left (rt, x));
+
+ y = rb_node_parent (rt, x);
+ while (!rb_node_is_tnil (rt, y) && y->left == rb_node_index (rt, x))
+ {
+ x = y;
+ y = rb_node_parent (rt, y);
+ }
+ return y;
+}
+
static inline void
rb_tree_transplant (rb_tree_t * rt, rb_node_t * u, rb_node_t * v)
{