xref: /petsc/include/petscbt.h (revision 76be9ce4a233aaa47cda2bc7f5a27cd7faabecaa)
1 /* $Id: bitarray.h,v 1.8 1997/12/04 19:32:12 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(bt,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
15      BTView(m,bt)
16 
17 */
18 #if !defined(__BITARRAY_H)
19 #define __BITARRAY_H
20 
21 #if !defined(BITSPERBYTE)
22 #define BITSPERBYTE 8
23 #endif
24 
25 typedef char*  BT;
26 
27 extern char _mask, _BT_c;
28 extern int  _BT_idx;
29 
30 #define BTView(m,bt) {\
31   int __i; \
32   for (__i=0; __i<m; __i++) { \
33     printf("%d %d\n",__i,BTLookup(bt,__i)); \
34   }}
35 
36 #define BTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
37 
38 #define BTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
39                            ( !array ) ? 1 : (BTMemzero(m,array),0) )
40 
41 #define BTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
42 
43 #define BTLookupSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
44                                         _BT_c           = array[_BT_idx], \
45                                         _mask           = (char)1 << ((index)%BITSPERBYTE), \
46                                         array[_BT_idx]  = _BT_c | _mask, \
47                                         _BT_c & _mask )
48 
49 #define BTSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
50                                  _BT_c           = array[_BT_idx], \
51                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
52                                  array[_BT_idx]  = _BT_c | _mask,0)
53 
54 
55 #define BTClear(array, index)  (_BT_idx         = (index)/BITSPERBYTE, \
56                                  _BT_c           = array[_BT_idx], \
57                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
58                                  array[_BT_idx]  = _BT_c & (~_mask),0)
59 
60 #define BTLookup(array, index) (_BT_idx         = (index)/BITSPERBYTE, \
61                                  _BT_c           = array[_BT_idx], \
62                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
63                                  (_BT_c & _mask) != 0 )
64 
65 
66 #define BTDestroy(array) (PetscFree(array),0)
67 
68 #endif
69