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: #undef __FUNCT__
24: /*@C
25: ParameterDictCreate - Creates an empty parameter dictionary.
27: Collective on MPI_Comm
28:
29: Input Parameter:
30: . comm - The MPI communicator to use
32: Output Parameter:
33: . dict - The ParameterDict object
35: Level: beginner
37: .keywords: ParameterDict, create
38: .seealso: ParameterDictDestroy(), ParameterDictSetObject(), ParameterDictGetObject()
39: @*/
40: int ParameterDictCreate(MPI_Comm comm, ParameterDict *dict)
41: {
42: ParameterDict d;
46: PetscHeaderCreate(d, _p_Dict, int, DICT_COOKIE, 0, "Dict", comm, ParameterDictDestroy, PETSC_NULL);
47: PetscLogObjectCreate(d);
48: PetscLogObjectMemory(d, sizeof(struct _p_Dict));
50: d->dictHead = PETSC_NULL;
51: *dict = d;
52: return(0);
53: }
55: #undef __FUNCT__
57: /*@C
58: ParameterDictDestroy - Destroys a parameter dictionary object.
60: Not Collective
62: Input Parameter:
63: . dict - The ParameterDict
65: Level: beginner
67: .keywords: ParameterDict, destroy
68: .seealso: ParameterDictCreate(), ParameterDictSetObject(), ParameterDictGetObject()
69: @*/
70: int ParameterDictDestroy(ParameterDict dict)
71: {
72: DictNode *node, *next;
73: int ierr;
77: if (--dict->refct > 0)
78: return(0);
79: next = dict->dictHead;
80: while(next != PETSC_NULL) {
81: node = next;
82: next = node->next;
83: PetscFree(node->key);
84: PetscFree(node);
85: }
86: PetscLogObjectDestroy(dict);
87: PetscHeaderDestroy(dict);
88: return(0);
89: }
91: #undef __FUNCT__
93: /*@C
94: ParameterDictSetInteger - Adds an integer argument to the dictionary
96: Collective on ParameterDict
98: Input Parameters:
99: + dict - The ParameterDict
100: . key - The argument name
101: - data - The integer to store
103: Level: beginner
105: .keywords: ParameterDict, set, integer
106: .seealso: ParameterDictSetDouble(), ParameterDictSetObject(), ParameterDictGetInteger()
107: @*/
108: int ParameterDictSetInteger(ParameterDict dict, const char key[], int data)
109: {
110: DictNode *node;
111: int ierr;
116: PetscMalloc(sizeof(DictNode), &node);
117: node->intData = data;
118: node->realData = 0.0;
119: node->data = PETSC_NULL;
120: PetscStrallocpy(key, &node->key);
121: /* Push node onto the linked list */
122: node->next = dict->dictHead;
123: dict->dictHead = node;
124: return(0);
125: }
127: #undef __FUNCT__
129: /*@C
130: ParameterDictSetDouble - Adds a real argument to the dictionary
132: Collective on ParameterDict
134: Input Parameters:
135: + dict - The ParameterDict
136: . key - The argument name
137: - data - The double to store
139: Level: beginner
141: .keywords: ParameterDict, set, double
142: .seealso: ParameterDictSetInteger(), ParameterDictSetObject(), ParameterDictGetDouble()
143: @*/
144: int ParameterDictSetDouble(ParameterDict dict, const char key[], double data)
145: {
146: DictNode *node;
147: int ierr;
152: PetscMalloc(sizeof(DictNode), &node);
153: node->intData = 0;
154: node->realData = data;
155: node->data = PETSC_NULL;
156: PetscStrallocpy(key, &node->key);
157: /* Push node onto the linked list */
158: node->next = dict->dictHead;
159: dict->dictHead = node;
160: return(0);
161: }
163: #undef __FUNCT__
165: /*@C
166: ParameterDictSetObject - Adds an object argument to the dictionary
168: Collective on ParameterDict
170: Input Parameters:
171: + dict - The ParameterDict
172: . key - The argument name
173: - data - The object to store
175: Level: beginner
177: .keywords: ParameterDict, set, object
178: .seealso: ParameterDictSetInteger(), ParameterDictSetDouble(), ParameterDictGetObject()
179: @*/
180: int ParameterDictSetObject(ParameterDict dict, const char key[], void *data)
181: {
182: DictNode *node;
183: int ierr;
188: PetscMalloc(sizeof(DictNode), &node);
189: node->intData = 0;
190: node->realData = 0.0;
191: node->data = data;
192: PetscStrallocpy(key, &node->key);
193: /* Push node onto the linked list */
194: node->next = dict->dictHead;
195: dict->dictHead = node;
196: return(0);
197: }
199: #undef __FUNCT__
201: /*@C
202: ParameterDictRemove - Removes an argument from the dictionary
204: Collective on ParameterDict
206: Input Parameters:
207: + dict - The ParameterDict
208: - key - The argument name
210: Level: beginner
212: .keywords: ParameterDict, remove
213: .seealso: ParameterDictSetObject(), ParameterDictGetObject()
214: @*/
215: int ParameterDictRemove(ParameterDict dict, const char key[])
216: {
217: DictNode *node, *prev;
218: PetscTruth found;
219: int ierr;
224: found = PETSC_FALSE;
225: node = dict->dictHead;
226: prev = node;
227: /* Check the head separately */
228: PetscStrcmp(key, node->key, &found);
229: if (found == PETSC_TRUE) {
230: dict->dictHead = node->next;
231: PetscFree(node->key);
232: PetscFree(node);
233: }
234: /* Check the rest */
235: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
236: PetscStrcmp(key, node->key, &found);
237: if (found == PETSC_TRUE) {
238: prev->next = node->next;
239: PetscFree(node->key);
240: PetscFree(node);
241: }
242: prev = node;
243: node = node->next;
244: }
245: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
246: return(0);
247: }
249: #undef __FUNCT__
251: /*@C
252: ParameterDictGetInteger - Gets an integer argument from the dictionary
254: Not collective
256: Input Parameters:
257: + dict - The ParameterDict
258: - key - The argument name
260: Output Parameter:
261: . data - The integer
263: Level: beginner
265: .keywords: ParameterDict, get, integer
266: .seealso: ParameterDictGetDouble(), ParameterDictGetObject(), ParameterDictSetInteger()
267: @*/
268: int ParameterDictGetInteger(ParameterDict dict, const char key[], int *data)
269: {
270: DictNode *node;
271: PetscTruth found;
272: int ierr;
278: found = PETSC_FALSE;
279: node = dict->dictHead;
280: /* Check the rest */
281: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
282: PetscStrcmp(key, node->key, &found);
283: if (found == PETSC_TRUE) {
284: *data = node->intData;
285: }
286: node = node->next;
287: }
288: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
289: return(0);
290: }
292: #undef __FUNCT__
294: /*@C
295: ParameterDictGetDouble - Gets a real argument from the dictionary
297: Not collective
299: Input Parameters:
300: + dict - The ParameterDict
301: - key - The argument name
303: Output Parameter:
304: . data - The double
306: Level: beginner
308: .keywords: ParameterDict, get, double
309: .seealso: ParameterDictGetInteger(), ParameterDictGetObject(), ParameterDictSetDouble()
310: @*/
311: int ParameterDictGetDouble(ParameterDict dict, const char key[], double *data)
312: {
313: DictNode *node;
314: PetscTruth found;
315: int ierr;
321: found = PETSC_FALSE;
322: node = dict->dictHead;
323: /* Check the rest */
324: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
325: PetscStrcmp(key, node->key, &found);
326: if (found == PETSC_TRUE) {
327: *data = node->realData;
328: }
329: node = node->next;
330: }
331: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
332: return(0);
333: }
335: #undef __FUNCT__
337: /*@C
338: ParameterDictGetObject - Gets an object argument from the dictionary
340: Not collective
342: Input Parameters:
343: + dict - The ParameterDict
344: - key - The argument name
346: Output Parameter:
347: . data - The object
349: Level: beginner
351: .keywords: ParameterDict, get, object
352: .seealso: ParameterDictGetInteger(), ParameterDictGetDouble(), ParameterDictSetObject()
353: @*/
354: int ParameterDictGetObject(ParameterDict dict, const char key[], void **data)
355: {
356: DictNode *node;
357: PetscTruth found;
358: int ierr;
364: found = PETSC_FALSE;
365: node = dict->dictHead;
366: /* Check the rest */
367: while((node != PETSC_NULL) && (found == PETSC_FALSE)) {
368: PetscStrcmp(key, node->key, &found);
369: if (found == PETSC_TRUE) {
370: *data = node->data;
371: }
372: node = node->next;
373: }
374: if (found == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Key not found in dictionary");
375: return(0);
376: }