SALSA Analysis Modules
anamodutils.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include "string.h"
00003 #include "anamod.h"
00004 
00005 #define NALLOC 70
00006 
00007 struct IntArray_ {
00008   char *name; int alloc,fill; PetscBool *has; int *data;
00009 };
00010 
00011 #undef __FUNCT__
00012 #define __FUNCT__ "CreateIntArray"
00013 PetscErrorCode CreateIntArray(const char *name,int size,IntArray *array)
00014 {
00015   IntArray inew; PetscErrorCode ierr;
00016   PetscFunctionBegin;
00017   ierr = PetscMalloc(sizeof(struct IntArray_),&inew); CHKERRQ(ierr);
00018   ierr = PetscMemzero(inew,sizeof(struct IntArray_)); CHKERRQ(ierr);
00019   if (name) inew->name = strdup(name);
00020   inew->fill = 0; inew->alloc = NALLOC;
00021   ierr = PetscMalloc(inew->alloc*sizeof(PetscBool),&(inew->has)); CHKERRQ(ierr);
00022   ierr = PetscMemzero(inew->has,inew->alloc*sizeof(PetscBool)); CHKERRQ(ierr);
00023   ierr = PetscMalloc(inew->alloc*sizeof(int),&(inew->data)); CHKERRQ(ierr);
00024   ierr = PetscMemzero(inew->data,inew->alloc*sizeof(int)); CHKERRQ(ierr);
00025   *array = inew;
00026   PetscFunctionReturn(0);
00027 }
00028 
00029 #undef __FUNCT__
00030 #define __FUNCT__ "DeleteIntArray"
00031 PetscErrorCode DeleteIntArray(IntArray array)
00032 {
00033   PetscErrorCode ierr;
00034   PetscFunctionBegin;
00035   ierr = PetscFree(array->data); CHKERRQ(ierr);
00036   ierr = PetscFree(array->has); CHKERRQ(ierr);
00037   if (array->name) free(array->name);
00038   ierr = PetscFree(array); CHKERRQ(ierr);
00039   PetscFunctionReturn(0);
00040 }
00041 
00042 #undef __FUNCT__
00043 #define __FUNCT__ "IntArrayAdd"
00044 /*! Add a new value and return the index. Right now we do not yet dynamically
00045   reallocate the array if there is no more space.
00046 */
00047 PetscErrorCode IntArrayAdd(IntArray array,int val,int *idx)
00048 {
00049   int ins;
00050   PetscFunctionBegin;
00051   if (array->fill>=array->alloc-1) SETERRQ(MPI_COMM_WORLD,1,"No more space");
00052   ins = array->fill++;
00053   array->data[ins] = val;
00054   array->has[ins] = PETSC_TRUE;
00055   PetscFunctionReturn(0);
00056 }
00057 
00058 #undef __FUNCT__
00059 #define __FUNCT__ "IntArraySetAt"
00060 /*! Set a value at a given index. Right now we do not yet dynamically
00061   reallocate the array if the index is out of bound.
00062 */
00063 PetscErrorCode IntArraySetAt(IntArray array,int idx,int val)
00064 {
00065   PetscFunctionBegin;
00066   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00067   array->data[idx] = val; array->has[idx] = PETSC_TRUE;
00068   if (idx>array->fill) array->fill = idx;
00069   PetscFunctionReturn(0);
00070 }
00071 
00072 #undef __FUNCT__
00073 #define __FUNCT__ "IntArrayTryGetAt"
00074 /*! Retrieve data from a given index. 
00075 
00076   Arguments:
00077   - \c array : the IntArray object
00078   - \c idx : the index; it is an error to ask out of bounds, but
00079   not to ask beyond the highest position filled; in that case failure will
00080   be reported
00081   - \c val (output) : the value retrieved. This argument is allowed to be null.
00082   - \c has (output) : true if a value was stored at this index.
00083   This argument is allowed to be null.
00084 */
00085 PetscErrorCode IntArrayTryGetAt(IntArray array,int idx,int *val,PetscBool *has)
00086 {
00087   PetscFunctionBegin;
00088   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00089   if (has) *has = array->has[idx];
00090   if (array->has[idx] && val) *val = array->data[idx];
00091   PetscFunctionReturn(0);
00092 }
00093 
00094 #undef __FUNCT__
00095 #define __FUNCT__ "IntArrayGetAt"
00096 /*! As IntArrayTryGetAt(), except that it is an error to ask
00097   for an index where no value has been set.
00098 */
00099 PetscErrorCode IntArrayGetAt(IntArray array,int idx,int *val)
00100 {
00101   PetscFunctionBegin;
00102   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00103   if (!array->has[idx]) SETERRQ1(MPI_COMM_WORLD,1,"Asking for non-set element: %d",idx);
00104   *val = array->data[idx];
00105   PetscFunctionReturn(0);
00106 }
00107 
00108 /*
00109  * String arrays
00110  */
00111 struct StringArray_ {
00112   char *name; int alloc,fill; PetscBool *has; char **data;
00113 };
00114 
00115 #undef __FUNCT__
00116 #define __FUNCT__ "CreateStringArray"
00117 PetscErrorCode CreateStringArray(const char *name,int size,StringArray *array)
00118 {
00119   StringArray snew; PetscErrorCode ierr;
00120   PetscFunctionBegin;
00121   ierr = PetscMalloc(sizeof(struct StringArray_),&snew); CHKERRQ(ierr);
00122   ierr = PetscMemzero(snew,sizeof(struct StringArray_)); CHKERRQ(ierr);
00123   if (name) snew->name = strdup(name);
00124   snew->fill = 0; snew->alloc = NALLOC;
00125   ierr = PetscMalloc(snew->alloc*sizeof(PetscBool),&(snew->has)); CHKERRQ(ierr);
00126   ierr = PetscMemzero(snew->has,snew->alloc*sizeof(PetscBool)); CHKERRQ(ierr);
00127   ierr = PetscMalloc(snew->alloc*sizeof(char*),&(snew->data)); CHKERRQ(ierr);
00128   ierr = PetscMemzero(snew->data,snew->alloc*sizeof(char*)); CHKERRQ(ierr);
00129   *array = snew;
00130   PetscFunctionReturn(0);
00131 }
00132 
00133 #undef __FUNCT__
00134 #define __FUNCT__ "DeleteStringArray"
00135 PetscErrorCode DeleteStringArray(StringArray array)
00136 {
00137   int i; PetscErrorCode ierr;
00138   PetscFunctionBegin;
00139   for (i=0; i<array->fill; i++)
00140     if (array->has[i]) free(array->data[i]);
00141   ierr = PetscFree(array->data); CHKERRQ(ierr);
00142   ierr = PetscFree(array->has); CHKERRQ(ierr);
00143   if (array->name) free(array->name);
00144   ierr = PetscFree(array); CHKERRQ(ierr);
00145   PetscFunctionReturn(0);
00146 }
00147 
00148 #undef __FUNCT__
00149 #define __FUNCT__ "StringArrayAdd"
00150 /*! Add a new value and return the index. Right now we do not yet dynamically
00151   reallocate the array if there is no more space.
00152 */
00153 PetscErrorCode StringArrayAdd(StringArray array,const char *val,int *idx)
00154 {
00155   int ins;
00156   PetscFunctionBegin;
00157   if (array->fill>=array->alloc-1) SETERRQ(MPI_COMM_WORLD,1,"No more space");
00158   ins = array->fill++;
00159   array->data[ins] = strdup(val);
00160   array->has[ins] = PETSC_TRUE;
00161   PetscFunctionReturn(0);
00162 }
00163 
00164 #undef __FUNCT__
00165 #define __FUNCT__ "StringArrayGetFill"
00166 PetscErrorCode StringArrayGetFill(StringArray array,int *idx)
00167 {
00168   PetscFunctionBegin;
00169   *idx = array->fill;
00170   PetscFunctionReturn(0);
00171 }
00172 
00173 #undef __FUNCT__
00174 #define __FUNCT__ "StringArraySetAt"
00175 /*! Set a value at a given index. Right now we do not yet dynamically
00176   reallocate the array if the index is out of bound.
00177 */
00178 PetscErrorCode StringArraySetAt(StringArray array,int idx,const char *val)
00179 {
00180   PetscFunctionBegin;
00181   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00182   array->data[idx] = strdup(val); array->has[idx] = PETSC_TRUE;
00183   if (idx>array->fill) array->fill = idx;
00184   PetscFunctionReturn(0);
00185 }
00186 
00187 #undef __FUNCT__
00188 #define __FUNCT__ "StringArrayTryGetAt"
00189 /*! Retrieve data from a given index. 
00190 
00191   Arguments:
00192   - \c array : the StringArray object
00193   - \c idx : the index; it is an error to ask out of bounds, but
00194   not to ask beyond the highest position filled; in that case failure will
00195   be reported
00196   - \c val (output) : the value retrieved. This argument is allowed to be null.
00197   - \c has (output) : true if a value was stored at this index.
00198   This argument is allowed to be null.
00199 */
00200 PetscErrorCode StringArrayTryGetAt(StringArray array,int idx,char **val,PetscBool *has)
00201 {
00202   PetscFunctionBegin;
00203   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00204   if (has) *has = array->has[idx];
00205   if (array->has[idx] && val) *val = array->data[idx];
00206   PetscFunctionReturn(0);
00207 }
00208 
00209 #undef __FUNCT__
00210 #define __FUNCT__ "StringArrayGetAt"
00211 /*! As StringArrayTryGetAt(), except that it is an error to ask
00212   for an index where no value has been set.
00213 */
00214 PetscErrorCode StringArrayGetAt(StringArray array,int idx,char **val)
00215 {
00216   PetscFunctionBegin;
00217   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00218   if (!array->has[idx]) SETERRQ1(MPI_COMM_WORLD,1,"Asking for non-set element: %d",idx);
00219   *val = array->data[idx];
00220   PetscFunctionReturn(0);
00221 }
00222 
00223 /*
00224  * AnalysisItem arrays
00225  */
00226 struct AnalysisItemArray_ {
00227   char *name; int alloc,fill,*has; AnalysisItem *data;
00228 };
00229 
00230 #undef __FUNCT__
00231 #define __FUNCT__ "CreateAnalysisItemArray"
00232 PetscErrorCode CreateAnalysisItemArray
00233 (const char *name,int size,AnalysisItemArray *array)
00234 {
00235   AnalysisItemArray anew; PetscErrorCode ierr;
00236   PetscFunctionBegin;
00237   ierr = PetscMalloc(sizeof(struct AnalysisItemArray_),&anew); CHKERRQ(ierr);
00238   ierr = PetscMemzero(anew,sizeof(struct AnalysisItemArray_)); CHKERRQ(ierr);
00239   if (name) anew->name = strdup(name);
00240   anew->fill = 0; anew->alloc = NALLOC;
00241   ierr = PetscMalloc(anew->alloc*sizeof(PetscBool),&(anew->has)); CHKERRQ(ierr);
00242   ierr = PetscMemzero(anew->has,anew->alloc*sizeof(PetscBool)); CHKERRQ(ierr);
00243   ierr = PetscMalloc(anew->alloc*sizeof(AnalysisItem),&(anew->data)); CHKERRQ(ierr);
00244   ierr = PetscMemzero(anew->data,anew->alloc*sizeof(AnalysisItem)); CHKERRQ(ierr);
00245   *array = anew;
00246   PetscFunctionReturn(0);
00247 }
00248 
00249 #undef __FUNCT__
00250 #define __FUNCT__ "DeleteAnalysisItemArray"
00251 PetscErrorCode DeleteAnalysisItemArray(AnalysisItemArray array)
00252 {
00253   PetscErrorCode ierr;
00254   PetscFunctionBegin;
00255   ierr = PetscFree(array->data); CHKERRQ(ierr);
00256   ierr = PetscFree(array->has); CHKERRQ(ierr);
00257   if (array->name) free(array->name);
00258   ierr = PetscFree(array); CHKERRQ(ierr);
00259   PetscFunctionReturn(0);
00260 }
00261 
00262 #undef __FUNCT__
00263 #define __FUNCT__ "AnalysisItemArrayAdd"
00264 /*! Add a new value and return the index. Right now we do not yet dynamically
00265   reallocate the array if there is no more space.
00266 */
00267 PetscErrorCode AnalysisItemArrayAdd(AnalysisItemArray array,AnalysisItem val,int *idx)
00268 {
00269   int ins;
00270   PetscFunctionBegin;
00271   if (array->fill>=array->alloc-1) SETERRQ(MPI_COMM_WORLD,1,"No more space");
00272   ins = array->fill++;
00273   array->data[ins] = val;
00274   array->has[ins] = PETSC_TRUE;
00275   PetscFunctionReturn(0);
00276 }
00277 
00278 #undef __FUNCT__
00279 #define __FUNCT__ "AnalysisItemArraySetAt"
00280 /*! Set a value at a given index. Right now we do not yet dynamically
00281   reallocate the array if the index is out of bound.
00282 */
00283 PetscErrorCode AnalysisItemArraySetAt(AnalysisItemArray array,int idx,AnalysisItem val)
00284 {
00285   PetscFunctionBegin;
00286   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00287   array->data[idx] = val; array->has[idx] = PETSC_TRUE;
00288   if (idx>array->fill) array->fill = idx;
00289   PetscFunctionReturn(0);
00290 }
00291 
00292 #undef __FUNCT__
00293 #define __FUNCT__ "AnalysisItemArrayTryGetAt"
00294 /*! Retrieve data from a given index. 
00295 
00296   Arguments:
00297   - \c array : the AnalysisItemArray object
00298   - \c idx : the index; it is an error to ask out of bounds, but
00299   not to ask beyond the highest position filled; in that case failure will
00300   be reported
00301   - \c val (output) : the value retrieved. This argument is allowed to be null.
00302   - \c has (output) : true if a value was stored at this index.
00303   This argument is allowed to be null.
00304 */
00305 PetscErrorCode AnalysisItemArrayTryGetAt(AnalysisItemArray array,int idx,AnalysisItem *val,PetscBool *has)
00306 {
00307   PetscFunctionBegin;
00308   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00309   if (has) *has = (PetscBool)array->has[idx];
00310   if (array->has[idx] && val) *val = array->data[idx];
00311   PetscFunctionReturn(0);
00312 }
00313 
00314 #undef __FUNCT__
00315 #define __FUNCT__ "AnalysisItemArrayGetAt"
00316 /*! As AnalysisItemArrayTryGetAt(), except that it is an error to ask
00317   for an index where no value has been set.
00318 */
00319 PetscErrorCode AnalysisItemArrayGetAt(AnalysisItemArray array,int idx,AnalysisItem *val)
00320 {
00321   PetscFunctionBegin;
00322   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00323   if (!array->has[idx]) SETERRQ1(MPI_COMM_WORLD,1,"Asking for non-set element: %d",idx);
00324   *val = array->data[idx];
00325   PetscFunctionReturn(0);
00326 }
00327 
00328 /*
00329  * AnalysisDataType arrays
00330  */
00331 struct AnalysisDataTypeArray_ {
00332   char *name; int alloc,fill,*has; AnalysisDataType *data;
00333 };
00334 
00335 #undef __FUNCT__
00336 #define __FUNCT__ "CreateAnalysisDataTypeArray"
00337 PetscErrorCode CreateAnalysisDataTypeArray
00338 (const char *name,int size,AnalysisDataTypeArray *array)
00339 {
00340   AnalysisDataTypeArray anew; PetscErrorCode ierr;
00341   PetscFunctionBegin;
00342   ierr = PetscMalloc(sizeof(struct AnalysisDataTypeArray_),&anew); CHKERRQ(ierr);
00343   ierr = PetscMemzero(anew,sizeof(struct AnalysisDataTypeArray_)); CHKERRQ(ierr);
00344   if (name) anew->name = strdup(name);
00345   anew->fill = 0; anew->alloc = NALLOC;
00346   ierr = PetscMalloc(anew->alloc*sizeof(PetscBool),&(anew->has)); CHKERRQ(ierr);
00347   ierr = PetscMemzero(anew->has,anew->alloc*sizeof(PetscBool)); CHKERRQ(ierr);
00348   ierr = PetscMalloc(anew->alloc*sizeof(AnalysisDataType),&(anew->data)); CHKERRQ(ierr);
00349   ierr = PetscMemzero(anew->data,anew->alloc*sizeof(AnalysisDataType)); CHKERRQ(ierr);
00350   *array = anew;
00351   PetscFunctionReturn(0);
00352 }
00353 
00354 #undef __FUNCT__
00355 #define __FUNCT__ "DeleteAnalysisDataTypeArray"
00356 PetscErrorCode DeleteAnalysisDataTypeArray(AnalysisDataTypeArray array)
00357 {
00358   PetscErrorCode ierr;
00359   PetscFunctionBegin;
00360   ierr = PetscFree(array->data); CHKERRQ(ierr);
00361   ierr = PetscFree(array->has); CHKERRQ(ierr);
00362   if (array->name) free(array->name);
00363   ierr = PetscFree(array); CHKERRQ(ierr);
00364   PetscFunctionReturn(0);
00365 }
00366 
00367 #undef __FUNCT__
00368 #define __FUNCT__ "AnalysisDataTypeArrayAdd"
00369 /*! Add a new value and return the index. Right now we do not yet dynamically
00370   reallocate the array if there is no more space.
00371 */
00372 PetscErrorCode AnalysisDataTypeArrayAdd(AnalysisDataTypeArray array,AnalysisDataType val,int *idx)
00373 {
00374   int ins;
00375   PetscFunctionBegin;
00376   if (array->fill>=array->alloc-1) SETERRQ(MPI_COMM_WORLD,1,"No more space");
00377   ins = array->fill++;
00378   array->data[ins] = val;
00379   array->has[ins] = PETSC_TRUE;
00380   PetscFunctionReturn(0);
00381 }
00382 
00383 #undef __FUNCT__
00384 #define __FUNCT__ "AnalysisDataTypeArraySetAt"
00385 /*! Set a value at a given index. Right now we do not yet dynamically
00386   reallocate the array if the index is out of bound.
00387 */
00388 PetscErrorCode AnalysisDataTypeArraySetAt(AnalysisDataTypeArray array,int idx,AnalysisDataType val)
00389 {
00390   PetscFunctionBegin;
00391   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00392   array->data[idx] = val; array->has[idx] = PETSC_TRUE;
00393   if (idx>array->fill) array->fill = idx;
00394   PetscFunctionReturn(0);
00395 }
00396 
00397 #undef __FUNCT__
00398 #define __FUNCT__ "AnalysisDataTypeArrayTryGetAt"
00399 /*! Retrieve data from a given index. 
00400 
00401   Arguments:
00402   - \c array : the AnalysisDataTypeArray object
00403   - \c idx : the index; it is an error to ask out of bounds, but
00404   not to ask beyond the highest position filled; in that case failure will
00405   be reported
00406   - \c val (output) : the value retrieved. This argument is allowed to be null.
00407   - \c has (output) : true if a value was stored at this index.
00408   This argument is allowed to be null.
00409 */
00410 PetscErrorCode AnalysisDataTypeArrayTryGetAt(AnalysisDataTypeArray array,int idx,AnalysisDataType *val,PetscBool *has)
00411 {
00412   PetscFunctionBegin;
00413   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00414   if (has) *has = (PetscBool)array->has[idx];
00415   if (array->has[idx] && val) *val = array->data[idx];
00416   PetscFunctionReturn(0);
00417 }
00418 
00419 #undef __FUNCT__
00420 #define __FUNCT__ "AnalysisDataTypeArrayGetAt"
00421 /*! As AnalysisDataTypeArrayTryGetAt(), except that it is an error to ask
00422   for an index where no value has been set.
00423 */
00424 PetscErrorCode AnalysisDataTypeArrayGetAt(AnalysisDataTypeArray array,int idx,AnalysisDataType *val)
00425 {
00426   PetscFunctionBegin;
00427   if (idx>=array->alloc) SETERRQ1(MPI_COMM_WORLD,1,"Index out of bounds: %d",idx);
00428   if (!array->has[idx]) SETERRQ1(MPI_COMM_WORLD,1,"Asking for non-set element: %d",idx);
00429   *val = array->data[idx];
00430   PetscFunctionReturn(0);
00431 }
00432