]> Dogcows Code - chaz/openbox/blobdiff - obt/bsearch.h
Merge branch 'master' into chaz
[chaz/openbox] / obt / bsearch.h
index ca5ae5b27a05158b93a328cbf1473965c2aff217..60da51d33bb728021e451df9e0fd8a291ec330b8 100644 (file)
 
 G_BEGIN_DECLS
 
-#define BSEARCH_SETUP(t) \
-    register t l_BSEARCH, r_BSEARCH, out_BSEARCH;
+/*! Setup to do a binary search on an array. */
+#define BSEARCH_SETUP() \
+    register guint l_BSEARCH, r_BSEARCH, out_BSEARCH;
 
-#define BSEARCH(t, ar, start, size, val)         \
+/*! Helper macro that just returns the input */
+#define BSEARCH_IS_T(t) (t)
+
+/*! Search an array @ar holding elements of type @t, starting at index @start,
+  with @size elements, looking for value @val. */
+#define BSEARCH(t, ar, start, size, val) \
+    BSEARCH_CMP(t, ar, start, size, val, BSEARCH_IS_T)
+
+/*! Search an array @ar, starting at index @start,
+  with @size elements, looking for value @val of type @t,
+  using the macro @to_t to convert values in the arrau @ar to type @t.*/
+#define BSEARCH_CMP(t, ar, start, size, val, to_t)  \
 { \
     l_BSEARCH = (start);              \
     r_BSEARCH = (start)+(size)-1;     \
@@ -34,17 +46,21 @@ G_BEGIN_DECLS
         /* m is in the middle, but to the left if there's an even number \
            of elements */ \
         out_BSEARCH = l_BSEARCH + (r_BSEARCH - l_BSEARCH)/2;      \
-        if ((val) == (ar)[out_BSEARCH]) {                           \
+        if ((val) == to_t((ar)[out_BSEARCH])) {             \
             break; \
         } \
-        else if ((val) < (ar)[out_BSEARCH])                       \
+        else if ((val) < to_t((ar)[out_BSEARCH]) && out_BSEARCH > 0) {   \
             r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \
+        } \
         else \
             l_BSEARCH = out_BSEARCH+1; /* search to the left side */ \
     } \
 }
 
+/*! Returns true if the element last searched for was found in the array */
 #define BSEARCH_FOUND() (l_BSEARCH <= r_BSEARCH)
+/*! Returns the position in the array at which the element last searched for
+  was found. */
 #define BSEARCH_AT() (out_BSEARCH)
 
 
This page took 0.020613 seconds and 4 git commands to generate.