annotate DynArray.c @ 2:ad8621ee5986

Chgd name to DynArray
author Me
date Thu, 02 Sep 2010 17:48:38 -0700
parents
children
rev   line source
Me@2 1 /*
Me@2 2 * Copyright 2010 OpenSourceCodeStewardshipFoundation
Me@2 3 *
Me@2 4 * Licensed under BSD
Me@2 5 */
Me@2 6
Me@2 7
Me@2 8
Me@2 9 #include <stdio.h>
Me@2 10 #include <malloc.h>
Me@2 11
Me@2 12 #include "Vector.h"
Me@2 13
Me@2 14
Me@2 15 /*make a struct with the sizes and a pointer to the
Me@2 16 * array, but hide a reverse pointer at the front of the array that
Me@2 17 * points back to the vector struct -- that way, can pass around the
Me@2 18 * array when doing work on elements, but when need to increase size,
Me@2 19 * get pointer to vector struct and use that, which will change the
Me@2 20 * ptr to the array in the vector struc, and return the new pointer..
Me@2 21 * so from the point of changing size on, have the correct array ptr,
Me@2 22 * and also all other places that initiate a sequence later will get
Me@2 23 * the array ptr from the vector struct at the sequence start..
Me@2 24 */
Me@2 25 bool8
Me@2 26 addToVect( void *ptrToElem, Vector *vect )
Me@2 27 { int32 numPtrsInVect, sizeOfVect;
Me@2 28
Me@2 29 /*
Me@2 30 numPtrsInVect = *(vect -1); //num ptrs is "hidden" in front
Me@2 31 sizeOfVect = *(vect -2);
Me@2 32 */
Me@2 33 numPtrsInVect = vect->numPtrsInArray;
Me@2 34 sizeOfVect = vect->sizeOfArray;
Me@2 35
Me@2 36 if( numPtrsInVect >= sizeOfVect ) return FALSE;
Me@2 37
Me@2 38 vect->arrayOfPtrs[numPtrsInVect] = ptrToElem;
Me@2 39 vect->numPtrsInArray++;
Me@2 40 }
Me@2 41
Me@2 42 void
Me@2 43 increaseSizeOfVect( Vector *vect )
Me@2 44 { int32 oldSizeOfArray, newSizeOfArray, i;
Me@2 45 void **newArray, **oldArray;
Me@2 46
Me@2 47 oldSizeOfArray = vect->sizeOfArray;
Me@2 48 newSizeOfArray = oldSizeOfArray * 2;
Me@2 49 oldArray = vect->arrayOfPtrs;
Me@2 50 newArray = malloc( (newSizeOfArray + 1) * sizeof(void *) );
Me@2 51 *newArray = vect;
Me@2 52 newArray++;
Me@2 53 for( i = 0; i < oldSizeOfArray; i++ )
Me@2 54 {
Me@2 55 newArray[i] = oldArray[i];
Me@2 56 }
Me@2 57 vect->arrayOfPtrs = newArray;
Me@2 58 vect->sizeOfArray = newSizeOfArray;
Me@2 59
Me@2 60 free( oldArray -1 );
Me@2 61 }
Me@2 62
Me@2 63 /*
Me@2 64 bool8
Me@2 65 forAllInVectDo( ptrToFnTakesVectElemAsVoid* )
Me@2 66 {
Me@2 67 return success;
Me@2 68 }
Me@2 69 */