xref: /petsc/include/petscbt.h (revision 454a90a3eb7bca6958262e5eca1eb393ad97e108)
1 /* $Id: bitarray.h,v 1.11 1998/10/16 03:15:04 bsmith Exp bsmith $ */
2 
3 /*
4 
5           BT - Bit array objects: used to compactly store logical arrays of variables.
6 
7      BTCreate(m,bt)        - creates a bit array with enough room to hold m values
8      BTDestroy(bt)         - destroys the bit array
9      BTMemzero(m,bt)       - zeros the entire bit array (sets all values to false)
10      BTSet(bt,index)       - sets a particular entry as true
11      BTClear(bt,index)     - sets a particular entry as false
12      BTLookup(bt,index)    - returns the value
13      BTLookupSet(bt,index) - returns the value and then sets it true
14      BTLength(m)           - returns number of bytes in array with m bits
15      BTView(m,bt)          - prints all the entries in a bit array
16 
17     These routines do not currently have manual pages.
18 
19     The are all implemented as macros with the trivial data structure for efficiency.
20 
21     These are not thread safe since they use a few global variables.
22 
23 */
24 #if !defined(__BITARRAY_H)
25 #define __BITARRAY_H
26 
27 #if !defined(BITSPERBYTE)
28 #define BITSPERBYTE 8
29 #endif
30 
31 typedef char* BT;
32 
33 extern char _BT_mask, _BT_c;
34 extern int  _BT_idx;
35 
36 #define BTView(m,bt) {\
37   int __i; \
38   for (__i=0; __i<m; __i++) { \
39     printf("%d %d\n",__i,BTLookup(bt,__i)); \
40   }}
41 
42 #define BTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
43 
44 #define BTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
45                            ( !array ) ? 1 : (BTMemzero(m,array),0) )
46 
47 #define BTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
48 
49 #define BTLookupSet(array, index)    (_BT_idx           = (index)/BITSPERBYTE, \
50                                         _BT_c           = array[_BT_idx], \
51                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
52                                         array[_BT_idx]  = _BT_c | _BT_mask, \
53                                         _BT_c & _BT_mask )
54 
55 #define BTSet(array, index)    (_BT_idx          = (index)/BITSPERBYTE, \
56                                  _BT_c           = array[_BT_idx], \
57                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
58                                  array[_BT_idx]  = _BT_c | _BT_mask,0)
59 
60 
61 #define BTClear(array, index)  (_BT_idx          = (index)/BITSPERBYTE, \
62                                  _BT_c           = array[_BT_idx], \
63                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
64                                  array[_BT_idx]  = _BT_c & (~_BT_mask),0)
65 
66 #define BTLookup(array, index) (_BT_idx          = (index)/BITSPERBYTE, \
67                                  _BT_c           = array[_BT_idx], \
68                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
69                                  (_BT_c & _BT_mask) != 0 )
70 
71 #define BTDestroy(array) PetscFree(array)
72 
73 #endif
74 
75 
76