Add search min/max for ranges
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 30 May 2011 17:49:28 +0000 (13:49 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 30 May 2011 17:49:28 +0000 (13:49 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
urcu-rbtree.c
urcu/rcurbtree.h

index 824883df25e91976f68ed0c3b94958ee509563a1..5ad7763f91ecdef8236fbe8d71f0d17ecd40c31f 100644 (file)
@@ -389,10 +389,11 @@ struct rcu_rbtree_node* rcu_rbtree_search(struct rcu_rbtree *rbtree,
                                          void *k)
 {
        x = rcu_dereference(x);
+       int comp;
 
-       while (!rcu_rbtree_is_nil(x) && k != x->key) {
+       while (!rcu_rbtree_is_nil(x) && (comp = rbtree->comp(k, x->key)) != 0) {
                usleep(10);
-               if (rbtree->comp(k, x->key) < 0)
+               if (comp < 0)
                        x = rcu_dereference(x->_left);
                else
                        x = rcu_dereference(x->_right);
@@ -400,6 +401,50 @@ struct rcu_rbtree_node* rcu_rbtree_search(struct rcu_rbtree *rbtree,
        return x;
 }
 
+struct rcu_rbtree_node* rcu_rbtree_search_min(struct rcu_rbtree *rbtree,
+                                         struct rcu_rbtree_node *x,
+                                         void *range_low, void *range_high)
+{
+       x = rcu_dereference(x);
+
+       while (!rcu_rbtree_is_nil(x)) {
+               usleep(10);
+               if (rbtree->comp(x->max_child_key, range_low) > 0) {
+                       x = rcu_dereference(x->_left);
+               } else if (rbtree->comp(x->key, range_low) >= 0
+                          && rbtree->comp(x->key, range_high) <= 0) {
+                       break;
+               } else if (rbtree->comp(range_low, x->min_child_key) > 0) {
+                       x = rcu_dereference(x->_right);
+               } else {
+                       x = make_nil(rbtree);
+               }
+       }
+       return x;
+}
+
+struct rcu_rbtree_node* rcu_rbtree_search_max(struct rcu_rbtree *rbtree,
+                                         struct rcu_rbtree_node *x,
+                                         void *range_low, void *range_high)
+{
+       x = rcu_dereference(x);
+
+       while (!rcu_rbtree_is_nil(x)) {
+               usleep(10);
+               if (rbtree->comp(x->min_child_key, range_high) < 0) {
+                       x = rcu_dereference(x->_right);
+               } else if (rbtree->comp(x->key, range_low) >= 0
+                          && rbtree->comp(x->key, range_high) <= 0) {
+                       break;
+               } else if (rbtree->comp(range_high, x->max_child_key) < 0) {
+                       x = rcu_dereference(x->_left);
+               } else {
+                       x = make_nil(rbtree);
+               }
+       }
+       return x;
+}
+
 static
 struct rcu_rbtree_node *rcu_rbtree_min_dup_decay(struct rcu_rbtree *rbtree,
                                                 struct rcu_rbtree_node *x,
index 3efbbfad8350cb338a9336f2bbb4a3f9dac1ed77..dc59653ceab9a4c127e0bf358e97ea01ca9a7fa5 100644 (file)
@@ -135,6 +135,18 @@ struct rcu_rbtree_node *rcu_rbtree_search(struct rcu_rbtree *rbtree,
                                          struct rcu_rbtree_node *x,
                                          void *key);
 
+/*
+ * Search for node with, respectively, smallest or largest value within
+ * the ranges (ranges are inclusive).
+ */
+struct rcu_rbtree_node *rcu_rbtree_search_min(struct rcu_rbtree *rbtree,
+                                         struct rcu_rbtree_node *x,
+                                         void *range_low, void *range_high);
+
+struct rcu_rbtree_node *rcu_rbtree_search_max(struct rcu_rbtree *rbtree,
+                                         struct rcu_rbtree_node *x,
+                                         void *range_low, void *range_high);
+
 struct rcu_rbtree_node *rcu_rbtree_min(struct rcu_rbtree *rbtree,
                                       struct rcu_rbtree_node *x);
 
This page took 0.027308 seconds and 4 git commands to generate.