Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > DynArray
view DynArray.c @ 2:ad8621ee5986
Chgd name to DynArray
| author | Me |
|---|---|
| date | Thu, 02 Sep 2010 17:48:38 -0700 |
| parents | |
| children |
line source
1 /*
2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <malloc.h>
12 #include "Vector.h"
15 /*make a struct with the sizes and a pointer to the
16 * array, but hide a reverse pointer at the front of the array that
17 * points back to the vector struct -- that way, can pass around the
18 * array when doing work on elements, but when need to increase size,
19 * get pointer to vector struct and use that, which will change the
20 * ptr to the array in the vector struc, and return the new pointer..
21 * so from the point of changing size on, have the correct array ptr,
22 * and also all other places that initiate a sequence later will get
23 * the array ptr from the vector struct at the sequence start..
24 */
25 bool8
26 addToVect( void *ptrToElem, Vector *vect )
27 { int32 numPtrsInVect, sizeOfVect;
29 /*
30 numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front
31 sizeOfVect = *(vect -2);
32 */
33 numPtrsInVect = vect->numPtrsInArray;
34 sizeOfVect = vect->sizeOfArray;
36 if( numPtrsInVect >= sizeOfVect ) return FALSE;
38 vect->arrayOfPtrs[numPtrsInVect] = ptrToElem;
39 vect->numPtrsInArray++;
40 }
42 void
43 increaseSizeOfVect( Vector *vect )
44 { int32 oldSizeOfArray, newSizeOfArray, i;
45 void **newArray, **oldArray;
47 oldSizeOfArray = vect->sizeOfArray;
48 newSizeOfArray = oldSizeOfArray * 2;
49 oldArray = vect->arrayOfPtrs;
50 newArray = malloc( (newSizeOfArray + 1) * sizeof(void *) );
51 *newArray = vect;
52 newArray++;
53 for( i = 0; i < oldSizeOfArray; i++ )
54 {
55 newArray[i] = oldArray[i];
56 }
57 vect->arrayOfPtrs = newArray;
58 vect->sizeOfArray = newSizeOfArray;
60 free( oldArray -1 );
61 }
63 /*
64 bool8
65 forAllInVectDo( ptrToFnTakesVectElemAsVoid* )
66 {
67 return success;
68 }
69 */
