Actual source code: petscctable.h

  1: #pragma once

  3: #include <petscsys.h>

  5: #if defined(PETSC_SKIP_PETSCTABLE_DEPRECATION_WARNING)
  6:   #define PETSC_TABLE_DEPRECATION_WARNING(...)
  7: #else
  8:   #define PETSC_TABLE_DEPRECATION_WARNING(repl) PETSC_DEPRECATED_FUNCTION(3, 19, 0, repl, )
  9: #endif

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

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

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

 25: PETSC_TABLE_DEPRECATION_WARNING("<no direct replacement!>") static inline unsigned long PetscHash(PetscTable ta, unsigned long x)
 26: {
 27:   return PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, x);
 28: }

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

 32: PETSC_TABLE_DEPRECATION_WARNING("<no direct replacement!>") static inline unsigned long PetscHashStep(PetscTable ta, unsigned long x)
 33: {
 34:   return PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, x);
 35: }

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

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

 53:   PetscFunctionBegin;
 54:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
 55:   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);
 56:   PetscCheck(data, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Null data");

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

 95: PETSC_TABLE_DEPRECATION_WARNING("<no direct replacement!>") static inline PetscErrorCode PetscTableAddCount(PetscTable ta, PetscInt key)
 96: {
 97:   PetscInt i, hash = (PetscInt)PetscHashMacroImplToGetAroundDeprecationWarning_Private(ta, key);
 98:   PetscInt hashstep = (PetscInt)PetscHashStepMacroImplToGetAroundDeprecationWarning_Private(ta, key);

100:   PetscFunctionBegin;
101:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
102:   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);

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

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

130:   PetscFunctionBegin;
131:   *data = 0;
132:   PetscCheck(key > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "key (value %" PetscInt_FMT ") <= 0", key);
133:   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);

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

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