/*
 * Copyright 2010  OpenSourceCodeStewardshipFoundation
 *
 * Licensed under BSD
 */



#include <stdio.h>
#include <malloc.h>



/*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; 
 }
*/
