Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > DynArray
comparison DynArray.c @ 2:ad8621ee5986
Chgd name to DynArray
| author | Me |
|---|---|
| date | Thu, 02 Sep 2010 17:48:38 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:6bf50d5ee5bc |
|---|---|
| 1 /* | |
| 2 * Copyright 2010 OpenSourceCodeStewardshipFoundation | |
| 3 * | |
| 4 * Licensed under BSD | |
| 5 */ | |
| 6 | |
| 7 | |
| 8 | |
| 9 #include <stdio.h> | |
| 10 #include <malloc.h> | |
| 11 | |
| 12 #include "Vector.h" | |
| 13 | |
| 14 | |
| 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; | |
| 28 | |
| 29 /* | |
| 30 numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front | |
| 31 sizeOfVect = *(vect -2); | |
| 32 */ | |
| 33 numPtrsInVect = vect->numPtrsInArray; | |
| 34 sizeOfVect = vect->sizeOfArray; | |
| 35 | |
| 36 if( numPtrsInVect >= sizeOfVect ) return FALSE; | |
| 37 | |
| 38 vect->arrayOfPtrs[numPtrsInVect] = ptrToElem; | |
| 39 vect->numPtrsInArray++; | |
| 40 } | |
| 41 | |
| 42 void | |
| 43 increaseSizeOfVect( Vector *vect ) | |
| 44 { int32 oldSizeOfArray, newSizeOfArray, i; | |
| 45 void **newArray, **oldArray; | |
| 46 | |
| 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; | |
| 59 | |
| 60 free( oldArray -1 ); | |
| 61 } | |
| 62 | |
| 63 /* | |
| 64 bool8 | |
| 65 forAllInVectDo( ptrToFnTakesVectElemAsVoid* ) | |
| 66 { | |
| 67 return success; | |
| 68 } | |
| 69 */ |
