xref: /petsc/include/petscbt.h (revision 1929aaefe4629f371bef50a567f0c8c0a6220b73)
1*1929aaefSSatish Balay /* $Id: petscbt.h,v 1.19 2001/03/09 18:50:07 balay Exp balay $ */
2bd3dcc6dSSatish Balay 
319fee000SSatish Balay /*
419fee000SSatish Balay 
53b71518fSBarry Smith           BT - Bit array objects: used to compactly store logical arrays of variables.
619fee000SSatish Balay 
76831982aSBarry Smith      PetscBTCreate(m,bt)        - creates a bit array with enough room to hold m values
86831982aSBarry Smith      PetscBTDestroy(bt)         - destroys the bit array
96831982aSBarry Smith      PetscBTMemzero(m,bt)       - zeros the entire bit array (sets all values to false)
106831982aSBarry Smith      PetscBTSet(bt,index)       - sets a particular entry as true
116831982aSBarry Smith      PetscBTClear(bt,index)     - sets a particular entry as false
12f1af5d2fSBarry Smith      PetscBTLookup(bt,index)    - returns the value
13f1af5d2fSBarry Smith      PetscBTLookupSet(bt,index) - returns the value and then sets it true
146831982aSBarry Smith      PetscBTLength(m)           - returns number of bytes in array with m bits
156831982aSBarry 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 
236831982aSBarry Smith     We do not currently check error flags on PetscBTSet(), PetscBTClear(), PetscBTLookup(),
246831982aSBarry Smith     PetcBTLookupSet(), PetscBTLength() cause error checking would cost hundreds more cycles then
256831982aSBarry Smith     the operation.
266831982aSBarry Smith 
2719fee000SSatish Balay */
280a835dfdSSatish Balay #if !defined(__PETSCBT_H)
290a835dfdSSatish Balay #define __PETSCBT_H
30ca71c51bSBarry Smith 
31e2e5485fSSatish Balay #if !defined(BITSPERBYTE)
32e2e5485fSSatish Balay #define BITSPERBYTE 8
33e2e5485fSSatish Balay #endif
34e2e5485fSSatish Balay 
35f1af5d2fSBarry Smith typedef char* PetscBT;
36ca71c51bSBarry Smith 
371a89f835SSatish Balay extern char _BT_mask,_BT_c;
3835c17c5bSBarry Smith extern int  _BT_idx;
39ca71c51bSBarry Smith 
4082502324SSatish Balay #define PetscBTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
4182502324SSatish Balay #define PetscBTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
4282502324SSatish Balay #define PetscBTDestroy(array)   PetscFree(array)
4382502324SSatish Balay 
4482502324SSatish Balay #define PetscBTView(m,bt,viewer) 0; {\
456831982aSBarry Smith   int    __i,__ierr; \
46b0a32e0cSBarry Smith   PetscViewer __viewer = viewer; \
47b0a32e0cSBarry Smith   if (!__viewer) __viewer = PETSC_VIEWER_STDOUT_SELF;\
483b71518fSBarry Smith   for (__i=0; __i<m; __i++) { \
49f1af5d2fSBarry Smith     __ierr = PetscPrintf(((PetscObject)__viewer)->comm,"%d %d\n",__i,PetscBTLookup(bt,__i));CHKERRQ(__ierr);\
503b71518fSBarry Smith   }}
513b71518fSBarry Smith 
5282502324SSatish Balay #define PetscBTCreate(m,array)  0; { \
5382502324SSatish Balay   int __ierr; \
5482502324SSatish Balay   __ierr = PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char),&(array));CHKERRQ(__ierr);\
5582502324SSatish Balay   __ierr = PetscBTMemzero(m,array);CHKERRQ(__ierr);\
5682502324SSatish Balay   }
57ca71c51bSBarry Smith 
58f1af5d2fSBarry Smith #define PetscBTLookupSet(array,index)   (_BT_idx           = (index)/BITSPERBYTE, \
5919fee000SSatish Balay                                         _BT_c           = array[_BT_idx], \
601a89f835SSatish Balay                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
611a89f835SSatish Balay                                         array[_BT_idx]  = _BT_c | _BT_mask, \
621a89f835SSatish Balay                                         _BT_c & _BT_mask)
6319fee000SSatish Balay 
646831982aSBarry Smith #define PetscBTSet(array,index)         (_BT_idx          = (index)/BITSPERBYTE, \
65ca71c51bSBarry Smith                                         _BT_c           = array[_BT_idx], \
661a89f835SSatish Balay                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
67*1929aaefSSatish Balay                                         array[_BT_idx]  = _BT_c | _BT_mask,0)
6819fee000SSatish Balay 
69ca71c51bSBarry Smith 
706831982aSBarry Smith #define PetscBTClear(array,index)  (_BT_idx          = (index)/BITSPERBYTE, \
71ca71c51bSBarry Smith                                    _BT_c           = array[_BT_idx], \
721a89f835SSatish Balay                                    _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
73*1929aaefSSatish Balay                                    array[_BT_idx]  = _BT_c & (~_BT_mask),0)
74ca71c51bSBarry Smith 
75f1af5d2fSBarry Smith #define PetscBTLookup(array,index) (_BT_idx          = (index)/BITSPERBYTE, \
76ca71c51bSBarry Smith                                    _BT_c           = array[_BT_idx], \
771a89f835SSatish Balay                                    _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
781a89f835SSatish Balay                                    (_BT_c & _BT_mask) != 0)
79ca71c51bSBarry Smith 
80ca71c51bSBarry Smith #endif
81184914b5SBarry Smith 
82184914b5SBarry Smith 
83