xref: /petsc/include/petscbt.h (revision 35c17c5b45474863be5053d4a78dddf1c1a3dc95)
1*35c17c5bSBarry Smith /* $Id: bitarray.h,v 1.8 1997/12/04 19:32:12 bsmith Exp bsmith $ */
2bd3dcc6dSSatish Balay 
319fee000SSatish Balay /*
419fee000SSatish Balay 
53b71518fSBarry Smith           BT - Bit array objects: used to compactly store logical arrays of variables.
619fee000SSatish Balay 
73b71518fSBarry Smith      BTCreate(m,bt)        - creates a bit array with enough room to hold m values
83b71518fSBarry Smith      BTDestroy(bt)         - destroys the bit array
93b71518fSBarry Smith      BTMemzero(bt,bt)      - zeros the entire bit array (sets all values to false)
103b71518fSBarry Smith      BTSet(bt,index)       - sets a particular entry as true
113b71518fSBarry Smith      BTClear(bt,index)     - sets a particular entry as false
123b71518fSBarry Smith      BTLookup(bt,index)    - returns the value
133b71518fSBarry Smith      BTLookupSet(bt,index) - returns the value and then sets it true
143b71518fSBarry Smith      BTLength(m)           - returns number of bytes in array
153b71518fSBarry Smith      BTView(m,bt)
1619fee000SSatish Balay 
1719fee000SSatish Balay */
18ca71c51bSBarry Smith #if !defined(__BITARRAY_H)
193b71518fSBarry 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 
27*35c17c5bSBarry Smith extern char _mask, _BT_c;
28*35c17c5bSBarry Smith extern int  _BT_idx;
29ca71c51bSBarry Smith 
303b71518fSBarry Smith #define BTView(m,bt) {\
313b71518fSBarry Smith   int __i; \
323b71518fSBarry Smith   for (__i=0; __i<m; __i++) { \
333b71518fSBarry Smith     printf("%d %d\n",__i,BTLookup(bt,__i)); \
343b71518fSBarry Smith   }}
353b71518fSBarry Smith 
363b71518fSBarry Smith #define BTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
373b71518fSBarry 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), \
633b71518fSBarry Smith                                  (_BT_c & _mask) != 0 )
64ca71c51bSBarry Smith 
65ca71c51bSBarry Smith 
66ca71c51bSBarry Smith #define BTDestroy(array) (PetscFree(array),0)
67ca71c51bSBarry Smith 
68ca71c51bSBarry Smith #endif
69