xref: /petsc/include/petscbt.h (revision 6831982abb6453c2f3e25efecb5d0951d333371e)
1*6831982aSBarry Smith /* $Id: bitarray.h,v 1.12 1999/09/27 21:33:07 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 
7*6831982aSBarry Smith      PetscBTCreate(m,bt)        - creates a bit array with enough room to hold m values
8*6831982aSBarry Smith      PetscBTDestroy(bt)         - destroys the bit array
9*6831982aSBarry Smith      PetscBTMemzero(m,bt)       - zeros the entire bit array (sets all values to false)
10*6831982aSBarry Smith      PetscBTSet(bt,index)       - sets a particular entry as true
11*6831982aSBarry Smith      PetscBTClear(bt,index)     - sets a particular entry as false
12*6831982aSBarry Smith      PetscBTLoopup(bt,index)    - returns the value
13*6831982aSBarry Smith      PetscBTLoopupSet(bt,index) - returns the value and then sets it true
14*6831982aSBarry Smith      PetscBTLength(m)           - returns number of bytes in array with m bits
15*6831982aSBarry Smith      PetscBTView(m,bt,viewer)   - prints all the entries in a bit array
16eec0b4cfSBarry Smith 
17eec0b4cfSBarry Smith     These routines do not currently have manual pages.
18eec0b4cfSBarry Smith 
19eec0b4cfSBarry Smith     The are all implemented as macros with the trivial data structure for efficiency.
20eec0b4cfSBarry Smith 
21eec0b4cfSBarry Smith     These are not thread safe since they use a few global variables.
2219fee000SSatish Balay 
23*6831982aSBarry Smith     We do not currently check error flags on PetscBTSet(), PetscBTClear(), PetscBTLookup(),
24*6831982aSBarry Smith     PetcBTLookupSet(), PetscBTLength() cause error checking would cost hundreds more cycles then
25*6831982aSBarry Smith     the operation.
26*6831982aSBarry Smith 
2719fee000SSatish Balay */
28ca71c51bSBarry Smith #if !defined(__BITARRAY_H)
293b71518fSBarry Smith #define __BITARRAY_H
30ca71c51bSBarry Smith 
31e2e5485fSSatish Balay #if !defined(BITSPERBYTE)
32e2e5485fSSatish Balay #define BITSPERBYTE 8
33e2e5485fSSatish Balay #endif
34e2e5485fSSatish Balay 
35*6831982aSBarry Smith typedef char* BTPetsc;
36ca71c51bSBarry Smith 
371a89f835SSatish Balay extern char _BT_mask, _BT_c;
3835c17c5bSBarry Smith extern int  _BT_idx;
39ca71c51bSBarry Smith 
40*6831982aSBarry Smith #define PetscBTView(m,bt,viewer) {\
41*6831982aSBarry Smith   int    __i,__ierr; \
42*6831982aSBarry Smith   Viewer __viewer = viewer; \
43*6831982aSBarry Smith   if (!__viewer) __viewer = VIEWER_STDOUT_SELF;\
443b71518fSBarry Smith   for (__i=0; __i<m; __i++) { \
45*6831982aSBarry Smith     __ierr = PetscPrintf(((PetscObject)__viewer)->comm,"%d %d\n",__i,PetscBTLoopup(bt,__i)); CHKERRQ(__ierr);\
463b71518fSBarry Smith   }}
473b71518fSBarry Smith 
48*6831982aSBarry Smith #define PetscBTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
493b71518fSBarry Smith 
50*6831982aSBarry Smith #define PetscBTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
51*6831982aSBarry Smith                            ( !array ) ? 1 : PetscBTMemzero(m,array) )
52ca71c51bSBarry Smith 
53*6831982aSBarry Smith #define PetscBTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
54ca71c51bSBarry Smith 
55*6831982aSBarry Smith #define PetscBTLoopupSet(array, index)    (_BT_idx           = (index)/BITSPERBYTE, \
5619fee000SSatish Balay                                         _BT_c           = array[_BT_idx], \
571a89f835SSatish Balay                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
581a89f835SSatish Balay                                         array[_BT_idx]  = _BT_c | _BT_mask, \
591a89f835SSatish Balay                                         _BT_c & _BT_mask )
6019fee000SSatish Balay 
61*6831982aSBarry Smith #define PetscBTSet(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)
6519fee000SSatish Balay 
66ca71c51bSBarry Smith 
67*6831982aSBarry Smith #define PetscBTClear(array, index)  (_BT_idx          = (index)/BITSPERBYTE, \
68ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
691a89f835SSatish Balay                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
701a89f835SSatish Balay                                  array[_BT_idx]  = _BT_c & (~_BT_mask),0)
71ca71c51bSBarry Smith 
72*6831982aSBarry Smith #define PetscBTLoopup(array, index) (_BT_idx          = (index)/BITSPERBYTE, \
73ca71c51bSBarry Smith                                  _BT_c           = array[_BT_idx], \
741a89f835SSatish Balay                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
751a89f835SSatish Balay                                  (_BT_c & _BT_mask) != 0 )
76ca71c51bSBarry Smith 
77*6831982aSBarry Smith #define PetscBTDestroy(array) PetscFree(array)
78ca71c51bSBarry Smith 
79ca71c51bSBarry Smith #endif
80184914b5SBarry Smith 
81184914b5SBarry Smith 
82