Actual source code: petscbt.h


  2: #if !defined(PETSCBT_H)
  3: #define PETSCBT_H

  5: #include <petscconf.h>
  6: #include <petscviewer.h>

  8: /*S
  9:      PetscBT - PETSc bitarrays

 11:      Level: advanced

 13:      PetscBTCreate(m,&bt)         - creates a bit array with enough room to hold m values
 14:      PetscBTDestroy(&bt)          - destroys the bit array
 15:      PetscBTMemzero(m,bt)         - zeros the entire bit array (sets all values to false)
 16:      PetscBTSet(bt,index)         - sets a particular entry as true
 17:      PetscBTClear(bt,index)       - sets a particular entry as false
 18:      PetscBTLookup(bt,index)      - returns the value
 19:      PetscBTLookupSet(bt,index)   - returns the value and then sets it true
 20:      PetscBTLookupClear(bt,index) - returns the value and then sets it false
 21:      PetscBTLength(m)             - returns number of bytes in array with m bits
 22:      PetscBTView(m,bt,viewer)     - prints all the entries in a bit array

 24:     We do not currently check error flags on PetscBTSet(), PetscBTClear(), PetscBTLookup(),
 25:     PetcBTLookupSet(), PetscBTLength() cause error checking would cost hundreds more cycles then
 26:     the operation.

 28: S*/
 29: typedef char* PetscBT;

 31: PETSC_STATIC_INLINE PetscInt PetscBTLength(PetscInt m)
 32: {
 33:   return  m/PETSC_BITS_PER_BYTE+1;
 34: }

 36: PETSC_STATIC_INLINE PetscErrorCode PetscBTMemzero(PetscInt m,PetscBT array)
 37: {
 38:   return PetscMemzero(array,sizeof(char)*((size_t)m/PETSC_BITS_PER_BYTE+1));
 39: }

 41: PETSC_STATIC_INLINE PetscErrorCode PetscBTDestroy(PetscBT *array)
 42: {
 43:   return PetscFree(*array);
 44: }

 46: PETSC_STATIC_INLINE char PetscBTLookup(PetscBT array,PetscInt index)
 47: {
 48:   char      BT_mask,BT_c;
 49:   PetscInt  BT_idx;

 51:   BT_idx        = index/PETSC_BITS_PER_BYTE;
 52:   BT_c          = array[BT_idx];
 53:   BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE);
 54:   return (char)(BT_c & BT_mask);
 55: }

 57: PETSC_STATIC_INLINE PetscErrorCode PetscBTView(PetscInt m,const PetscBT bt,PetscViewer viewer)
 58: {
 59:   PetscInt       i;

 62:   if (!viewer) {PetscViewerASCIIGetStdout(PETSC_COMM_SELF,&viewer);}
 63:   PetscViewerASCIIPushSynchronized(viewer);
 64:   for (i=0; i<m; i++) {
 65:     PetscViewerASCIISynchronizedPrintf(viewer,"%D %d\n",i,(int)PetscBTLookup(bt,i));
 66:   }
 67:   PetscViewerFlush(viewer);
 68:   PetscViewerASCIIPopSynchronized(viewer);
 69:   return 0;
 70: }

 72: PETSC_STATIC_INLINE PetscErrorCode PetscBTCreate(PetscInt m,PetscBT *array)
 73: {
 74:   return PetscMalloc1((size_t)m/PETSC_BITS_PER_BYTE+1,array) || PetscBTMemzero(m,*array);
 75: }

 77: PETSC_STATIC_INLINE char PetscBTLookupSet(PetscBT array,PetscInt index)
 78: {
 79:   char      BT_mask,BT_c;
 80:   PetscInt  BT_idx;

 82:   BT_idx        = index/PETSC_BITS_PER_BYTE;
 83:   BT_c          = array[BT_idx];
 84:   BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE);
 85:   array[BT_idx] = (char)(BT_c | BT_mask);
 86:   return        (char)(BT_c & BT_mask);
 87: }

 89: PETSC_STATIC_INLINE PetscErrorCode PetscBTSet(PetscBT array,PetscInt index)
 90: {
 91:   char      BT_mask,BT_c;
 92:   PetscInt  BT_idx;

 94:   BT_idx        = index/PETSC_BITS_PER_BYTE;
 95:   BT_c          = array[BT_idx];
 96:   BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE);
 97:   array[BT_idx] = (char)(BT_c | BT_mask);
 98:   return 0;
 99: }

101: PETSC_STATIC_INLINE PetscErrorCode PetscBTNegate(PetscBT array,PetscInt index)
102: {
103:   const PetscInt BT_idx  = index/PETSC_BITS_PER_BYTE;
104:   const char     BT_mask = (char)(1 << index%PETSC_BITS_PER_BYTE);

106:   array[BT_idx] ^= BT_mask;
107:   return 0;
108: }

110: PETSC_STATIC_INLINE char PetscBTLookupClear(PetscBT array,PetscInt index)
111: {
112:   char      BT_mask,BT_c;
113:   PetscInt  BT_idx;

115:   BT_idx        = index/PETSC_BITS_PER_BYTE;
116:   BT_c          = array[BT_idx];
117:   BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE);
118:   array[BT_idx] = (char)(BT_c & ~BT_mask);
119:   return (char)(BT_c & BT_mask);
120: }

122: PETSC_STATIC_INLINE PetscErrorCode PetscBTClear(PetscBT array,PetscInt index)
123: {
124:   char      BT_mask,BT_c;
125:   PetscInt  BT_idx;

127:   BT_idx        = index/PETSC_BITS_PER_BYTE;
128:   BT_c          = array[BT_idx];
129:   BT_mask       = (char)(1 << index%PETSC_BITS_PER_BYTE);
130:   array[BT_idx] = (char)(BT_c & ~BT_mask);
131:  return 0;
132: }

134: #endif