Actual source code: olist.c

  1: #define PETSC_DLL
  2: /*
  3:          Provides a general mechanism to maintain a linked list of PETSc objects.
  4:      This is used to allow PETSc objects to carry a list of "composed" objects
  5: */
 6:  #include petscsys.h

  8: struct _n_PetscOList {
  9:     char        name[256];
 10:     PetscObject obj;
 11:     PetscOList  next;
 12: };

 16: /*

 18:        Notes: Replaces item if it is already in list. Removes item if you pass in a 
 19:               PETSC_NULL object.    

 21: .seealso: PetscOListDestroy()
 22: */
 23: PetscErrorCode  PetscOListAdd(PetscOList *fl,const char name[],PetscObject obj)
 24: {
 25:   PetscOList     olist,nlist,prev;
 27:   PetscTruth     match;


 31:   if (!obj) { /* this means remove from list if it is there */
 32:     nlist = *fl; prev = 0;
 33:     while (nlist) {
 34:       PetscStrcmp(name,nlist->name,&match);
 35:       if (match) {  /* found it already in the list */
 36:         PetscObjectDereference(nlist->obj);
 37:         if (prev) prev->next = nlist->next;
 38:         else if (nlist->next) {
 39:           *fl = nlist->next;
 40:         } else {
 41:           *fl = 0;
 42:         }
 43:         PetscFree(nlist);
 44:         return(0);
 45:       }
 46:       prev  = nlist;
 47:       nlist = nlist->next;
 48:     }
 49:     return(0); /* did not find it to remove */
 50:   }
 51:   /* look for it already in list */
 52:   nlist = *fl;
 53:   while (nlist) {
 54:     PetscStrcmp(name,nlist->name,&match);
 55:     if (match) {  /* found it in the list */
 56:       PetscObjectReference(obj);
 57:       PetscObjectDereference(nlist->obj);
 58:       nlist->obj = obj;
 59:       return(0);
 60:     }
 61:     nlist = nlist->next;
 62:   }

 64:   /* add it to list, because it was not already there */

 66:   PetscNew(struct _n_PetscOList,&olist);
 67:   olist->next = 0;
 68:   olist->obj  = obj;
 69:   PetscObjectReference(obj);
 70:   PetscStrcpy(olist->name,name);

 72:   if (!*fl) {
 73:     *fl = olist;
 74:   } else { /* go to end of list */
 75:     nlist = *fl;
 76:     while (nlist->next) {
 77:       nlist = nlist->next;
 78:     }
 79:     nlist->next = olist;
 80:   }
 81:   return(0);
 82: }

 86: /*
 87:     PetscOListDestroy - Destroy a list of objects

 89:     Input Parameter:
 90: .   fl   - pointer to list
 91: */
 92: PetscErrorCode  PetscOListDestroy(PetscOList fl)
 93: {
 94:   PetscOList     tmp;

 98:   while (fl) {
 99:     tmp   = fl->next;
100:     PetscObjectDereference(fl->obj);
101:     PetscFree(fl);
102:     fl    = tmp;
103:   }
104:   return(0);
105: }


110: /*
111:     PetscOListFind - givn a name, find the matching object

113:     Input Parameters:
114: +   fl   - pointer to list
115: -   name - name string

117:     Output Parameters:
118: .   ob - the PETSc object

120:     Notes:
121:     The name must have been registered with the PetscOListAdd() before calling this 
122:     routine.

124: .seealso: PetscOListReverseFind()

126: */
127: PetscErrorCode  PetscOListFind(PetscOList fl,const char name[],PetscObject *obj)
128: {
130:   PetscTruth     match;


134:   *obj = 0;
135:   while (fl) {
136:     PetscStrcmp(name,fl->name,&match);
137:     if (match) {
138:       *obj = fl->obj;
139:       break;
140:     }
141:     fl = fl->next;
142:   }
143:   return(0);
144: }

148: /*
149:     PetscOListReverseFind - given a object, find the matching name if it exists

151:     Input Parameters:
152: +   fl   - pointer to list
153: -   ob - the PETSc object

155:     Output Parameters:
156: .   name - name string

158:     Notes:
159:     The name must have been registered with the PetscOListAdd() before calling this 
160:     routine.

162: .seealso: PetscOListFind()

164: */
165: PetscErrorCode  PetscOListReverseFind(PetscOList fl,PetscObject obj,char **name)
166: {

169:   *name = 0;
170:   while (fl) {
171:     if (fl->obj == obj) {
172:       *name = fl->name;
173:       break;
174:     }
175:     fl = fl->next;
176:   }
177:   return(0);
178: }


183: /*
184:     PetscOListDuplicate - Creates a new list from a give object list.

186:     Input Parameters:
187: .   fl   - pointer to list

189:     Output Parameters:
190: .   nl - the new list (should point to 0 to start, otherwise appends)


193: */
194: PetscErrorCode  PetscOListDuplicate(PetscOList fl,PetscOList *nl)
195: {

199:   while (fl) {
200:     PetscOListAdd(nl,fl->name,fl->obj);
201:     fl = fl->next;
202:   }
203:   return(0);
204: }