Actual source code: petscctable.h

  1: #ifndef PETSCCTABLE_H
  2: #define PETSCCTABLE_H

  4: #include <petscsys.h>

  6: #if defined(PETSC_SKIP_PETSCTABLE_DEPRECATION_WARNING)
  7:   #define PETSC_TABLE_DEPRECATION_WARNING(...)
  8: #else
  9:   #define PETSC_TABLE_DEPRECATION_WARNING(...) PETSC_DEPRECATED_FUNCTION(__VA_ARGS__ "(since version 3.19)")
 10: #endif

 12: struct _n_PetscTable {
 13:   PetscInt *keytable;
 14:   PetscInt *table;
 15:   PetscInt  count;
 16:   PetscInt  tablesize;
 17:   PetscInt  head;
 18:   PetscInt  maxkey; /* largest key allowed */
 19: };

 21: typedef struct _n_PetscTable *PetscTable;
 22: typedef PetscInt             *PetscTablePosition;

 24: #define PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, x) (((unsigned long)(x)) % ((unsigned long)((ta)->tablesize)))

 26: PETSC_TABLE_DEPRECATION_WARNING("") static inline unsigned long PetscHash(PetscTable ta, unsigned long x)
 27: {
 28:   return PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, x);
 29: }

 31: #define PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, x) (1 + (((unsigned long)(x)) % ((unsigned long)((ta)->tablesize - 1))))

 33: PETSC_TABLE_DEPRECATION_WARNING("") static inline unsigned long PetscHashStep(PetscTable ta, unsigned long x)
 34: {
 35:   return PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, x);
 36: }

 38: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapICreateWithSize()") PETSC_EXTERN PetscErrorCode PetscTableCreate(PetscInt, PetscInt, PetscTable *);
 39: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIDuplicate()") PETSC_EXTERN PetscErrorCode PetscTableCreateCopy(PetscTable, PetscTable *);
 40: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIDestroy()") PETSC_EXTERN PetscErrorCode PetscTableDestroy(PetscTable *);
 41: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIGetSize()") PETSC_EXTERN PetscErrorCode PetscTableGetCount(PetscTable, PetscInt *);
 42: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIGetSize()") PETSC_EXTERN PetscErrorCode PetscTableIsEmpty(PetscTable, PetscInt *);
 43: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapISetWithMode()") PETSC_EXTERN PetscErrorCode PetscTableAddExpand(PetscTable, PetscInt, PetscInt, InsertMode);
 44: PETSC_TABLE_DEPRECATION_WARNING("") PETSC_EXTERN PetscErrorCode PetscTableAddCountExpand(PetscTable, PetscInt);
 45: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHashIterBegin()") PETSC_EXTERN PetscErrorCode PetscTableGetHeadPosition(PetscTable, PetscTablePosition *);
 46: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHashIterNext(), PetscHashIterGetKey(), and PetscHashIterGetVal()") PETSC_EXTERN PetscErrorCode PetscTableGetNext(PetscTable, PetscTablePosition *, PetscInt *, PetscInt *);
 47: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIClear()") PETSC_EXTERN PetscErrorCode PetscTableRemoveAll(PetscTable);

 49: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapISetWithMode()") static inline PetscErrorCode PetscTableAdd(PetscTable ta, PetscInt key, PetscInt data, InsertMode imode)
 50: {
 51:   PetscInt i, hash = (PetscInt)PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, key);
 52:   PetscInt hashstep = (PetscInt)PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, key);

 54:   PetscFunctionBegin;
 55:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
 56:   PetscCheck(key <= ta->maxkey, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key %" PetscInt_FMT " is greater than largest key allowed %" PetscInt_FMT, key, ta->maxkey);
 57:   PetscCheck(data, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Null data");

 59:   for (i = 0; i < ta->tablesize; i++) {
 60:     if (ta->keytable[hash] == key) {
 61:       switch (imode) {
 62:       case INSERT_VALUES:
 63:         ta->table[hash] = data; /* over write */
 64:         break;
 65:       case ADD_VALUES:
 66:         ta->table[hash] += data;
 67:         break;
 68:       case MAX_VALUES:
 69:         ta->table[hash] = PetscMax(ta->table[hash], data);
 70:         break;
 71:       case MIN_VALUES:
 72:         ta->table[hash] = PetscMin(ta->table[hash], data);
 73:         break;
 74:       case NOT_SET_VALUES:
 75:       case INSERT_ALL_VALUES:
 76:       case ADD_ALL_VALUES:
 77:       case INSERT_BC_VALUES:
 78:       case ADD_BC_VALUES:
 79:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Unsupported InsertMode");
 80:       }
 81:       PetscFunctionReturn(PETSC_SUCCESS);
 82:     } else if (!ta->keytable[hash]) {
 83:       if (ta->count < 5 * (ta->tablesize / 6) - 1) {
 84:         ta->count++; /* add */
 85:         ta->keytable[hash] = key;
 86:         ta->table[hash]    = data;
 87:       } else PetscCall(PetscTableAddExpand(ta, key, data, imode));
 88:       PetscFunctionReturn(PETSC_SUCCESS);
 89:     }
 90:     hash = (hash + hashstep) % ta->tablesize;
 91:   }
 92:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_COR, "Full table");
 93:   /* PetscFunctionReturn(PETSC_SUCCESS); */
 94: }

 96: PETSC_TABLE_DEPRECATION_WARNING("") static inline PetscErrorCode PetscTableAddCount(PetscTable ta, PetscInt key)
 97: {
 98:   PetscInt i, hash = (PetscInt)PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, key);
 99:   PetscInt hashstep = (PetscInt)PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, key);

