Mercurial > cgi-bin > hgwebdir.cgi > PR > PR_Implementations > PR__Univ > PR__includes > PR__include
comparison prdynarray.h @ 4:10986666560d
Changes on copy on D:
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Sun, 15 Sep 2013 07:18:46 -0700 |
| parents | |
| children | 14241f07f742 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1eb19d5c5c0d |
|---|---|
| 1 /* | |
| 2 * File: Vector.h | |
| 3 * Author: Me | |
| 4 * | |
| 5 * Created on May 14, 2010, 3:08 PM | |
| 6 */ | |
| 7 | |
| 8 #ifndef _DYNARRAY_H | |
| 9 #define _DYNARRAY_H | |
| 10 | |
| 11 #include <PR__include/PR__primitive_data_types.h> | |
| 12 | |
| 13 | |
| 14 | |
| 15 /*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray | |
| 16 * inside the function called! After adding or other operation that might | |
| 17 * change the size, must re-read the addr of the chunk of memory that is the | |
| 18 * array, via the DynArrayInfo. | |
| 19 *Here's why: An array variable is a location, either on the stack | |
| 20 * or in a field of a struct, whose contents is an addr. That addr is of the | |
| 21 * first location of a chunk of locations. The DynArray works by changing | |
| 22 * the chunk of locations, then modifying the contents of the original | |
| 23 * array variable. It overwrites the addr of the old chunk of locations | |
| 24 * with the addr of the new chunk. | |
| 25 *But when the array variable is passed as a parameter, such as | |
| 26 * in this: "foo( myDynArray )", then there are now two locations that hold | |
| 27 * the addr of the same chunk of locations. So when a call is made that | |
| 28 * adds to the DynArray, and inside the DynArray expands, it only updates | |
| 29 * the original location with the new addr. Hence, the function will begin | |
| 30 * overwriting memory past the end of the old chunk, because it still has | |
| 31 * the pointer to the old chunk of locations. | |
| 32 * | |
| 33 *A dynamic array is accessed same as any other array. However, must use | |
| 34 * dyn array calls, defined in here, in order to add or increase the size. | |
| 35 * Must re-read the original array variable after any size-changing calls. | |
| 36 *To pass a DynArray as a parameter to a function, can only pass the | |
| 37 * DynArrayInfo, then inside the function, to read the addr of the first | |
| 38 * location in the chunk of locations that is the array, do this: | |
| 39 * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can | |
| 40 * treat localArrayCopy as a normal array, as long as don't make any calls | |
| 41 * that add or otherwise could increase the size of the array. If do make | |
| 42 * such a call, then re-copy the array via the above. Can then use the | |
| 43 * copy up until another add to the array. | |
| 44 * | |
| 45 */ | |
| 46 typedef struct | |
| 47 { | |
| 48 void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple * | |
| 49 int32 numInArray; //num entries added | |
| 50 int32 sizeOfArray; //num elems alloc'd | |
| 51 int32 sizeOfElem; //num bytes in one elem of array -- used in 2nd version | |
| 52 } | |
| 53 PrivDynArrayInfo; | |
| 54 | |
| 55 PrivDynArrayInfo * | |
| 56 makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray ); | |
| 57 | |
| 58 PrivDynArrayInfo * | |
| 59 makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray ); | |
| 60 | |
| 61 PrivDynArrayInfo * | |
| 62 makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray ); | |
| 63 | |
| 64 int32 | |
| 65 addToDynArray( void *value, PrivDynArrayInfo *info ); | |
| 66 | |
| 67 void | |
| 68 makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex ); | |
| 69 | |
| 70 void | |
| 71 makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex); | |
| 72 | |
| 73 void | |
| 74 increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize ); | |
| 75 | |
| 76 typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever | |
| 77 | |
| 78 void | |
| 79 freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr ); | |
| 80 | |
| 81 void | |
| 82 freeDynArrayFlat( PrivDynArrayInfo *info ); | |
| 83 | |
| 84 | |
| 85 typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void * | |
| 86 | |
| 87 void | |
| 88 forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr ); | |
| 89 | |
| 90 #endif /* _DYNARRAY_H */ | |
| 91 |
