4 * (c) 2003--2004 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 typedef u32 *bitarray_t;
13 #define BIT_ARRAY_WORDS(n) (((n)+31)/32)
14 #define BIT_ARRAY_BYTES(n) (4*BIT_ARRAY_WORDS(n))
15 #define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
18 bit_array_zero(bitarray_t a, uns n)
20 bzero(a, BIT_ARRAY_BYTES(n));
24 bit_array_set(bitarray_t a, uns i)
26 a[i/32] |= (1 << (i%32));
30 bit_array_clear(bitarray_t a, uns i)
32 a[i/32] &= ~(1 << (i%32));
36 bit_array_isset(bitarray_t a, uns i)
38 return a[i/32] & (1 << (i%32));
42 bit_array_get(bitarray_t a, uns i)
44 return !! bit_array_isset(a, i);
48 bit_array_test_and_set(bitarray_t a, uns i)
50 uns t = bit_array_isset(a, i);
56 bit_array_test_and_clear(bitarray_t a, uns i)
58 uns t = bit_array_isset(a, i);
59 bit_array_clear(a, i);
63 /* Iterate over all set bits, possibly destructively */
64 #define BIT_ARRAY_FISH_BITS_BEGIN(var,ary,size) \
65 for (uns var##_hi=0; var##_hi < BIT_ARRAY_WORDS(size); var##_hi++) \
66 for (uns var##_lo=0; ary[var##_hi]; var##_lo++) \
67 if (ary[var##_hi] & (1 << var##_lo)) \
69 uns var = 32*var##_hi + var##_lo; \
70 ary[var##_hi] &= ~(1 << var##_lo); \
73 #define BIT_ARRAY_FISH_BITS_END \