[or-cvs] r12360: Add a smartlist_bsearch_idx function that gives more useful (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Sat Nov 3 20:12:39 UTC 2007


Author: nickm
Date: 2007-11-03 16:12:38 -0400 (Sat, 03 Nov 2007)
New Revision: 12360

Modified:
   tor/trunk/
   tor/trunk/src/common/container.c
   tor/trunk/src/common/container.h
   tor/trunk/src/or/test.c
Log:
 r14677 at tombo:  nickm | 2007-11-03 15:16:27 -0400
 Add a smartlist_bsearch_idx function that gives more useful output than regular bsearch for the value-not-found case.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14677] on d9e39d38-0f13-419c-a857-e10a0ce2aa0c

Modified: tor/trunk/src/common/container.c
===================================================================
--- tor/trunk/src/common/container.c	2007-11-03 15:55:15 UTC (rev 12359)
+++ tor/trunk/src/common/container.c	2007-11-03 20:12:38 UTC (rev 12360)
@@ -490,23 +490,71 @@
 }
 
 /** Assuming the members of <b>sl</b> are in order, return a pointer to the
- * member which matches <b>key</b>.  Ordering and matching are defined by a
- * <b>compare</b> function, which returns 0 on a match; less than 0 if key is
+ * member that matches <b>key</b>.  Ordering and matching are defined by a
+ * <b>compare</b> function that returns 0 on a match; less than 0 if key is
  * less than member, and greater than 0 if key is greater then member.
  */
 void *
 smartlist_bsearch(smartlist_t *sl, const void *key,
                   int (*compare)(const void *key, const void **member))
 {
+#if 0
   void ** r;
+
   if (!sl->num_used)
     return NULL;
 
   r = bsearch(key, sl->list, sl->num_used, sizeof(void*),
               (int (*)(const void *, const void *))compare);
   return r ? *r : NULL;
+#endif
+  int found, idx;
+  idx = smartlist_bsearch_idx(sl, key, compare, &found);
+  return found ? smartlist_get(sl, idx) : NULL;
 }
 
+/** Assuming the members of <b>sl</b> are in order, return the index of the
+ * member that matches <b>key</b>.  If no member matches, return the index of
+ * the first member greater than <b>key</b>, or smartlist_len(sl) if no member
+ * is greater than <b>key</b>.  Set <b>found_out</b> to true on a match, to
+ * false otherwise.  Ordering and matching are defined by a <b>compare</b>
+ * function that returns 0 on a match; less than 0 if key is less than member,
+ * and greater than 0 if key is greater then member.
+ */
+int
+smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
+                      int (*compare)(const void *key, const void **member),
+                      int *found_out)
+{
+  int hi = smartlist_len(sl) - 1, lo = 0, cmp, mid;
+
+  while (lo <= hi) {
+    mid = (lo + hi) / 2;
+    cmp = compare(key, (const void**) &(sl->list[mid]));
+    if (cmp>0) { /* key > sl[mid] */
+      lo = mid+1;
+    } else if (cmp<0) { /* key < sl[mid] */
+      hi = mid-1;
+    } else { /* key == sl[mid] */
+      *found_out = 1;
+      return mid;
+    }
+  }
+  /* lo > hi. */
+  {
+    tor_assert(lo >= 0);
+    if (lo < smartlist_len(sl)) {
+      cmp = compare(key, (const void**) &(sl->list[lo]));
+      tor_assert(cmp < 0);
+    } else if (smartlist_len(sl)) {
+      cmp = compare(key, (const void**) &(sl->list[smartlist_len(sl)-1]));
+      tor_assert(cmp > 0);
+    }
+  }
+  *found_out = 0;
+  return lo;
+}
+
 /** Helper: compare two const char **s. */
 static int
 _compare_string_ptrs(const void **_a, const void **_b)

Modified: tor/trunk/src/common/container.h
===================================================================
--- tor/trunk/src/common/container.h	2007-11-03 15:55:15 UTC (rev 12359)
+++ tor/trunk/src/common/container.h	2007-11-03 20:12:38 UTC (rev 12360)
@@ -106,6 +106,10 @@
 void *smartlist_bsearch(smartlist_t *sl, const void *key,
                         int (*compare)(const void *key, const void **member))
  ATTR_PURE;
+int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
+                          int (*compare)(const void *key, const void **member),
+                          int *found_out)
+ ATTR_PURE;
 
 void smartlist_pqueue_add(smartlist_t *sl,
                           int (*compare)(const void *a, const void *b),

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2007-11-03 15:55:15 UTC (rev 12359)
+++ tor/trunk/src/or/test.c	2007-11-03 20:12:38 UTC (rev 12360)
@@ -1487,6 +1487,23 @@
   test_streq("and", smartlist_bsearch(sl, " AND", _compare_without_first_ch));
   test_eq_ptr(NULL, smartlist_bsearch(sl, " ANz", _compare_without_first_ch));
 
+  /* Test bsearch_idx */
+  {
+    int f;
+    test_eq(0, smartlist_bsearch_idx(sl," aaa",_compare_without_first_ch,&f));
+    test_eq(f, 0);
+    test_eq(0, smartlist_bsearch_idx(sl," and",_compare_without_first_ch,&f));
+    test_eq(f, 1);
+    test_eq(1, smartlist_bsearch_idx(sl," arm",_compare_without_first_ch,&f));
+    test_eq(f, 0);
+    test_eq(1, smartlist_bsearch_idx(sl," arma",_compare_without_first_ch,&f));
+    test_eq(f, 1);
+    test_eq(2, smartlist_bsearch_idx(sl," armb",_compare_without_first_ch,&f));
+    test_eq(f, 0);
+    test_eq(7, smartlist_bsearch_idx(sl," zzzz",_compare_without_first_ch,&f));
+    test_eq(f, 0);
+  }
+
   /* Test reverse() and pop_last() */
   smartlist_reverse(sl);
   cp = smartlist_join_strings(sl, ",", 0, NULL);



More information about the tor-commits mailing list