projects
/
dana
/
openbox.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
30fe969
)
Add another BSEARCH function that lets you search through an array of objects.
author
Dana Jansens
<danakj@orodu.net>
Thu, 27 Jan 2011 22:23:47 +0000
(17:23 -0500)
committer
Dana Jansens
<danakj@orodu.net>
Sun, 30 Sep 2012 18:50:30 +0000
(14:50 -0400)
If you give it a value x, the macro lets you pull a value out of each object
that you want to compare to x.
obt/bsearch.h
patch
|
blob
|
history
obt/paths.c
patch
|
blob
|
history
diff --git
a/obt/bsearch.h
b/obt/bsearch.h
index
9613c51
..
60da51d
100644
(file)
--- a/
obt/bsearch.h
+++ b/
obt/bsearch.h
@@
-23,13
+23,22
@@
G_BEGIN_DECLS
G_BEGIN_DECLS
-/*! Setup to do a binary search on an array holding elements of type @t */
-#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;
+
+/*! 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. */
/*! 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) \
+#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; \
{ \
l_BSEARCH = (start); \
r_BSEARCH = (start)+(size)-1; \
@@
-37,10
+46,10
@@
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; \
/* 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; \
} \
break; \
} \
- else if ((val) <
(ar)[out_BSEARCH] && out_BSEARCH > 0) {
\
+ else if ((val) <
to_t((ar)[out_BSEARCH]) && out_BSEARCH > 0) {
\
r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \
} \
else \
r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \
} \
else \
diff --git
a/obt/paths.c
b/obt/paths.c
index
aa76b8e
..
f2d4b42
100644
(file)
--- a/
obt/paths.c
+++ b/
obt/paths.c
@@
-335,7
+335,7
@@
static inline gboolean try_exec(const ObtPaths *const p,
const gchar *const path)
{
struct stat st;
const gchar *const path)
{
struct stat st;
- BSEARCH_SETUP(
guint
);
+ BSEARCH_SETUP();
if (stat(path, &st) != 0)
return FALSE;
if (stat(path, &st) != 0)
return FALSE;