annotate Vector.c @ 0:b1f178ed41a3

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