xref: /petsc/include/petscbt.h (revision c2ea1186303e6ad186d8293edcc4dffdc3bb3ffe)
1 /* $Id: bitarray.h,v 1.6 1997/10/01 22:43:45 bsmith Exp balay $ */
2 
3 /*
4       BTSet - Expexts a charecter array -'array' as input, and
5       treats it as  an array of bits. It Checks if a given bit location
6       ( specified by 'index') is marked, and later marks that location.
7 
8       Input:
9       array  - an array of char. Initially all bits are to be set to zero
10                by using PetscMemzero().
11       index  - specifies the index of the required bit in the bit array.
12 
13       Output:
14       return val - 0 if the bit is not found,
15                  - nonzero if found.
16 
17       Usage :
18       BT_LOOKUP(char * array,  int index) ;
19 
20       Summary:
21           The bit operations are equivalent to:
22               1: retval = array[index];
23               2: array[index] = 1;
24               3: return retval;
25 */
26 #if !defined(__BITARRAY_H)
27 
28 #if !defined(BITSPERBYTE)
29 #define BITSPERBYTE 8
30 #endif
31 
32 typedef char*  BT;
33 
34 static char _mask, _BT_c;
35 static int  _BT_idx;
36 
37 #define BTCreate(m,array) (array = (char *)PetscMalloc(((m)/BITSPERBYTE+1)*sizeof(char)),\
38                            ( !array ) ? 1 : (BTMemzero(m,array),0) )
39 
40 #define BTMemzero(m,array) PetscMemzero(array,(m)/BITSPERBYTE+1)
41 
42 #define BTLookupSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
43                                         _BT_c           = array[_BT_idx], \
44                                         _mask           = (char)1 << ((index)%BITSPERBYTE), \
45                                         array[_BT_idx]  = _BT_c | _mask, \
46                                         _BT_c & _mask )
47 
48 #define BTSet(array, index)    (_BT_idx         = (index)/BITSPERBYTE, \
49                                  _BT_c           = array[_BT_idx], \
50                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
51                                  array[_BT_idx]  = _BT_c | _mask,0)
52 
53 
54 #define BTClear(array, index)  (_BT_idx         = (index)/BITSPERBYTE, \
55                                  _BT_c           = array[_BT_idx], \
56                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
57                                  array[_BT_idx]  = _BT_c & (~_mask),0)
58 
59 #define BTLookup(array, index) (_BT_idx         = (index)/BITSPERBYTE, \
60                                  _BT_c           = array[_BT_idx], \
61                                  _mask           = (char)1 << ((index)%BITSPERBYTE), \
62                                  _BT_c & _mask )
63 
64 
65 #define BTDestroy(array) (PetscFree(array),0)
66 
67 #endif
68