changeset 0:b1f178ed41a3

initial add
author Me
date Sat, 22 May 2010 19:51:49 -0700
parents
children 2698781db812 8473c52c6f0e
files Vector.c Vector.h
diffstat 2 files changed, 96 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Vector.c	Sat May 22 19:51:49 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 +
    1.16 +
    1.17 +/*make a struct with the sizes and a pointer to the
    1.18 + * array, but hide a reverse pointer at the front of the array that
    1.19 + * points back to the vector struct -- that way, can pass around the
    1.20 + * array when doing work on elements, but when need to increase size,
    1.21 + * get pointer to vector struct and use that, which will change the
    1.22 + * ptr to the array in the vector struc, and return the new pointer..
    1.23 + * so from the point of changing size on, have the correct array ptr,
    1.24 + * and also all other places that initiate a sequence later will get
    1.25 + * the array ptr from the vector struct at the sequence start..
    1.26 + */
    1.27 +bool8
    1.28 +addToVect( Vector *vect, void *ptrToElem )
    1.29 + { int32 numPtrsInVect, sizeOfVect;
    1.30 +
    1.31 +/*
    1.32 +   numPtrsInVect = *(vect -1);  //num ptrs is "hidden" in front
    1.33 +   sizeOfVect    = *(vect -2);
    1.34 +*/
    1.35 +   numPtrsInVect = vect->numPtrsInArray;
    1.36 +   sizeOfVect    = vect->sizeOfArray;
    1.37 +
    1.38 +   if( numPtrsInVect >= sizeOfVect ) return FALSE;
    1.39 +
    1.40 +   vect->arrayOfPtrs[numPtrsInVect] = ptrToElem;
    1.41 +   vect->numPtrsInArray++;
    1.42 + }
    1.43 +
    1.44 +void **
    1.45 +increaseSizeOfVect( Vector *vect )
    1.46 + { int32 oldSizeOfArray, newSizeOfArray, i;
    1.47 +   void **newArray, **oldArray;
    1.48 + 
    1.49 +   oldSizeOfArray   = vect->sizeOfArray;
    1.50 +   newSizeOfArray   = oldSizeOfArray * 2;
    1.51 +   oldArray      = vect->arrayOfPtrs;
    1.52 +   newArray      = malloc( (newSizeOfArray + 1) * sizeof(void *) );
    1.53 +   *newArray     = vect;
    1.54 +   newArray++;
    1.55 +   for( i = 0; i < oldSizeOfArray; i++ )
    1.56 +    {
    1.57 +      newArray[i] = oldArray[i];
    1.58 +    }
    1.59 +   vect->arrayOfPtrs = newArray;
    1.60 +   vect->sizeOfArray = newSizeOfArray;
    1.61 +
    1.62 +   free( oldArray -1 );
    1.63 +   return newArray;
    1.64 + }
    1.65 +
    1.66 +/*
    1.67 +bool8
    1.68 +forAllInVectDo( ptrToFnTakesVectElemAsVoid* )
    1.69 + {
    1.70 +   return success; 
    1.71 + }
    1.72 +*/
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Vector.h	Sat May 22 19:51:49 2010 -0700
     2.3 @@ -0,0 +1,27 @@
     2.4 +/* 
     2.5 + * File:   Vector.h
     2.6 + * Author: Me
     2.7 + *
     2.8 + * Created on May 14, 2010, 3:08 PM
     2.9 + */
    2.10 +
    2.11 +#ifndef _VECTOR_H
    2.12 +#define	_VECTOR_H
    2.13 +
    2.14 +//Doing one special cheat -- hiding a back-ptr in front of array
    2.15 +typedef struct
    2.16 + {
    2.17 +   void  **arrayOfPtrs;
    2.18 +   int     numPtrsInArray;
    2.19 +   int     sizeOfArray;
    2.20 + }
    2.21 +Vector;
    2.22 +
    2.23 +Vector *createVect        ( int32 initialSizeOfArray );
    2.24 +Vector *increaseSizeOfVect( Vector *vect );
    2.25 +bool8   addToVect         ( void *ptrToAdd, Vector *vect );
    2.26 +bool8   removeLastInVect  ( Vector *vect );
    2.27 +
    2.28 +
    2.29 +#endif	/* _VECTOR_H */
    2.30 +