Actual source code: petscfptimpl.h

      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: PETSC_INTERN PetscFPT PetscFPTData;

     20: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTView(PetscViewer viewer)
     21: {
     22:   PetscInt       i;

     24:   if (!PetscFPTData) return(0);
     25:   for (i=0; i<PetscFPTData->tablesize; i++) {
     26:     if (PetscFPTData->functionpointer[i]) {
     27:       printf("%s()\n",PetscFPTData->functionname[i]);
     28:     }
     29:   }
     30:   return(0);
     31: }

     33: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTDestroy(void)
     34: {
     36:   PetscFPT       _PetscFPTData = PetscFPTData;

     38:   PetscFPTData = NULL;
     39:   if (!_PetscFPTData) return 0;
     40:   PetscFree((_PetscFPTData)->functionpointer);
     41:   PetscFree((_PetscFPTData)->functionname);
     42:   PetscFree(_PetscFPTData);
     43:   return(0);
     44: }

     46: /*
     47:    PetscFPTCreate  Creates a PETSc look up table from function pointers to strings

     49:    Input Parameters:
     50: .     n - expected number of keys

     52: */
     53: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTCreate(PetscInt n)
     54: {
     56:   PetscInt       i;
     57:   PetscFPT       _PetscFPTData;

     59:   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"n < 0");
     60:   /* Cannot use PetscNew() here because it is not yet defined in the include file chain */
     61:   PetscMalloc(sizeof(struct _n_PetscFPT),&_PetscFPTData);
     62:   _PetscFPTData->tablesize = (3*n)/2 + 17;
     63:   if (_PetscFPTData->tablesize < n) _PetscFPTData->tablesize = PETSC_MAX_INT/4; /* overflow */
     64:   PetscMalloc(sizeof(void*)*_PetscFPTData->tablesize,&_PetscFPTData->functionpointer);
     65:   for (i=0; i<_PetscFPTData->tablesize; i++) {
     66:     _PetscFPTData->functionpointer[i] = NULL;
     67:   }
     68:   PetscMalloc(sizeof(char**)*_PetscFPTData->tablesize,&_PetscFPTData->functionname);
     69:   _PetscFPTData->count     = 0;
     70:   PetscFPTData = _PetscFPTData;
     71:   return(0);
     72: }

     74: PETSC_STATIC_INLINE unsigned long PetscFPTHashPointer(void *ptr)
     75: {
     76: #define PETSC_FPT_HASH_FACT 79943
     77:   return((PETSC_FPT_HASH_FACT*((size_t)ptr))%PetscFPTData->tablesize);
     78: }

     80: PETSC_STATIC_INLINE PetscErrorCode PetscFPTAdd(void* key,const char* data)
     81: {
     82:   PetscInt       i,hash;

     84:   if (!data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Null function name");
     85:   if (!PetscFPTData) return(0);
     86:   hash = (PetscInt)PetscFPTHashPointer(key);
     87:   for (i=0; i<PetscFPTData->tablesize; i++) {
     88:     if (PetscFPTData->functionpointer[hash] == key) {
     89:       PetscFPTData->functionname[hash] = (char*) data;
     90:       return(0);
     91:     } else if (!PetscFPTData->functionpointer[hash]) {
     92:       PetscFPTData->count++;
     93:       PetscFPTData->functionpointer[hash] = key;
     94:       PetscFPTData->functionname[hash] = (char*) data;
     95:       return(0);
     96:     }
     97:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
     98:   }
     99:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Function pointer table is full");
    100:   return(0);
    101: }

    103: /*
    104:     PetscFPTFind - checks if a function pointer is in the table

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

    108: */
    109: PETSC_STATIC_INLINE PetscErrorCode  PetscFPTFind(void* key,char const **data)
    110: {
    111:   PetscInt hash,ii = 0;

    113:   *data = NULL;
    114:   if (!PetscFPTData) return(0);
    115:   hash  = PetscFPTHashPointer(key);
    116:   while (ii++ < PetscFPTData->tablesize) {
    117:     if (!PetscFPTData->functionpointer[hash]) break;
    118:     else if (PetscFPTData->functionpointer[hash] == key) {
    119:       *data = PetscFPTData->functionname[hash];
    120:       break;
    121:     }
    122:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
    123:   }
    124:   return(0);
    125: }

    127: #endif