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