• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _BITSET_H
2 #define _BITSET_H
3 
4 #include <stdlib.h>
5 
6 typedef int * BITSET;
7 
bit_set(BITSET bs,int num)8 static inline void bit_set(BITSET bs,int num)
9 {
10   bs[num/(8*sizeof(int))]|=1<<(num%(8*sizeof(int)));
11 }
12 
bit_check(BITSET bs,int num)13 static inline int bit_check(BITSET bs,int num)
14 {
15   return bs[num/(8*sizeof(int))]&1<<(num%(8*sizeof(int)));
16 }
17 
18 // use free() when done. returns NULL on bad_alloc
bitset_new(int size)19 static inline BITSET bitset_new(int size)
20 {
21   return (BITSET)calloc(1,((size+8*sizeof(int)-1)&~(8*sizeof(int)-1))/8);
22 }
23 
bits_used(BITSET bits,int size)24 static inline int bits_used(BITSET bits,int size) // {{{  returns true if any bit is used
25 {
26   size=(size+8*sizeof(int)-1)/(8*sizeof(int));
27   while (size>0) {
28     if (*bits) {
29       return 1;
30     }
31     bits++;
32     size--;
33   }
34   return 0;
35 }
36 // }}}
37 
38 #endif
39