changeset 3:8473c52c6f0e

Changed name from Vector to DynArray
author Me
date Sun, 12 Sep 2010 12:10:25 -0700
parents b1f178ed41a3
children f35e64d7a42b
files DynArray.c DynArray.h Vector.c Vector.h
diffstat 4 files changed, 96 insertions(+), 96 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/DynArray.c	Sun Sep 12 12:10:25 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/DynArray.h	Sun Sep 12 12:10:25 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 +
     3.1 --- a/Vector.c	Sat May 22 19:51:49 2010 -0700
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,69 +0,0 @@
     3.4 -/*
     3.5 - * Copyright 2010  OpenSourceCodeStewardshipFoundation
     3.6 - *
     3.7 - * Licensed under BSD
     3.8 - */
     3.9 -
    3.10 -
    3.11 -
    3.12 -#include <stdio.h>
    3.13 -#include <malloc.h>
    3.14 -
    3.15 -
    3.16 -
    3.17 -/*make a struct with the sizes and a pointer to the
    3.18 - * array, but hide a reverse pointer at the front of the array that
    3.19 - * points back to the vector struct -- that way, can pass around the
    3.20 - * array when doing work on elements, but when need to increase size,
    3.21 - * get pointer to vector struct and use that, which will change the
    3.22 - * ptr to the array in the vector struc, and return the new pointer..
    3.23 - * so from the point of changing size on, have the correct array ptr,
    3.24 - * and also all other places that initiate a sequence later will get
    3.25 - * the array ptr from the vector struct at the sequence start..
    3.26 - */
    3.27 -bool8
    3.28 -addToVect( Vector *vect, void *ptrToElem )
    3.29 - { int32 numPtrsInVect, sizeOfVect;
    3.30 -
    3.31 -/*
    3.32 -   numPtrsInVect = *(vect -1);  //num ptrs is "hidden" in front
    3.33 -   sizeOfVect    = *(vect -2);
    3.34 -*/
    3.35 -   numPtrsInVect = vect->numPtrsInArray;
    3.36 -   sizeOfVect    = vect->sizeOfArray;
    3.37 -
    3.38 -   if( numPtrsInVect >= sizeOfVect ) return FALSE;
    3.39 -
    3.40 -   vect->arrayOfPtrs[numPtrsInVect] = ptrToElem;
    3.41 -   vect->numPtrsInArray++;
    3.42 - }
    3.43 -
    3.44 -void **
    3.45 -increaseSizeOfVect( Vector *vect )
    3.46 - { int32 oldSizeOfArray, newSizeOfArray, i;
    3.47 -   void **newArray, **oldArray;
    3.48 - 
    3.49 -   oldSizeOfArray   = vect->sizeOfArray;
    3.50 -   newSizeOfArray   = oldSizeOfArray * 2;
    3.51 -   oldArray      = vect->arrayOfPtrs;
    3.52 -   newArray      = malloc( (newSizeOfArray + 1) * sizeof(void *) );
    3.53 -   *newArray     = vect;
    3.54 -   newArray++;
    3.55 -   for( i = 0; i < oldSizeOfArray; i++ )
    3.56 -    {
    3.57 -      newArray[i] = oldArray[i];
    3.58 -    }
    3.59 -   vect->arrayOfPtrs = newArray;
    3.60 -   vect->sizeOfArray = newSizeOfArray;
    3.61 -
    3.62 -   free( oldArray -1 );
    3.63 -   return newArray;
    3.64 - }
    3.65 -
    3.66 -/*
    3.67 -bool8
    3.68 -forAllInVectDo( ptrToFnTakesVectElemAsVoid* )
    3.69 - {
    3.70 -   return success; 
    3.71 - }
    3.72 -*/
     4.1 --- a/Vector.h	Sat May 22 19:51:49 2010 -0700
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,27 +0,0 @@
     4.4 -/* 
     4.5 - * File:   Vector.h
     4.6 - * Author: Me
     4.7 - *
     4.8 - * Created on May 14, 2010, 3:08 PM
     4.9 - */
    4.10 -
    4.11 -#ifndef _VECTOR_H
    4.12 -#define	_VECTOR_H
    4.13 -
    4.14 -//Doing one special cheat -- hiding a back-ptr in front of array
    4.15 -typedef struct
    4.16 - {
    4.17 -   void  **arrayOfPtrs;
    4.18 -   int     numPtrsInArray;
    4.19 -   int     sizeOfArray;
    4.20 - }
    4.21 -Vector;
    4.22 -
    4.23 -Vector *createVect        ( int32 initialSizeOfArray );
    4.24 -Vector *increaseSizeOfVect( Vector *vect );
    4.25 -bool8   addToVect         ( void *ptrToAdd, Vector *vect );
    4.26 -bool8   removeLastInVect  ( Vector *vect );
    4.27 -
    4.28 -
    4.29 -#endif	/* _VECTOR_H */
    4.30 -