# HG changeset patch # User Me # Date 1284318625 25200 # Node ID 8473c52c6f0e47c87b7eac0e3d1ec8174798cc77 # Parent b1f178ed41a3a0458bee5be1f7839bf1a244e64a Changed name from Vector to DynArray diff -r b1f178ed41a3 -r 8473c52c6f0e DynArray.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DynArray.c Sun Sep 12 12:10:25 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 b1f178ed41a3 -r 8473c52c6f0e DynArray.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DynArray.h Sun Sep 12 12:10:25 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 */ + diff -r b1f178ed41a3 -r 8473c52c6f0e Vector.c --- a/Vector.c Sat May 22 19:51:49 2010 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* - * 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 b1f178ed41a3 -r 8473c52c6f0e Vector.h --- a/Vector.h Sat May 22 19:51:49 2010 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -/* - * 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 */ -