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