Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > DynArray
diff DynArray.c @ 3:8473c52c6f0e
Changed name from Vector to DynArray
| author | Me |
|---|---|
| date | Sun, 12 Sep 2010 12:10:25 -0700 |
| parents | |
| children | f35e64d7a42b |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/DynArray.c Sun Sep 12 12:10:25 2010 -0700 1.3 @@ -0,0 +1,69 @@ 1.4 +/* 1.5 + * Copyright 2010 OpenSourceCodeStewardshipFoundation 1.6 + * 1.7 + * Licensed under BSD 1.8 + */ 1.9 + 1.10 + 1.11 + 1.12 +#include <stdio.h> 1.13 +#include <malloc.h> 1.14 + 1.15 + 1.16 + 1.17 +/*make a struct with the sizes and a pointer to the 1.18 + * array, but hide a reverse pointer at the front of the array that 1.19 + * points back to the vector struct -- that way, can pass around the 1.20 + * array when doing work on elements, but when need to increase size, 1.21 + * get pointer to vector struct and use that, which will change the 1.22 + * ptr to the array in the vector struc, and return the new pointer.. 1.23 + * so from the point of changing size on, have the correct array ptr, 1.24 + * and also all other places that initiate a sequence later will get 1.25 + * the array ptr from the vector struct at the sequence start.. 1.26 + */ 1.27 +bool8 1.28 +addToVect( Vector *vect, void *ptrToElem ) 1.29 + { int32 numPtrsInVect, sizeOfVect; 1.30 + 1.31 +/* 1.32 + numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front 1.33 + sizeOfVect = *(vect -2); 1.34 +*/ 1.35 + numPtrsInVect = vect->numPtrsInArray; 1.36 + sizeOfVect = vect->sizeOfArray; 1.37 + 1.38 + if( numPtrsInVect >= sizeOfVect ) return FALSE; 1.39 + 1.40 + vect->arrayOfPtrs[numPtrsInVect] = ptrToElem; 1.41 + vect->numPtrsInArray++; 1.42 + } 1.43 + 1.44 +void ** 1.45 +increaseSizeOfVect( Vector *vect ) 1.46 + { int32 oldSizeOfArray, newSizeOfArray, i; 1.47 + void **newArray, **oldArray; 1.48 + 1.49 + oldSizeOfArray = vect->sizeOfArray; 1.50 + newSizeOfArray = oldSizeOfArray * 2; 1.51 + oldArray = vect->arrayOfPtrs; 1.52 + newArray = malloc( (newSizeOfArray + 1) * sizeof(void *) ); 1.53 + *newArray = vect; 1.54 + newArray++; 1.55 + for( i = 0; i < oldSizeOfArray; i++ ) 1.56 + { 1.57 + newArray[i] = oldArray[i]; 1.58 + } 1.59 + vect->arrayOfPtrs = newArray; 1.60 + vect->sizeOfArray = newSizeOfArray; 1.61 + 1.62 + free( oldArray -1 ); 1.63 + return newArray; 1.64 + } 1.65 + 1.66 +/* 1.67 +bool8 1.68 +forAllInVectDo( ptrToFnTakesVectElemAsVoid* ) 1.69 + { 1.70 + return success; 1.71 + } 1.72 +*/
