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