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