diff prdynarray.h @ 36:82b36ebdc648

new branch -- PR_univ_lib -- is a static library, copied to /usr/lib/PR__lib
author Sean Halle <seanhalle@yahoo.com>
date Sat, 27 Jul 2013 15:22:09 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prdynarray.h	Sat Jul 27 15:22:09 2013 -0700
     1.3 @@ -0,0 +1,91 @@
     1.4 +/* 
     1.5 + * File:   Vector.h
     1.6 + * Author: Me
     1.7 + *
     1.8 + * Created on May 14, 2010, 3:08 PM
     1.9 + */
    1.10 +
    1.11 +#ifndef _DYNARRAY_H
    1.12 +#define	_DYNARRAY_H
    1.13 +
    1.14 +#include <PR__include/PR__primitive_data_types.h>
    1.15 +
    1.16 +
    1.17 +
    1.18 +/*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray
    1.19 + * inside the function called!   After adding or other operation that might
    1.20 + * change the size, must re-read the addr of the chunk of memory that is the
    1.21 + * array, via the DynArrayInfo.
    1.22 + *Here's why: An array variable is a location, either on the stack
    1.23 + * or in a field of a struct, whose contents is an addr.  That addr is of the
    1.24 + * first location of a chunk of locations.  The DynArray works by changing
    1.25 + * the chunk of locations, then modifying the contents of the original 
    1.26 + * array variable.  It overwrites the addr of the old chunk of locations
    1.27 + * with the addr of the new chunk.
    1.28 + *But when the array variable is passed as a parameter, such as 
    1.29 + * in this: "foo( myDynArray )", then there are now two locations that hold
    1.30 + * the addr of the same chunk of locations.  So when a call is made that
    1.31 + * adds to the DynArray, and inside the DynArray expands, it only updates
    1.32 + * the original location with  the new addr.  Hence, the function will begin
    1.33 + * overwriting memory past the end of the old chunk, because it still has 
    1.34 + * the pointer to the old chunk of locations.
    1.35 + *
    1.36 + *A dynamic array is accessed same as any other array.  However, must use
    1.37 + * dyn array calls, defined in here, in order to add or increase the size.
    1.38 + * Must re-read the original array variable after any size-changing calls.
    1.39 + *To pass a DynArray as a parameter to a function, can only pass the 
    1.40 + * DynArrayInfo, then inside the function, to read the addr of the first 
    1.41 + * location in the chunk of locations that is the array, do this:
    1.42 + * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray).  After that, can 
    1.43 + * treat localArrayCopy as a normal array, as long as don't make any calls
    1.44 + * that add or otherwise could increase the size of the array.  If do make
    1.45 + * such a call, then re-copy the array via the above.  Can then use the
    1.46 + * copy up until another add to the array.
    1.47 + * 
    1.48 + */
    1.49 +typedef struct
    1.50 + {
    1.51 +   void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple *
    1.52 +   int32   numInArray;  //num entries added
    1.53 +   int32   sizeOfArray; //num elems alloc'd
    1.54 +   int32   sizeOfElem;  //num bytes in one elem of array -- used in 2nd version
    1.55 + }
    1.56 +PrivDynArrayInfo;
    1.57 +
    1.58 +PrivDynArrayInfo *
    1.59 +makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.60 +
    1.61 +PrivDynArrayInfo *
    1.62 +makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.63 +
    1.64 +PrivDynArrayInfo *
    1.65 +makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.66 +
    1.67 +int32
    1.68 +addToDynArray( void *value, PrivDynArrayInfo *info );
    1.69 +
    1.70 +void
    1.71 +makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
    1.72 +
    1.73 +void
    1.74 +makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
    1.75 +
    1.76 +void
    1.77 +increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
    1.78 +
    1.79 +typedef void  (*FreeFnPtr)  ( void * ); //fn has to cast void * to whatever
    1.80 +
    1.81 +void
    1.82 +freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
    1.83 +
    1.84 +void
    1.85 +freeDynArrayFlat( PrivDynArrayInfo *info );
    1.86 +
    1.87 +
    1.88 +typedef void  (*DynArrayFnPtr)  ( void * );  //fn has to cast void *
    1.89 +
    1.90 +void
    1.91 +forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
    1.92 +
    1.93 +#endif	/* _DYNARRAY_H */
    1.94 +