101:   PetscFunctionBegin;
102:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
103:   PetscCheck(key <= ta->maxkey, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key %" PetscInt_FMT " is greater than largest key allowed %" PetscInt_FMT, key, ta->maxkey);

105:   for (i = 0; i < ta->tablesize; i++) {
106:     if (ta->keytable[hash] == key) {
107:       PetscFunctionReturn(PETSC_SUCCESS);
108:     } else if (!ta->keytable[hash]) {
109:       if (ta->count < 5 * (ta->tablesize / 6) - 1) {
110:         ta->count++; /* add */
111:         ta->keytable[hash] = key;
112:         ta->table[hash]    = ta->count;
113:       } else PetscCall(PetscTableAddCountExpand(ta, key));
114:       PetscFunctionReturn(PETSC_SUCCESS);
115:     }
116:     hash = (hash + hashstep) % ta->tablesize;
117:   }
118:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_COR, "Full table");
119:   /* PetscFunctionReturn(PETSC_SUCCESS); */
120: }

122: /*
123:     PetscTableFind - finds data in table from a given key, if the key is valid but not in the table returns 0
124: */
125: PETSC_TABLE_DEPRECATION_WARNING("Use PetscHMapIGetWithDefault()") static inline PetscErrorCode PetscTableFind(PetscTable ta, PetscInt key, PetscInt *data)
126: {
127:   PetscInt ii       = 0;
128:   PetscInt hash     = (PetscInt)PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, key);
129:   PetscInt hashstep = (PetscInt)PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, key);

131:   PetscFunctionBegin;
132:   *data = 0;
133:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
134:   PetscCheck(key <= ta->maxkey, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key %" PetscInt_FMT " is greater than largest key allowed %" PetscInt_FMT, key, ta->maxkey);

136:   while (ii++ < ta->tablesize) {
137:     if (!ta->keytable[hash]) break;
138:     else if (ta->keytable[hash] == key) {
139:       *data = ta->table[hash];
140:       break;
141:     }
142:     hash = (hash + hashstep) % ta->tablesize;
143:   }
144:   PetscFunctionReturn(PETSC_SUCCESS);
145: }

147: #undef PetscHashMacroImplToGetAroundDeprecationWarning_Private
148: #undef PetscHashStepMacroImplToGetAroundDeprecationWarning_Private
149: #undef PETSC_TABLE_DEPRECATION_WARNING

151: #endif /* PETSCCTABLE_H */