Actual source code: dict.c
1: #ifdef PETSC_RCS_HEADER
2: static char vcid[] = "$Id: dict.c,v 1.3 2000/01/10 03:27:01 knepley Exp $";
3: #endif
5: #include petscsys.h
7: int DICT_COOKIE;
9: typedef struct _p_DictNode {
10: char *key;
11: int intData;
12: double realData;
13: void *data;
14: struct _p_DictNode *next;
15: } DictNode;
17: struct _p_Dict {
18: PETSCHEADER(int)
19: DictNode *dictHead;
20: };
22: /*@C
23: ParameterDictCreate - Creates an empty parameter dictionary.
25: Collective on MPI_Comm
26:
27: Input Parameter:
28: . comm - The MPI communicator to use
30: Output Parameter:
31: . dict - The ParameterDict object
33: Level: beginner
35: .keywords: ParameterDict, create
36: .seealso: ParameterDictDestroy(), ParameterDictSetObject(), ParameterDictGetObject()
37: @*/
38: int ParameterDictCreate(MPI_Comm comm, ParameterDict *dict)
39: {
40: ParameterDict d;
44: PetscHeaderCreate(d, _p_Dict, int, DICT_COOKIE, 0, "Dict", comm, ParameterDictDestroy, PETSC_NULL);
45: PetscLogObjectCreate(d);
46: PetscLogObjectMemory(d, sizeof(struct _p_Dict));
48: d->dictHead = PETSC_NULL;
49: *dict = d;
50: return(0);
51: }
53: /*@C
54: ParameterDictDestroy - Destroys a parameter dictionary object.
56: Not Collective
58: Input Parameter:
59: . dict - The ParameterDict
61: Level: beginner
63: .keywords: ParameterDict, destroy
64: .seealso: ParameterDictCreate(), ParameterDictSetObject(), ParameterDictGetObject()
65: @*/
66: int ParameterDictDestroy(ParameterDict dict)
67: {
68: DictNode *node, *next;
69: int ierr;
73: if (--dict->refct > 0)
74: return(0);
75: next = dict->dictHead;
76: while(next != PETSC_NULL) {
77: node = next;
78: next = node->next;
79: PetscFree(node->key);
80: PetscFree(node);
81: }
82: PetscLogObjectDestroy(dict);
83: PetscHeaderDestroy(dict);
84: return(0);
85: }
87: /*@C
88: ParameterDictSetInteger - Adds an integer argument to the dictionary
90: Collective on ParameterDict
92: Input Parameters:
93: + dict - The ParameterDict
94: . key - The argument name
95: - data - The integer to store
97: Level: beginner
99: .keywords: ParameterDict, set, integer
100: .seealso: ParameterDictSetDouble(), ParameterDictSetObject(), ParameterDictGetInteger()
101: @*/
102: int ParameterDictSetInteger(ParameterDict dict, const char key[], int data)
103: {
104: DictNode *node;
105: int ierr;
110: PetscMalloc(sizeof(DictNode), &node);
111: node->intData = data;
112: node->realData = 0.0;
113: node->data = PETSC_NULL;
114: PetscStrallocpy(key, &node->key);
115: /* Push node onto the linked list */
116: node->next = dict->dictHead;
117: dict->dictHead = node;
118: return(0);
119: }
121: /*@C
122: ParameterDictSetDouble - Adds a real argument to the dictionary
124: Collective on ParameterDict
126: Input Parameters:
127: + dict - The ParameterDict
128: . key - The argument name
129: - data - The double to store
131: Level: beginner
133: .keywords: ParameterDict, set, double
134: .seealso: ParameterDictSetInteger(), ParameterDictSetObject(), ParameterDictGetDouble()
135: @*/
136: int ParameterDictSetDouble(ParameterDict dict, const char key[], double data)
137: {
138: DictNode *node;
139: int ierr;
144: PetscMalloc(sizeof(DictNode), &node);
145: node->intData = 0;
146: node->realData = data;
147: node->data = PETSC_NULL;
148: PetscStrallocpy(key, &node->key);
149: /* Push node onto the linked list */
150: node->next = dict->dictHead;
151: dict->dictHead = node;
152: return(0);
153: }
155: /*@C
156: ParameterDictSetObject - Adds an object argument to the dictionary
158: Collective on ParameterDict
160: Input Parameters:
161: + dict - The ParameterDict
162: . key - The argument name
163: - data - The object to store
165: Level: beginner
167: .keywords: ParameterDict, set, object
168: .seealso: ParameterDictSetInteger(), ParameterDictSetDouble(), ParameterDictGetObject()
169: @*/
170: int ParameterDictSetObject(ParameterDict dict, const char key[], void *data)
171: {
172: DictNode *node;
173: int ierr;
178: PetscMalloc(sizeof(DictNode), &node);
179: node->intData = 0;
180: node->realData = 0.0;
181: node->data = data;
182: PetscStrallocpy(key, &node->key);
183: /* Push node onto the linked list */
184: node->next = dict->dictHead;
185: dict->dictHead = node;
186: return(0);
187: }
189: /*@C
190: ParameterDictRemove - Removes an argument from the dictionary
192: Collective on ParameterDict
194: Input Parameters:
195: + dict - The ParameterDict
196: - key - The argument name
198: Level: beginner
200: .keywords: ParameterDict, remove
201: .seealso: ParameterDictSetObject(), ParameterDictGetObject()
202: @*/
203: int ParameterDictRemove(ParameterDict dict, const char key[])
204: {
205: DictNode *node, *prev;
206: PetscTruth found;
207: int ierr;
212: found = PETSC_FALSE;
213: node = dict->dictHead;
214: prev = node;
215: /* Check the head separately */
216: PetscStrcmp(key, node->key, &found);
217: if (found == PETSC_TRUE) {
218: dict->dictHead = node->next;
219: PetscFree(node->key);
220: PetscFree(node);
221: }
222: /* Check the rest */
223: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
224: PetscStrcmp(key, node->key, &found);
225: if (found == PETSC_TRUE) {
226: prev->next = node->next;
227: PetscFree(node->key);
228: PetscFree(node);
229: }
230: prev = node;
231: node = node->next;
232: }
233: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
234: return(0);
235: }
237: /*@C
238: ParameterDictGetInteger - Gets an integer argument from the dictionary
240: Not collective
242: Input Parameters:
243: + dict - The ParameterDict
244: - key - The argument name
246: Output Parameter:
247: . data - The integer
249: Level: beginner
251: .keywords: ParameterDict, get, integer
252: .seealso: ParameterDictGetDouble(), ParameterDictGetObject(), ParameterDictSetInteger()
253: @*/
254: int ParameterDictGetInteger(ParameterDict dict, const char key[], int *data)
255: {
256: DictNode *node;
257: PetscTruth found;
258: int ierr;
264: found = PETSC_FALSE;
265: node = dict->dictHead;
266: /* Check the rest */
267: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
268: PetscStrcmp(key, node->key, &found);
269: if (found == PETSC_TRUE) {
270: *data = node->intData;
271: }
272: node = node->next;
273: }
274: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
275: return(0);
276: }
278: /*@C
279: ParameterDictGetDouble - Gets a real argument from the dictionary
281: Not collective
283: Input Parameters:
284: + dict - The ParameterDict
285: - key - The argument name
287: Output Parameter:
288: . data - The double
290: Level: beginner
292: .keywords: ParameterDict, get, double
293: .seealso: ParameterDictGetInteger(), ParameterDictGetObject(), ParameterDictSetDouble()
294: @*/
295: int ParameterDictGetDouble(ParameterDict dict, const char key[], double *data)
296: {
297: DictNode *node;
298: PetscTruth found;
299: int ierr;
305: found = PETSC_FALSE;
306: node = dict->dictHead;
307: /* Check the rest */
308: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
309: PetscStrcmp(key, node->key, &found);
310: if (found == PETSC_TRUE) {
311: *data = node->realData;
312: }
313: node = node->next;
314: }
315: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
316: return(0);
317: }
319: /*@C
320: ParameterDictGetObject - Gets an object argument from the dictionary
322: Not collective
324: Input Parameters:
325: + dict - The ParameterDict
326: - key - The argument name
328: Output Parameter:
329: . data - The object
331: Level: beginner
333: .keywords: ParameterDict, get, object
334: .seealso: ParameterDictGetInteger(), ParameterDictGetDouble(), ParameterDictSetObject()
335: @*/
336: int ParameterDictGetObject(ParameterDict dict, const char key[], void **data)
337: {
338: DictNode *node;
339: PetscTruth found;
340: int ierr;
346: found = PETSC_FALSE;
347: node = dict->dictHead;
348: /* Check the rest */
349: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
350: PetscStrcmp(key, node->key, &found);
351: if (found == PETSC_TRUE) {
352: *data = node->data;
353: }
354: node = node->next;
355: }
356: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
357: return(0);
358: }