Actual source code: petscctable.h

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: #ifndef __PETSCCTABLE_H
  3: #include <petscsys.h>

  5: struct _n_PetscTable {
  6:   PetscInt *keytable;
  7:   PetscInt *table;
  8:   PetscInt count;
  9:   PetscInt tablesize;
 10:   PetscInt head;
 11:   PetscInt maxkey;   /* largest key allowed */
 12: };

 14: typedef struct _n_PetscTable* PetscTable;
 15: typedef PetscInt* PetscTablePosition;

 19: PETSC_STATIC_INLINE unsigned long PetscHash(PetscTable ta,unsigned long x)
 20: {
 21: #define PETSC_HASH_FACT 79943
 23:   PetscFunctionReturn((PETSC_HASH_FACT*x)%(unsigned long)ta->tablesize);
 24: }

 26: PETSC_EXTERN PetscErrorCode PetscTableCreate(const PetscInt,PetscInt,PetscTable*);
 27: PETSC_EXTERN PetscErrorCode PetscTableCreateCopy(const PetscTable,PetscTable*);
 28: PETSC_EXTERN PetscErrorCode PetscTableDestroy(PetscTable*);
 29: PETSC_EXTERN PetscErrorCode PetscTableGetCount(const PetscTable,PetscInt*);
 30: PETSC_EXTERN PetscErrorCode PetscTableIsEmpty(const PetscTable,PetscInt*);
 31: PETSC_EXTERN PetscErrorCode PetscTableAddExpand(PetscTable,PetscInt,PetscInt,InsertMode);
 32: PETSC_EXTERN PetscErrorCode PetscTableAddCountExpand(PetscTable,PetscInt);
 33: PETSC_EXTERN PetscErrorCode PetscTableGetHeadPosition(PetscTable,PetscTablePosition*);
 34: PETSC_EXTERN PetscErrorCode PetscTableGetNext(PetscTable,PetscTablePosition*,PetscInt*,PetscInt*);
 35: PETSC_EXTERN PetscErrorCode PetscTableRemoveAll(PetscTable);

 39: PETSC_STATIC_INLINE PetscErrorCode PetscTableAdd(PetscTable ta,PetscInt key,PetscInt data,InsertMode imode)
 40: {
 42:   PetscInt       i,hash = (PetscInt)PetscHash(ta,(unsigned long)key);

 45:   if (key <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key <= 0");
 46:   if (key > ta->maxkey) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key %D is greater than largest key allowed %D",key,ta->maxkey);
 47:   if (!data) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Null data");

 49:   for (i=0; i<ta->tablesize; i++) {
 50:     if (ta->keytable[hash] == key) {
 51:       switch (imode) {
 52:       case INSERT_VALUES:
 53:         ta->table[hash] = data; /* over write */
 54:         break;
 55:       case ADD_VALUES:
 56:         ta->table[hash] += data;
 57:         break;
 58:       case MAX_VALUES:
 59:         ta->table[hash] = PetscMax(ta->table[hash],data);
 60:         break;
 61:       case NOT_SET_VALUES:
 62:       case INSERT_ALL_VALUES:
 63:       case ADD_ALL_VALUES:
 64:       case INSERT_BC_VALUES:
 65:       case ADD_BC_VALUES:
 66:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported InsertMode");
 67:       }
 68:       return(0);
 69:     } else if (!ta->keytable[hash]) {
 70:       if (ta->count < 5*(ta->tablesize/6) - 1) {
 71:         ta->count++; /* add */
 72:         ta->keytable[hash] = key;
 73:         ta->table[hash] = data;
 74:       } else {
 75:         PetscTableAddExpand(ta,key,data,imode);
 76:       }
 77:       return(0);
 78:     }
 79:     hash = (hash == (ta->tablesize-1)) ? 0 : hash+1;
 80:   }
 81:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Full table");
 82:   /* return(0); */
 83: }

 87: PETSC_STATIC_INLINE PetscErrorCode  PetscTableAddCount(PetscTable ta,PetscInt key)
 88: {
 90:   PetscInt       i,hash = (PetscInt)PetscHash(ta,(unsigned long)key);

 93:   if (key <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key <= 0");
 94:   if (key > ta->maxkey) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key %D is greater than largest key allowed %D",key,ta->maxkey);

 96:   for (i=0; i<ta->tablesize; i++) {
 97:     if (ta->keytable[hash] == key) {
 98:       return(0);
 99:     } else if (!ta->keytable[hash]) {
100:       if (ta->count < 5*(ta->tablesize/6) - 1) {
101:         ta->count++; /* add */
102:         ta->keytable[hash] = key;
103:         ta->table[hash] = ta->count;
104:       } else {
105:         PetscTableAddCountExpand(ta,key);
106:       }
107:       return(0);
108:     }
109:     hash = (hash == (ta->tablesize-1)) ? 0 : hash+1;
110:   }
111:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Full table");
112:   /* return(0); */
113: }


118: /*
119:     PetscTableFind - checks if a key is in the table

121:     If data==0, then no table entry exists.

123: */
124: PETSC_STATIC_INLINE PetscErrorCode  PetscTableFind(PetscTable ta,PetscInt key,PetscInt *data)
125: {
126:   PetscInt hash,ii = 0;

129:   *data = 0;
130:   if (key <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Key <= 0");
131:   if (key > ta->maxkey) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key %D is greater than largest key allowed %D",key,ta->maxkey);

133:   hash  = (PetscInt)PetscHash(ta,(unsigned long)key);
134:   while (ii++ < ta->tablesize) {
135:     if (!ta->keytable[hash]) break;
136:     else if (ta->keytable[hash] == key) {
137:       *data = ta->table[hash];
138:       break;
139:     }
140:     hash = (hash == (ta->tablesize-1)) ? 0 : hash+1;
141:   }
142:   return(0);
143: }

145: /* Reset __FUNCT__ in case the user does not define it themselves */

149: #endif