Date: 2022-03-31T04:49:10+00:00

Actual source code: petscbt.h

      1: #ifndef PETSCBT_H
      2: #define PETSCBT_H

      4: #include <petscviewer.h>

      6: /*S
      7:      PetscBT - PETSc bitarrays

      9:      Level: advanced

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

     22:     We do not currently check error flags on PetscBTLookup(), PetcBTLookupSet(), PetscBTLength() cause error checking
     23:     would cost hundreds more cycles then the operation.

     25: S*/
     26: typedef char* PetscBT;

     28: /* convert an index i to an index suitable for indexing a PetscBT, such that
     29:  * bt[PetscBTIndex(i)] returns the i'th value of the bt */
     30: static inline size_t PetscBTIndex_Internal(PetscInt index)
     31: {
     32:   return (size_t)index/PETSC_BITS_PER_BYTE;
     33: }

     35: static inline char PetscBTMask_Internal(PetscInt index)
     36: {
     37:   return 1 << index%PETSC_BITS_PER_BYTE;
     38: }

     40: static inline size_t PetscBTLength(PetscInt m)
     41: {
     42:   return (size_t)m/PETSC_BITS_PER_BYTE+1;
     43: }

     45: static inline PetscErrorCode PetscBTMemzero(PetscInt m, PetscBT array)
     46: {
     47:   return PetscArrayzero(array,PetscBTLength(m));
     48: }

     50: static inline PetscErrorCode PetscBTDestroy(PetscBT *array)
     51: {
     52:   return (*array) ? PetscFree(*array) : 0;
     53: }

     55: static inline PetscErrorCode PetscBTCreate(PetscInt m, PetscBT *array)
     56: {
     57:   return PetscCalloc1(PetscBTLength(m),array);
     58: }

     60: static inline char PetscBTLookup(PetscBT array, PetscInt index)
     61: {
     62:   return array[PetscBTIndex_Internal(index)] & PetscBTMask_Internal(index);
     63: }

     65: static inline PetscErrorCode PetscBTSet(PetscBT array, PetscInt index)
     66: {
     67:   array[PetscBTIndex_Internal(index)] |= PetscBTMask_Internal(index);
     68:   return 0;
     69: }

     71: static inline PetscErrorCode PetscBTNegate(PetscBT array, PetscInt index)
     72: {
     73:   array[PetscBTIndex_Internal(index)] ^= PetscBTMask_Internal(index);
     74:   return 0;
     75: }

     77: static inline PetscErrorCode PetscBTClear(PetscBT array, PetscInt index)
     78: {
     79:   array[PetscBTIndex_Internal(index)] &= (char) ~PetscBTMask_Internal(index);
     80:   return 0;
     81: }

     83: static inline char PetscBTLookupSet(PetscBT array, PetscInt index)
     84: {
     85:   const char ret = PetscBTLookup(array,index);
     86:   PetscBTSet(array,index);
     87:   return ret;
     88: }

     90: static inline char PetscBTLookupClear(PetscBT array, PetscInt index)
     91: {
     92:   const char ret = PetscBTLookup(array,index);
     93:   PetscBTClear(array,index);
     94:   return ret;
     95: }

     97: static inline PetscErrorCode PetscBTView(PetscInt m, const PetscBT bt, PetscViewer viewer)
     98: {
     99:   if (m < 1) return 0;
    100:   if (!viewer) PetscViewerASCIIGetStdout(PETSC_COMM_SELF,&viewer);
    101:   PetscViewerASCIIPushSynchronized(viewer);
    102:   for (PetscInt i = 0; i < m; ++i) {
    103:     PetscViewerASCIISynchronizedPrintf(viewer,"%" PetscInt_FMT " %d\n",i,(int)PetscBTLookup(bt,i));
    104:   }
    105:   PetscViewerFlush(viewer);
    106:   PetscViewerASCIIPopSynchronized(viewer);
    107:   return 0;
    108: }
    109: #endif /* PETSCBT_H */