Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

dyn_array.h

Go to the documentation of this file.
00001 #ifndef _RWC_DYN_ARRAY_HEADER_
00002 #define _RWC_DYN_ARRAY_HEADER_
00003 
00004 /*--------------------------------------------------------------------------*/
00005 /*! Set of macros to define and use dynamic arrays.
00006      * If the array element type is not defined by one symbol,
00007         then you must typedef it, as in "typedef char * str".
00008      * DECLARE_ARRAY_TYPE(str) then typedefs the new
00009         type "str_array".
00010      * To declare a variable of type "str_array *", you can
00011         either do "str_array *sar", or "DECLARE_ARRAY(sar,str)".
00012      * To initialize this variable to point to the empty array,
00013         you do "INIT_ARRAY(sar,str)".
00014      * To add an element to the array, do "ADDTO_ARRAY(sar,str,val)"
00015         where "val" is assigned to the newly allocated last
00016         location in the array.  Since this is an assignment, if
00017         "val" is a pointer, it should be to malloc()-ed space,
00018         not to a local variable.
00019      * To free an array that is composed of un-malloc()-ed elements,
00020         use "DELETE_ARRAY(sar)".  To free an array that is composed
00021         of malloc()-ed element, use "FREE_ARRAY(sar)", which will
00022         call free() on each element of the array before deleting
00023         the array struct itself.  To free an array that requires
00024         a more elaborate destructor for each element, use
00025         "KILL_ARRAY(sar,kfunc)", where kfunc() will be called
00026         on each element of the array before the array struct
00027         is deleted.
00028      * The number of elements in an array is "sar->num".
00029      * The i-th element in an array is "sar->ar[i]",
00030         for i=0 .. sar->num-1.
00031 ----------------------------------------------------------------------------*/
00032 
00033 #include <stdlib.h>  /* for malloc */
00034 
00035 /*! Declare an array type of "typ_array". */
00036 
00037 #define DECLARE_ARRAY_TYPE(typ)                                 \
00038   typedef struct { int num ; typ * ar ; } typ ## _array
00039 
00040 /*! Declare a pointer of type "typ_array *". */
00041 
00042 #define DECLARE_ARRAY(anam,typ) typ ## _array * anam
00043 
00044 /*! Initialize an array pointer of type "typ_array *". */
00045 
00046 #define INIT_ARRAY(anam,typ)                                    \
00047  do{ anam = malloc(sizeof(typ ## _array *)) ;                   \
00048      anam->num = 0 ; anam->ar = NULL ;       } while(0)
00049 
00050 /*! Append element "val" to an array pointer "anam" of type "typ_array *". */
00051 
00052 #define ADDTO_ARRAY(anam,typ,val)                               \
00053  do{ int n=anam->num ;                                          \
00054      anam->ar = realloc(anam->ar,sizeof(typ)*(n+1)) ;           \
00055      anam->ar[n] = (val) ; anam->num++ ;             } while(0)
00056 
00057 /*! Delete the array pointed to by "anam". */
00058 
00059 #define DELETE_ARRAY(anam)                                      \
00060  do{ free(anam->ar) ; free(anam) ; anam = NULL ; } while(0)
00061 
00062 /*! Apply "killer()" to each element of the array
00063     pointed to by "anam", then delete anam itself. */
00064 
00065 #define KILL_ARRAY(anam,killer)                                 \
00066  do{ int i ;                                                    \
00067      for( i=0 ; i < anam->num ; i++ ) killer(anam->ar[i]) ;     \
00068      free(anam->ar) ; free(anam) ; anam = NULL ;                \
00069  } while(0)
00070 
00071 /*! Apply free() to each element of the array
00072     pointed to by "anam", then delete anam itself. */
00073 
00074 #define FREE_ARRAY(anam) KILL_ARRAY(anam,free)
00075 
00076 /*============================================*/
00077 /* Sample program that uses the macros above. */
00078 /*============================================*/
00079 
00080 #if 0
00081 typedef char * str ;      /* declare this type */
00082 
00083 DECLARE_ARRAY_TYPE(int) ; /* declare type of int_array */
00084 DECLARE_ARRAY_TYPE(str) ; /* declare type of str_array */
00085 
00086 int main( int argc , char *argv[] )
00087 {
00088    DECLARE_ARRAY(iar,int) ; /* declare iar to be of type int_array * */
00089    DECLARE_ARRAY(sar,str) ; /* declare sar to be of type str_array * */
00090    int ii ;
00091    char buf[32] , *tb ;
00092 
00093    INIT_ARRAY(iar,int) ;    /* initialize the two arrays */
00094    INIT_ARRAY(sar,str) ;
00095 
00096    /* fill the two arrays with some stuff */
00097 
00098    for( ii=0 ; ii < 9 ; ii++ ){
00099       ADDTO_ARRAY(iar,int,2*ii+7) ;  /* just assign a value to int_array */
00100 
00101       /* for str_array, we are assigning pointers,
00102          so each one must be unique; we do this by using strdup() */
00103 
00104       sprintf(buf,"%05d",2*ii+7) ; tb = strdup(buf) ;
00105       ADDTO_ARRAY(sar,str,tb) ;
00106    }
00107 
00108    /* print out info from each array */
00109 
00110    printf("iar->num=%d  sar->num=%d\n",iar->num,sar->num) ;
00111 
00112    for( ii=0 ; ii < 9 ; ii++ )
00113       printf(" iar[%d]=%d   sar[%d]=%s\n",
00114              ii,iar->ar[ii] , ii,sar->ar[ii] ) ;
00115 
00116    /* erase the two arrays */
00117 
00118    DELETE_ARRAY(iar) ;   /* can just delete the array struct itself */
00119    FREE_ARRAY(sar) ;     /* must use free() on each element first */
00120 
00121    exit(0) ;
00122 }
00123 #endif
00124 
00125 #endif /* _RWC_DYN_ARRAY_HEADER_ */
 

Powered by Plone

This site conforms to the following standards: