]> Dogcows Code - chaz/openbox/blob - obt/bsearch.h
Merge branch 'master' into chaz
[chaz/openbox] / obt / bsearch.h
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/bsearch.h for the Openbox window manager
4 Copyright (c) 2010 Dana Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #ifndef __obt_bsearch_h
20 #define __obt_bsearch_h
21
22 #include <glib.h>
23
24 G_BEGIN_DECLS
25
26 /*! Setup to do a binary search on an array. */
27 #define BSEARCH_SETUP() \
28 register guint l_BSEARCH, r_BSEARCH, out_BSEARCH;
29
30 /*! Helper macro that just returns the input */
31 #define BSEARCH_IS_T(t) (t)
32
33 /*! Search an array @ar holding elements of type @t, starting at index @start,
34 with @size elements, looking for value @val. */
35 #define BSEARCH(t, ar, start, size, val) \
36 BSEARCH_CMP(t, ar, start, size, val, BSEARCH_IS_T)
37
38 /*! Search an array @ar, starting at index @start,
39 with @size elements, looking for value @val of type @t,
40 using the macro @to_t to convert values in the arrau @ar to type @t.*/
41 #define BSEARCH_CMP(t, ar, start, size, val, to_t) \
42 { \
43 l_BSEARCH = (start); \
44 r_BSEARCH = (start)+(size)-1; \
45 while (l_BSEARCH <= r_BSEARCH) { \
46 /* m is in the middle, but to the left if there's an even number \
47 of elements */ \
48 out_BSEARCH = l_BSEARCH + (r_BSEARCH - l_BSEARCH)/2; \
49 if ((val) == to_t((ar)[out_BSEARCH])) { \
50 break; \
51 } \
52 else if ((val) < to_t((ar)[out_BSEARCH]) && out_BSEARCH > 0) { \
53 r_BSEARCH = out_BSEARCH-1; /* search to the left side */ \
54 } \
55 else \
56 l_BSEARCH = out_BSEARCH+1; /* search to the left side */ \
57 } \
58 }
59
60 /*! Returns true if the element last searched for was found in the array */
61 #define BSEARCH_FOUND() (l_BSEARCH <= r_BSEARCH)
62 /*! Returns the position in the array at which the element last searched for
63 was found. */
64 #define BSEARCH_AT() (out_BSEARCH)
65
66
67
68 G_END_DECLS
69
70 #endif
This page took 0.03222 seconds and 4 git commands to generate.