xref: /petsc/include/petscbt.h (revision 1d2e40055fe3bf783df36eeeccf2d7d5fcfab0fd)
1 #if !defined(__PETSCBT_H)
2 #define __PETSCBT_H
3 PETSC_EXTERN_CXX_BEGIN
4 
5 /*S
6      PetscBT - PETSc bitarrays
7 
8      Level: advanced
9 
10      PetscBTCreate(m,bt)        - creates a bit array with enough room to hold m values
11      PetscBTDestroy(bt)         - destroys the bit array
12      PetscBTMemzero(m,bt)       - zeros the entire bit array (sets all values to false)
13      PetscBTSet(bt,index)       - sets a particular entry as true
14      PetscBTClear(bt,index)     - sets a particular entry as false
15      PetscBTLookup(bt,index)    - returns the value
16      PetscBTLookupSet(bt,index) - returns the value and then sets it true
17      PetscBTLength(m)           - returns number of bytes in array with m bits
18      PetscBTView(m,bt,viewer)   - prints all the entries in a bit array
19 
20     The are all implemented as macros with the trivial data structure for efficiency.
21 
22     These are not thread safe since they use a few global variables.
23 
24     We do not currently check error flags on PetscBTSet(), PetscBTClear(), PetscBTLookup(),
25     PetcBTLookupSet(), PetscBTLength() cause error checking would cost hundreds more cycles then
26     the operation.
27 
28 S*/
29 typedef char* PetscBT;
30 
31 extern PETSC_DLLEXPORT char      _BT_mask;
32 extern PETSC_DLLEXPORT char      _BT_c;
33 extern PETSC_DLLEXPORT PetscInt  _BT_idx;
34 
35 #define PetscBTLength(m)        ((m)/PETSC_BITS_PER_BYTE+1)
36 #define PetscBTMemzero(m,array) PetscMemzero(array,sizeof(char)*((m)/PETSC_BITS_PER_BYTE+1))
37 #define PetscBTDestroy(array)   PetscFree(array)
38 
39 #define PetscBTView(m,bt,viewer) 0; {\
40   PetscInt __i; PetscErrorCode _8_ierr; \
41   PetscViewer __viewer = viewer; \
42   if (!__viewer) __viewer = PETSC_VIEWER_STDOUT_SELF;\
43   for (__i=0; __i<m; __i++) { \
44     _8_ierr = PetscPrintf(((PetscObject)__viewer)->comm,"%D %d\n",__i,PetscBTLookup(bt,__i));CHKERRQ(_8_ierr);\
45   }}
46 
47 #define PetscBTCreate(m,array)  \
48   (PetscMalloc(((m)/PETSC_BITS_PER_BYTE+1)*sizeof(char),&(array)) || PetscBTMemzero(m,array))
49 
50 #define PetscBTLookupSet(array,index) \
51   (_BT_idx        = (index)/PETSC_BITS_PER_BYTE, \
52    _BT_c          = array[_BT_idx], \
53    _BT_mask       = (char)1 << ((index)%PETSC_BITS_PER_BYTE), \
54    array[_BT_idx] = _BT_c | _BT_mask, \
55    _BT_c & _BT_mask)
56 
57 #define PetscBTSet(array,index)  \
58   (_BT_idx        = (index)/PETSC_BITS_PER_BYTE, \
59    _BT_c          = array[_BT_idx], \
60    _BT_mask       = (char)1 << ((index)%PETSC_BITS_PER_BYTE), \
61    array[_BT_idx] = _BT_c | _BT_mask,0)
62 
63 #define PetscBTClear(array,index) \
64   (_BT_idx        = (index)/PETSC_BITS_PER_BYTE, \
65    _BT_c          = array[_BT_idx], \
66    _BT_mask       = (char)1 << ((index)%PETSC_BITS_PER_BYTE), \
67    array[_BT_idx] = _BT_c & (~_BT_mask),0)
68 
69 #define PetscBTLookup(array,index) \
70   (_BT_idx        = (index)/PETSC_BITS_PER_BYTE, \
71    _BT_c          = array[_BT_idx], \
72    _BT_mask       = (char)1 << ((index)%PETSC_BITS_PER_BYTE), \
73    (_BT_c & _BT_mask) != 0)
74 
75 PETSC_EXTERN_CXX_END
76 #endif
77