xref: /petsc/include/petscbt.h (revision 3b71518ff5d020f0ab0d46c56ed7f516578ab134)
1*3b71518fSBarry Smith /* $Id: bitarray.h,v 1.7 1997/10/06 15:34:04 balay Exp bsmith $ */
2bd3dcc6dSSatish Balay 
319fee000SSatish Balay /*
419fee000SSatish Balay 
5*3b71518fSBarry Smith           BT - Bit array objects: used to compactly store logical arrays of variables.
619fee000SSatish Balay 
7*3b71518fSBarry Smith      BTCreate(m,bt)        - creates a bit array with enough room to hold m values
8*3b71518fSBarry Smith      BTDestroy(bt)         - destroys the bit array
9*3b71518fSBarry Smith      BTMemzero(bt,bt)      - zeros the entire bit array (sets all values to false)
10*3b71518fSBarry Smith      BTSet(bt,index)       - sets a particular entry as true
11*3b71518fSBarry Smith      BTClear(bt,index)     - sets a particular entry as false
12*3b71518fSBarry Smith      BTLookup(bt,index)    - returns the value
13*3b71518fSBarry Smith      BTLookupSet(bt,index) - returns the value and then sets it true
14*3b71518fSBarry Smith      BTLength(m)           - returns number of bytes in array
15*3b71518fSBarry Smith      BTView(m,bt)
1619fee000SSatish Balay 
1719fee000SSatish Balay */
18ca71c51bSBarry Smith #if !defined(__BITARRAY_H)
19*3b71518fSBarry Smith #define __BITARRAY_H
20ca71c51bSBarry Smith 
21e2e5485fSSatish Balay #if !defined(BITSPERBYTE)
22e2e5485fSSatish Balay #define BITSPERBYTE 8
23e2e5485fSSatish Balay #endif
24e2e5485fSSatish Balay 
25ca71c51bSBarry Smith typedef char*  BT;
26ca71c51bSBarry Smith 
2719fee000SSatish Balay static char _mask, _BT_c;
2819fee000SSatish Balay static int  _BT_idx;
29ca71c51bSBarry Smith 
30*3b71518fSBarry Smith #define BTView(m,bt) {\
31*3b71518fSBarry Smith   int __i; \
32*3b71518fSBarry Smith   for (__i=0; __i<m; __i++) { \
33*3b71518fSBarry Smith     printf("%d %d\n",__i,BTLookup(bt,__i)); \
34*3b71518fSBarry Smith   }}
35*3b71518fSBarry Smith 
36*3b71518fSBarry Smith #define BTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
37*3b71518fSBarry Smith 
38ca71c51bSBarry Smith #define BTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
39ca71c51bSBarry Smith                            ( !array ) ? 1 : (BTMemzero(m,array),0) )
40ca71c51bSBarry Smith 
41f155196cSSatish Balay #define BTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
42ca71c51bSBarry Smith 
43ca71c51bSBarry Smith #define BTLookupSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
4419fee000SSatish Balay                                         _BT_c           = array[_BT_idx], \
45ca71c51bSBarry Smith                                         _mask           = (char)1 << ((index)%BITSPERBYTE), \
4619fee000SSatish Balay                                         array[_BT_idx]  = _BT_c | _mask, \
4719fee000SSatish Balay                                         _BT_c & _mask )
4819fee000SSatish Balay 
49ca71c51bSBarry Smith #define BTSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
50ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
51ca71c51bSBarry Smith                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
52ca71c51bSBarry Smith                                  array[_BT_idx]  = _BT_c | _mask,0)
5319fee000SSatish Balay 
54ca71c51bSBarry Smith 
55ca71c51bSBarry Smith #define BTClear(array, index)  (_BT_idx         = (index)/BITSPERBYTE, \
56ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
57ca71c51bSBarry Smith                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
58ca71c51bSBarry Smith                                  array[_BT_idx]  = _BT_c & (~_mask),0)
59ca71c51bSBarry Smith 
60ca71c51bSBarry Smith #define BTLookup(array, index) (_BT_idx         = (index)/BITSPERBYTE, \
61ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
62ca71c51bSBarry Smith                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
63*3b71518fSBarry Smith                                  (_BT_c & _mask) != 0 )
64ca71c51bSBarry Smith 
65ca71c51bSBarry Smith 
66ca71c51bSBarry Smith #define BTDestroy(array) (PetscFree(array),0)
67ca71c51bSBarry Smith 
68ca71c51bSBarry Smith #endif
69