# HG changeset patch # User Me # Date 1274583109 25200 # Node ID b1f178ed41a3a0458bee5be1f7839bf1a244e64a initial add diff -r 000000000000 -r b1f178ed41a3 Vector.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Vector.c Sat May 22 19:51:49 2010 -0700 @@ -0,0 +1,69 @@ +/* + * Copyright 2010 OpenSourceCodeStewardshipFoundation + * + * Licensed under BSD + */ + + + +#include +#include + + + +/*make a struct with the sizes and a pointer to the + * array, but hide a reverse pointer at the front of the array that + * points back to the vector struct -- that way, can pass around the + * array when doing work on elements, but when need to increase size, + * get pointer to vector struct and use that, which will change the + * ptr to the array in the vector struc, and return the new pointer.. + * so from the point of changing size on, have the correct array ptr, + * and also all other places that initiate a sequence later will get + * the array ptr from the vector struct at the sequence start.. + */ +bool8 +addToVect( Vector *vect, void *ptrToElem ) + { int32 numPtrsInVect, sizeOfVect; + +/* + numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front + sizeOfVect = *(vect -2); +*/ + numPtrsInVect = vect->numPtrsInArray; + sizeOfVect = vect->sizeOfArray; + + if( numPtrsInVect >= sizeOfVect ) return FALSE; + + vect->arrayOfPtrs[numPtrsInVect] = ptrToElem; + vect->numPtrsInArray++; + } + +void ** +increaseSizeOfVect( Vector *vect ) + { int32 oldSizeOfArray, newSizeOfArray, i; + void **newArray, **oldArray; + + oldSizeOfArray = vect->sizeOfArray; + newSizeOfArray = oldSizeOfArray * 2; + oldArray = vect->arrayOfPtrs; + newArray = malloc( (newSizeOfArray + 1) * sizeof(void *) ); + *newArray = vect; + newArray++; + for( i = 0; i < oldSizeOfArray; i++ ) + { + newArray[i] = oldArray[i]; + } + vect->arrayOfPtrs = newArray; + vect->sizeOfArray = newSizeOfArray; + + free( oldArray -1 ); + return newArray; + } + +/* +bool8 +forAllInVectDo( ptrToFnTakesVectElemAsVoid* ) + { + return success; + } +*/ diff -r 000000000000 -r b1f178ed41a3 Vector.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Vector.h Sat May 22 19:51:49 2010 -0700 @@ -0,0 +1,27 @@ +/* + * File: Vector.h + * Author: Me + * + * Created on May 14, 2010, 3:08 PM + */ + +#ifndef _VECTOR_H +#define _VECTOR_H + +//Doing one special cheat -- hiding a back-ptr in front of array +typedef struct + { + void **arrayOfPtrs; + int numPtrsInArray; + int sizeOfArray; + } +Vector; + +Vector *createVect ( int32 initialSizeOfArray ); +Vector *increaseSizeOfVect( Vector *vect ); +bool8 addToVect ( void *ptrToAdd, Vector *vect ); +bool8 removeLastInVect ( Vector *vect ); + + +#endif /* _VECTOR_H */ +