xref: /petsc/include/petscbt.h (revision b5758dff40dc4232f7fa9713b5481f7e0e0d5d24)
1 /* $Id: petscbt.h,v 1.17 2001/01/15 21:50:04 bsmith Exp balay $ */
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 
31 #if !defined(BITSPERBYTE)
32 #define BITSPERBYTE 8
33 #endif
34 
35 typedef char* PetscBT;
36 
37 extern char _BT_mask,_BT_c;
38 extern int  _BT_idx;
39 
40 #define PetscBTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
41 #define PetscBTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
42 #define PetscBTDestroy(array)   PetscFree(array)
43 
44 #define PetscBTView(m,bt,viewer) 0; {\
45   int    __i,__ierr; \
46   PetscViewer __viewer = viewer; \
47   if (!__viewer) __viewer = PETSC_VIEWER_STDOUT_SELF;\
48   for (__i=0; __i<m; __i++) { \
49     __ierr = PetscPrintf(((PetscObject)__viewer)->comm,"%d %d\n",__i,PetscBTLookup(bt,__i));CHKERRQ(__ierr);\
50   }}
51 
52 #define PetscBTCreate(m,array)  0; { \
53   int __ierr; \
54   __ierr = PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char),&(array));CHKERRQ(__ierr);\
55   __ierr = PetscBTMemzero(m,array);CHKERRQ(__ierr);\
56   }
57 
58 #define PetscBTLookupSet(array,index)   (_BT_idx           = (index)/BITSPERBYTE, \
59                                         _BT_c           = array[_BT_idx], \
60                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
61                                         array[_BT_idx]  = _BT_c | _BT_mask, \
62                                         _BT_c & _BT_mask)
63 
64 #define PetscBTSet(array,index)         (_BT_idx          = (index)/BITSPERBYTE, \
65                                         _BT_c           = array[_BT_idx], \
66                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
67                                         array[_BT_idx]  = _BT_c | _BT_mask,0)
68 
69 
70 #define PetscBTClear(array,index)  (_BT_idx          = (index)/BITSPERBYTE, \
71                                    _BT_c           = array[_BT_idx], \
72                                    _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
73                                    array[_BT_idx]  = _BT_c & (~_BT_mask),0)
74 
75 #define PetscBTLookup(array,index) (_BT_idx          = (index)/BITSPERBYTE, \
76                                    _BT_c           = array[_BT_idx], \
77                                    _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
78                                    (_BT_c & _BT_mask) != 0)
79 
80 #endif
81 
82 
83