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