Actual source code: petscbt.h
petsc-3.7.7 2017-09-25
Report Typos and Errors

      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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    136: #endif