diff DynArray.c @ 2:ad8621ee5986

Chgd name to DynArray
author Me
date Thu, 02 Sep 2010 17:48:38 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/DynArray.c	Thu Sep 02 17:48:38 2010 -0700
     1.3 @@ -0,0 +1,69 @@
     1.4 +/*
     1.5 + * Copyright 2010  OpenSourceCodeStewardshipFoundation
     1.6 + *
     1.7 + * Licensed under BSD
     1.8 + */
     1.9 +
    1.10 +
    1.11 +
    1.12 +#include <stdio.h>
    1.13 +#include <malloc.h>
    1.14 +
    1.15 +#include "Vector.h"
    1.16 +
    1.17 +
    1.18 +/*make a struct with the sizes and a pointer to the
    1.19 + * array, but hide a reverse pointer at the front of the array that
    1.20 + * points back to the vector struct -- that way, can pass around the
    1.21 + * array when doing work on elements, but when need to increase size,
    1.22 + * get pointer to vector struct and use that, which will change the
    1.23 + * ptr to the array in the vector struc, and return the new pointer..
    1.24 + * so from the point of changing size on, have the correct array ptr,
    1.25 + * and also all other places that initiate a sequence later will get
    1.26 + * the array ptr from the vector struct at the sequence start..
    1.27 + */
    1.28 +bool8
    1.29 +addToVect( void *ptrToElem, Vector *vect )
    1.30 + { int32 numPtrsInVect, sizeOfVect;
    1.31 +
    1.32 +/*
    1.33 +   numPtrsInVect = *(vect -1);  //num ptrs is "hidden" in front
    1.34 +   sizeOfVect    = *(vect -2);
    1.35 +*/
    1.36 +   numPtrsInVect = vect->numPtrsInArray;
    1.37 +   sizeOfVect    = vect->sizeOfArray;
    1.38 +
    1.39 +   if( numPtrsInVect >= sizeOfVect ) return FALSE;
    1.40 +
    1.41 +   vect->arrayOfPtrs[numPtrsInVect] = ptrToElem;
    1.42 +   vect->numPtrsInArray++;
    1.43 + }
    1.44 +
    1.45 +void
    1.46 +increaseSizeOfVect( Vector *vect )
    1.47 + { int32 oldSizeOfArray, newSizeOfArray, i;
    1.48 +   void **newArray, **oldArray;
    1.49 + 
    1.50 +   oldSizeOfArray   = vect->sizeOfArray;
    1.51 +   newSizeOfArray   = oldSizeOfArray * 2;
    1.52 +   oldArray      = vect->arrayOfPtrs;
    1.53 +   newArray      = malloc( (newSizeOfArray + 1) * sizeof(void *) );
    1.54 +   *newArray     = vect;
    1.55 +   newArray++;
    1.56 +   for( i = 0; i < oldSizeOfArray; i++ )
    1.57 +    {
    1.58 +      newArray[i] = oldArray[i];
    1.59 +    }
    1.60 +   vect->arrayOfPtrs = newArray;
    1.61 +   vect->sizeOfArray = newSizeOfArray;
    1.62 +
    1.63 +   free( oldArray -1 );
    1.64 + }
    1.65 +
    1.66 +/*
    1.67 +bool8
    1.68 +forAllInVectDo( ptrToFnTakesVectElemAsVoid* )
    1.69 + {
    1.70 +   return success; 
    1.71 + }
    1.72 +*/