diff options
Diffstat (limited to 'src/vppinfra')
-rw-r--r-- | src/vppinfra/rbtree.c | 34 | ||||
-rw-r--r-- | src/vppinfra/rbtree.h | 2 |
2 files changed, 36 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) { diff --git a/src/vppinfra/rbtree.h b/src/vppinfra/rbtree.h index ace07ac5622..73180d1d54c 100644 --- a/src/vppinfra/rbtree.h +++ b/src/vppinfra/rbtree.h @@ -54,6 +54,8 @@ u32 rb_tree_n_nodes (rb_tree_t * rt); rb_node_t *rb_tree_min_subtree (rb_tree_t * rt, rb_node_t * x); rb_node_t *rb_tree_max_subtree (rb_tree_t * rt, rb_node_t * x); rb_node_t *rb_tree_search_subtree (rb_tree_t * rt, rb_node_t * x, u32 key); +rb_node_t *rb_tree_successor (rb_tree_t * rt, rb_node_t * x); +rb_node_t *rb_tree_predecessor (rb_tree_t * rt, rb_node_t * x); static inline rb_node_index_t rb_node_index (rb_tree_t * rt, rb_node_t * n) |