xref: /petsc/include/petscbt.h (revision 6831982abb6453c2f3e25efecb5d0951d333371e)
1 /* $Id: bitarray.h,v 1.12 1999/09/27 21:33:07 bsmith Exp bsmith $ */
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      PetscBTLoopup(bt,index)    - returns the value
13      PetscBTLoopupSet(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(__BITARRAY_H)
29 #define __BITARRAY_H
30 
31 #if !defined(BITSPERBYTE)
32 #define BITSPERBYTE 8
33 #endif
34 
35 typedef char* BTPetsc;
36 
37 extern char _BT_mask, _BT_c;
38 extern int  _BT_idx;
39 
40 #define PetscBTView(m,bt,viewer) {\
41   int    __i,__ierr; \
42   Viewer __viewer = viewer; \
43   if (!__viewer) __viewer = VIEWER_STDOUT_SELF;\
44   for (__i=0; __i<m; __i++) { \
45     __ierr = PetscPrintf(((PetscObject)__viewer)->comm,"%d %d\n",__i,PetscBTLoopup(bt,__i)); CHKERRQ(__ierr);\
46   }}
47 
48 #define PetscBTLength(m)        ((m)/BITSPERBYTE+1)*sizeof(char)
49 
50 #define PetscBTCreate(m,array)  (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
51                            ( !array ) ? 1 : PetscBTMemzero(m,array) )
52 
53 #define PetscBTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
54 
55 #define PetscBTLoopupSet(array, index)    (_BT_idx           = (index)/BITSPERBYTE, \
56                                         _BT_c           = array[_BT_idx], \
57                                         _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
58                                         array[_BT_idx]  = _BT_c | _BT_mask, \
59                                         _BT_c & _BT_mask )
60 
61 #define PetscBTSet(array, index)    (_BT_idx          = (index)/BITSPERBYTE, \
62                                  _BT_c           = array[_BT_idx], \
63                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
64                                  array[_BT_idx]  = _BT_c | _BT_mask,0)
65 
66 
67 #define PetscBTClear(array, index)  (_BT_idx          = (index)/BITSPERBYTE, \
68                                  _BT_c           = array[_BT_idx], \
69                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
70                                  array[_BT_idx]  = _BT_c & (~_BT_mask),0)
71 
72 #define PetscBTLoopup(array, index) (_BT_idx          = (index)/BITSPERBYTE, \
73                                  _BT_c           = array[_BT_idx], \
74                                  _BT_mask        = (char)1 << ((index)%BITSPERBYTE), \
75                                  (_BT_c & _BT_mask) != 0 )
76 
77 #define PetscBTDestroy(array) PetscFree(array)
78 
79 #endif
80 
81 
82