annotate DynArray.c @ 3:8473c52c6f0e

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