Me@2: /* Me@2: * Copyright 2010 OpenSourceCodeStewardshipFoundation Me@2: * Me@2: * Licensed under BSD Me@2: */ Me@2: Me@2: Me@2: Me@2: #include Me@2: #include Me@2: Me@2: #include "Vector.h" Me@2: Me@2: Me@2: /*make a struct with the sizes and a pointer to the Me@2: * array, but hide a reverse pointer at the front of the array that Me@2: * points back to the vector struct -- that way, can pass around the Me@2: * array when doing work on elements, but when need to increase size, Me@2: * get pointer to vector struct and use that, which will change the Me@2: * ptr to the array in the vector struc, and return the new pointer.. Me@2: * so from the point of changing size on, have the correct array ptr, Me@2: * and also all other places that initiate a sequence later will get Me@2: * the array ptr from the vector struct at the sequence start.. Me@2: */ Me@2: bool8 Me@2: addToVect( void *ptrToElem, Vector *vect ) Me@2: { int32 numPtrsInVect, sizeOfVect; Me@2: Me@2: /* Me@2: numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front Me@2: sizeOfVect = *(vect -2); Me@2: */ Me@2: numPtrsInVect = vect->numPtrsInArray; Me@2: sizeOfVect = vect->sizeOfArray; Me@2: Me@2: if( numPtrsInVect >= sizeOfVect ) return FALSE; Me@2: Me@2: vect->arrayOfPtrs[numPtrsInVect] = ptrToElem; Me@2: vect->numPtrsInArray++; Me@2: } Me@2: Me@2: void Me@2: increaseSizeOfVect( Vector *vect ) Me@2: { int32 oldSizeOfArray, newSizeOfArray, i; Me@2: void **newArray, **oldArray; Me@2: Me@2: oldSizeOfArray = vect->sizeOfArray; Me@2: newSizeOfArray = oldSizeOfArray * 2; Me@2: oldArray = vect->arrayOfPtrs; Me@2: newArray = malloc( (newSizeOfArray + 1) * sizeof(void *) ); Me@2: *newArray = vect; Me@2: newArray++; Me@2: for( i = 0; i < oldSizeOfArray; i++ ) Me@2: { Me@2: newArray[i] = oldArray[i]; Me@2: } Me@2: vect->arrayOfPtrs = newArray; Me@2: vect->sizeOfArray = newSizeOfArray; Me@2: Me@2: free( oldArray -1 ); Me@2: } Me@2: Me@2: /* Me@2: bool8 Me@2: forAllInVectDo( ptrToFnTakesVectElemAsVoid* ) Me@2: { Me@2: return success; Me@2: } Me@2: */