Actual source code: petscfptimpl.h
petsc-3.7.1 2016-05-15
Report Typos and Errors

      2: #if !defined(_PETSCFPIMPL_H)
      3: #define _PETSCFPIMPL_H
      4: #include <petscviewertypes.h>
      5: #include <petscsys.h>
      6: /*
      7:     Function pointer table that maps from function pointers to their string representation

     10: */
     11: typedef struct _n_PetscFPT* PetscFPT;
     12: struct _n_PetscFPT {
     13:   void     **functionpointer;
     14:   char     **functionname;
     15:   PetscInt count;
     16:   PetscInt tablesize;
     17: };
     18: extern PetscFPT  PetscFPTData;

     22: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTView(PetscViewer viewer)
     23: {
     24:   PetscInt       i;

     26:   if (!PetscFPTData) return(0);
     27:   for (i=0; i<PetscFPTData->tablesize; i++) {
     28:     if (PetscFPTData->functionpointer[i]) {
     29:       printf("%s()\n",PetscFPTData->functionname[i]);
     30:     }
     31:   }
     32:   return(0);
     33: }
     36: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTDestroy(void)
     37: {
     39:   PetscFPT       _PetscFPTData = PetscFPTData;

     41:   PetscFPTData = NULL;
     42:   if (!_PetscFPTData) return 0;
     43:   PetscFree((_PetscFPTData)->functionpointer);
     44:   PetscFree((_PetscFPTData)->functionname);
     45:   PetscFree(_PetscFPTData);
     46:   return(0);
     47: }

     51: /*
     52:    PetscFPTCreate  Creates a PETSc look up table from function pointers to strings

     54:    Input Parameters:
     55: .     n - expected number of keys

     57: */
     58: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTCreate(PetscInt n)
     59: {
     61:   PetscInt       i;
     62:   PetscFPT       _PetscFPTData;

     64:   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n < 0");
     65:   PetscMalloc(sizeof(struct _n_PetscFPT),&_PetscFPTData);
     66:   _PetscFPTData->tablesize = (3*n)/2 + 17;
     67:   if (_PetscFPTData->tablesize < n) _PetscFPTData->tablesize = PETSC_MAX_INT/4; /* overflow */
     68:   PetscMalloc(sizeof(void*)*_PetscFPTData->tablesize,&_PetscFPTData->functionpointer);
     69:   for (i=0; i<_PetscFPTData->tablesize; i++) {
     70:     _PetscFPTData->functionpointer[i] = NULL;
     71:   }
     72:   PetscMalloc(sizeof(char**)*_PetscFPTData->tablesize,&_PetscFPTData->functionname);
     73:   _PetscFPTData->count     = 0;
     74:   PetscFPTData = _PetscFPTData;
     75:   return(0);
     76: }

     80: PETSC_STATIC_INLINE unsigned long PetscHashPointer(void *ptr)
     81: {
     82: #define PETSC_FPT_HASH_FACT 79943
     83:   return((PETSC_FPT_HASH_FACT*((size_t)ptr))%PetscFPTData->tablesize);
     84: }

     88: PETSC_STATIC_INLINE PetscErrorCode PetscFPTAdd(void* key,const char* data)
     89: {
     90:   PetscInt       i,hash;

     92:   if (!data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Null function name");
     93:   if (!PetscFPTData) return(0);
     94:   hash = (PetscInt)PetscHashPointer(key);
     95:   for (i=0; i<PetscFPTData->tablesize; i++) {
     96:     if (PetscFPTData->functionpointer[hash] == key) {
     97:       PetscFPTData->functionname[hash] = (char*) data;
     98:       return(0);
     99:     } else if (!PetscFPTData->functionpointer[hash]) {
    100:       PetscFPTData->count++;
    101:       PetscFPTData->functionpointer[hash] = key;
    102:       PetscFPTData->functionname[hash] = (char*) data;
    103:       return(0);
    104:     }
    105:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
    106:   }
    107:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Function pointer table is full");
    108:   return(0);
    109: }

    113: /*
    114:     PetscFPTFind - checks if a function pointer is in the table

    116:     If data==0, then no entry exists

    118: */
    119: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTFind(void* key,char const **data)
    120: {
    121:   PetscInt hash,ii = 0;

    123:   *data = 0;
    124:   if (!PetscFPTData) return(0);
    125:   hash  = PetscHashPointer(key);
    126:   while (ii++ < PetscFPTData->tablesize) {
    127:     if (!PetscFPTData->functionpointer[hash]) break;
    128:     else if (PetscFPTData->functionpointer[hash] == key) {
    129:       *data = PetscFPTData->functionname[hash];
    130:       break;
    131:     }
    132:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
    133:   }
    134:   return(0);
    135: }

    137: #endif