rendered paste body#include "vector.h"#include <stdio.h>#include <stddef.h>#include <assert.h>#include <stdlib.h>#include <string.h>#define DEFAULT_INITIAL_ALLOCATION 10void VectorNew(vector *v, int elemSize, VectorFreeFunction freefn, int initialAllocation) { assert(v!=NULL); assert(elemSize > 0); if(initialAllocation <= 0 ) initialAllocation = DEFAULT_INITIAL_ALLOCATION; v->buffer = malloc(elemSize * initialAllocation); if(v->buffer == NULL) { fprintf(stderr, "malloc failed for vector"); exit(EXIT_FAILURE); } v->capacity = initialAllocation; v->count = 0; v->freefn = freefn; v->elemSize = elemSize;}//TODO make this workvoid VectorDispose(vector *v) { /*begin Vector sanity check*/ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /*end vector sanity check */ if(v->freefn != NULL) { int i; for(i=0 ; i<v->count ; i++) { (*v->freefn)(VectorNth(v, i)); } } printf("Made it through the loop\n"); v->count = -1; v->elemSize = -1; v->capacity = -1; v->freefn = NULL; free(v->buffer);}int VectorLength(const vector *v) { /*begin Vector sanity check*/ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /*end vector sanity check */ return v->count;}void *VectorNth(const vector *v, int position) { /*begin vector sanity check*/ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /*end vector sanity check */ assert(position >= 0 && position < v->count); void *retval = (void *)(((unsigned char *)v->buffer) + position * v->elemSize); return retval;}void VectorInsert(vector *v, const void *elemAddr, int position){ /*begin vector sanity check*/ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /*end vector sanity check */ assert(position > 0 && position <= v->count); assert(elemAddr != NULL); if(position == v->count) { VectorAppend(v, elemAddr); return; } if(v->count == v->capacity) { v->capacity *= 2; v->buffer = realloc(v->buffer, v->capacity * v->elemSize); if(v->buffer == NULL) { fprintf(stderr, "realloc failed for vector\n"); exit(EXIT_FAILURE); } } void *insertPos = (void *)(((unsigned char *)v->buffer) + position * v->elemSize); memmove((void *)(((unsigned char *)insertPos) + v->elemSize), insertPos, v->elemSize * (v->count - position)); memcpy(insertPos, elemAddr, v->elemSize); v->count++; }void VectorAppend(vector *v, const void *elemAddr){ /*begin Vector sanity check*/ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /*end vector sanity check */ assert(elemAddr != NULL); if(v->count == v->capacity) { //gotta resize the vector first v->capacity*=2; printf("Preparing to realloc\n"); v->buffer = realloc(v->buffer, v->elemSize * v->capacity ); if(v->buffer == NULL) { fprintf(stderr, "malloc failed for vector"); exit(EXIT_FAILURE); } } void *insertPos = (void *)(((unsigned char *)v->buffer) + v->count * v->elemSize); memcpy(insertPos, elemAddr, v->elemSize); v->count++;}void VectorReplace(vector *v, const void *elemAddr, int position) { /* begin vector sanity check */ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /* end vector sanity check */ assert(position >=0); assert(position <= v->count); assert(elemAddr != NULL); void *insertPos = (void *)(((unsigned char *)v->buffer) + position * v->elemSize); memcpy(insertPos, elemAddr, v->elemSize);}void VectorDelete(vector *v, int position) { /* begin vector sanity check */ assert(v != NULL); assert(v->count >= 0); assert(v->elemSize > 0); assert(v->buffer != NULL); assert(v->capacity > 0); assert(v->count <= v->capacity); /* end vector sanity check */ assert(position > 0 && position <= v->count); void *removalPos = VectorNth(v, position); if(v->freefn != NULL) { (*v->freefn)(removalPos); } memmove(removalPos, (void *)(((unsigned char *)removalPos)+v->elemSize), (--v->count - position) * v->elemSize );}int main(int argc, char **argv) { vector v; VectorNew(&v, sizeof(int), NULL, 0); int nums[50], i = 0; for(;i<50;i++) { nums[i] = i * 4; VectorAppend(&v, &nums[i]); } int replacement = 99; int rep2 = 456; VectorReplace(&v, &replacement, 6); for(i=0 ; i<VectorLength(&v); i++) { printf("%d:%d, ", i, *((int *)VectorNth(&v, i)) ); } printf("\n"); VectorInsert(&v, &rep2, 2); for(i=0 ; i<VectorLength(&v); i++) { printf("%d:%d, ", i, *((int *)VectorNth(&v, i)) ); } printf("\n"); VectorDelete(&v, 10); for(i=0 ; i<VectorLength(&v); i++) { printf("%d:%d, ", i, *((int *)VectorNth(&v, i)) ); } VectorDispose(&v); return EXIT_SUCCESS;}