Actual source code: petscctable.h
petsc-3.11.4 2019-09-28
1: #ifndef PETSCCTABLE_H
2: #define 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;
17: PETSC_STATIC_INLINE unsigned long PetscHash(PetscTable ta,unsigned long x)
18: {
19: return(x%(unsigned long)ta->tablesize);
20: }
22: PETSC_STATIC_INLINE unsigned long PetscHashStep(PetscTable ta,unsigned long x)
23: {
24: return(1+(x%(unsigned long)(ta->tablesize-1)));
25: }
27: PETSC_EXTERN PetscErrorCode PetscTableCreate(const PetscInt,PetscInt,PetscTable*);
28: PETSC_EXTERN PetscErrorCode PetscTableCreateCopy(const PetscTable,PetscTable*);
29: PETSC_EXTERN PetscErrorCode PetscTableDestroy(PetscTable*);
30: PETSC_EXTERN PetscErrorCode PetscTableGetCount(const PetscTable,PetscInt*);
31: PETSC_EXTERN PetscErrorCode PetscTableIsEmpty(const PetscTable,PetscInt*);
32: PETSC_EXTERN PetscErrorCode PetscTableAddExpand(PetscTable,PetscInt,PetscInt,InsertMode);
33: PETSC_EXTERN PetscErrorCode PetscTableAddCountExpand(PetscTable,PetscInt);
34: PETSC_EXTERN PetscErrorCode PetscTableGetHeadPosition(PetscTable,PetscTablePosition*);
35: PETSC_EXTERN PetscErrorCode PetscTableGetNext(PetscTable,PetscTablePosition*,PetscInt*,PetscInt*);
36: PETSC_EXTERN PetscErrorCode PetscTableRemoveAll(PetscTable);
38: PETSC_STATIC_INLINE PetscErrorCode PetscTableAdd(PetscTable ta,PetscInt key,PetscInt data,InsertMode imode)
39: {
41: PetscInt i,hash = (PetscInt)PetscHash(ta,(unsigned long)key);
42: PetscInt hashstep = (PetscInt)PetscHashStep(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 + hashstep)%ta->tablesize;
80: }
81: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Full table");
82: /* return(0); */
83: }
85: PETSC_STATIC_INLINE PetscErrorCode PetscTableAddCount(PetscTable ta,PetscInt key)
86: {
88: PetscInt i,hash = (PetscInt)PetscHash(ta,(unsigned long)key);
89: PetscInt hashstep = (PetscInt)PetscHashStep(ta,(unsigned long)key);
92: if (key <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key <= 0");
93: if (key > ta->maxkey) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key %D is greater than largest key allowed %D",key,ta->maxkey);
95: for (i=0; i<ta->tablesize; i++) {
96: if (ta->keytable[hash] == key) {
97: return(0);
98: } else if (!ta->keytable[hash]) {
99: if (ta->count < 5*(ta->tablesize/6) - 1) {
100: ta->count++; /* add */
101: ta->keytable[hash] = key;
102: ta->table[hash] = ta->count;
103: } else {
104: PetscTableAddCountExpand(ta,key);
105: }
106: return(0);
107: }
108: hash = (hash + hashstep)%ta->tablesize;
109: }
110: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_COR,"Full table");
111: /* return(0); */
112: }
114: /*
115: PetscTableFind - checks if a key is in the table
117: If data==0, then no table entry exists.
119: */
120: PETSC_STATIC_INLINE PetscErrorCode PetscTableFind(PetscTable ta,PetscInt key,PetscInt *data)
121: {
122: PetscInt ii = 0;
123: PetscInt hash = (PetscInt)PetscHash(ta,(unsigned long)key);
124: PetscInt hashstep = (PetscInt)PetscHashStep(ta,(unsigned long)key);
127: *data = 0;
128: if (key <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Key <= 0");
129: if (key > ta->maxkey) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"key %D is greater than largest key allowed %D",key,ta->maxkey);
131: while (ii++ < ta->tablesize) {
132: if (!ta->keytable[hash]) break;
133: else if (ta->keytable[hash] == key) {
134: *data = ta->table[hash];
135: break;
136: }
137: hash = (hash + hashstep)%ta->tablesize;
138: }
139: return(0);
140: }
142: #endif