xref: /petsc/include/petscbt.h (revision eec0b4cf4ba0d9fc4996ee4d29940b7a9bb1ac57)
1*eec0b4cfSBarry Smith /* $Id: bitarray.h,v 1.10 1998/03/24 16:59:23 balay 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
9*eec0b4cfSBarry Smith      BTMemzero(m,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
14*eec0b4cfSBarry Smith      BTLength(m)           - returns number of bytes in array with m bits
15*eec0b4cfSBarry Smith      BTView(m,bt)          - prints all the entries in a bit array
16*eec0b4cfSBarry Smith 
17*eec0b4cfSBarry Smith     These routines do not currently have manual pages.
18*eec0b4cfSBarry Smith 
19*eec0b4cfSBarry Smith     The are all implemented as macros with the trivial data structure for efficiency.
20*eec0b4cfSBarry Smith 
21*eec0b4cfSBarry Smith     These are not thread safe since they use a few global variables.
2219fee000SSatish Balay 
2319fee000SSatish Balay */
24ca71c51bSBarry Smith #if !defined(__BITARRAY_H)
253b71518fSBarry Smith #define __BITARRAY_H
26ca71c51bSBarry Smith 
27e2e5485fSSatish Balay #if !defined(BITSPERBYTE)
28e2e5485fSSatish Balay #define BITSPERBYTE 8
29e2e5485fSSatish Balay #endif
30e2e5485fSSatish Balay 
31ca71c51bSBarry Smith typedef char* BT;
32ca71c51bSBarry Smith 
331a89f835SSatish Balay extern char _BT_mask, _BT_c;
3435c17c5bSBarry Smith extern int  _BT_idx;
35ca71c51bSBarry Smith 
363b71518fSBarry Smith #define BTView(m,bt) {\
373b71518fSBarry Smith   int __i; \
383b71518fSBarry Smith   for (__i=0; __i<m; __i++) { \
393b71518fSBarry Smith     printf("%d %d\n",__i,BTLookup(bt,__i)); \
403b71518fSBarry Smith   }}
413b71518fSBarry Smith 
423b71518fSBarry Smith #define BTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
433b71518fSBarry Smith 
44ca71c51bSBarry Smith #define BTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
45ca71c51bSBarry Smith                            ( !array ) ? 1 : (BTMemzero(m,array),0) )
46ca71c51bSBarry Smith 
47f155196cSSatish Balay #define BTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
48ca71c51bSBarry Smith 
49ca71c51bSBarry Smith #define BTLookupSet(array, index)    (_BT_idx           = (index)/BITSPERBYTE, \
5019fee000SSatish Balay                                         _BT_c           = array[_BT_idx], \
511a89f835SSatish Balay                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
521a89f835SSatish Balay                                         array[_BT_idx]  = _BT_c | _BT_mask, \
531a89f835SSatish Balay                                         _BT_c & _BT_mask )
5419fee000SSatish Balay 
55ca71c51bSBarry Smith #define BTSet(array, index)    (_BT_idx          = (index)/BITSPERBYTE, \
56ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
571a89f835SSatish Balay                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
581a89f835SSatish Balay                                  array[_BT_idx]  = _BT_c | _BT_mask,0)
5919fee000SSatish Balay 
60ca71c51bSBarry Smith 
61ca71c51bSBarry Smith #define BTClear(array, index)  (_BT_idx          = (index)/BITSPERBYTE, \
62ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
631a89f835SSatish Balay                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
641a89f835SSatish Balay                                  array[_BT_idx]  = _BT_c & (~_BT_mask),0)
65ca71c51bSBarry Smith 
66ca71c51bSBarry Smith #define BTLookup(array, index) (_BT_idx          = (index)/BITSPERBYTE, \
67ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
681a89f835SSatish Balay                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
691a89f835SSatish Balay                                  (_BT_c & _BT_mask) != 0 )
70ca71c51bSBarry Smith 
71ca71c51bSBarry Smith #define BTDestroy(array) (PetscFree(array),0)
72ca71c51bSBarry Smith 
73ca71c51bSBarry Smith #endif
74