Add another BSEARCH function that lets you search through an array of objects.
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.
This commit is contained in:
parent
30fe9697df
commit
7d32190a4c
2 changed files with 16 additions and 7 deletions
|
@ -23,13 +23,22 @@
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
/*! Setup to do a binary search on an array holding elements of type @t */
|
/*! Setup to do a binary search on an array. */
|
||||||
#define BSEARCH_SETUP(t) \
|
#define BSEARCH_SETUP() \
|
||||||
register t l_BSEARCH, r_BSEARCH, out_BSEARCH;
|
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,
|
/*! Search an array @ar holding elements of type @t, starting at index @start,
|
||||||
with @size elements, looking for value @val. */
|
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); \
|
l_BSEARCH = (start); \
|
||||||
r_BSEARCH = (start)+(size)-1; \
|
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 \
|
/* m is in the middle, but to the left if there's an even number \
|
||||||
of elements */ \
|
of elements */ \
|
||||||
out_BSEARCH = l_BSEARCH + (r_BSEARCH - l_BSEARCH)/2; \
|
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 */ \
|
r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \
|
||||||
} \
|
} \
|
||||||
else \
|
else \
|
||||||
|
|
|
@ -335,7 +335,7 @@ static inline gboolean try_exec(const ObtPaths *const p,
|
||||||
const gchar *const path)
|
const gchar *const path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
BSEARCH_SETUP(guint);
|
BSEARCH_SETUP();
|
||||||
|
|
||||||
if (stat(path, &st) != 0)
|
if (stat(path, &st) != 0)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
Loading…
Reference in a new issue