From aeee67bdda0e0f8834cbd3f65da99e3c79f69f65 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:28:23 -0400 Subject: Fix organize function order in vector docs and src Functions now ordered roughly to what they do/deal with. --- collections/vector/vector.c | 117 ++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 59 deletions(-) (limited to 'collections/vector/vector.c') diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 8aaffb6..23e5c47 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -1,11 +1,10 @@ #include "vector.h" +#include #include #include #include -#include - #define START_SIZE 64; struct vector { @@ -13,6 +12,19 @@ struct vector { int end, limit; }; +void vec_debug_print(root) +vec *root; +{ + int i; + + fprintf(stderr, "VEC[base: %p, end: %p, limit:%p]:\n\t ", + root->base, root->end, root->limit); + for (i=0; i < root->end; i++){ + fprintf(stderr, "[%p]", vec_index(root,i)); + } + fprintf(stderr, "\n"); +} + vec* vec_new() { vec *root; @@ -61,6 +73,40 @@ vec *root; assert(root->base); } +vec* vec_cp(root) +vec *root; +{ + vec *copy; + + if (!root) + return NULL; + + copy = vec_with_capacity(root->limit); + + copy->base = memcpy(copy->base, root->base, + vec_size(root) * sizeof(void*)); + assert(copy->base); + + copy->end = root->end; + copy->limit = root->limit; + + return copy; +} + +void vec_print(root, to_string) +vec *root; +char* to_string(void*); +{ + int i; + + printf("["); + for(i = 0; i < root->end; i++) { + printf("%s", to_string(vec_index(root, i))); + } + printf("\b]\n"); + +} + void vec_push(root, item) vec *root; void *item; @@ -76,17 +122,6 @@ void *item; root->base[root->end++] = item; } -void* vec_index(root, index) -vec *root; -int index; -{ - if (!root || index >= root->end || index < 0) { - return NULL; - } - - return root->base[index]; -} - void* vec_pop(root) vec *root; { @@ -97,13 +132,15 @@ vec *root; return root->base[--root->end]; } -void vec_free(root) +void* vec_index(root, index) vec *root; +int index; { - free(root->base); - root->base = NULL; + if (!root || index >= root->end || index < 0) { + return NULL; + } - free(root); + return root->base[index]; } void vec_clear(root) @@ -118,49 +155,11 @@ vec *root; root->end = 0; } -void vec_print(root, to_string) -vec *root; -char* to_string(void*); -{ - int i; - - printf("["); - for(i = 0; i < root->end; i++) { - printf("%s", to_string(vec_index(root, i))); - } - printf("\b]\n"); - -} - -void vec_debug_print(root) -vec *root; -{ - int i; - - fprintf(stderr, "VEC[base: %p, end: %p, limit:%p]:\n\t ", - root->base, root->end, root->limit); - for (i=0; i < root->end; i++){ - fprintf(stderr, "[%p]", vec_index(root,i)); - } - fprintf(stderr, "\n"); -} - -vec* vec_cp(root) +void vec_free(root) vec *root; { - vec *copy; - - if (!root) - return NULL; - - copy = vec_with_capacity(root->limit); - - copy->base = memcpy(copy->base, root->base, - vec_size(root) * sizeof(void*)); - assert(copy->base); - - copy->end = root->end; - copy->limit = root->limit; + free(root->base); + root->base = NULL; - return copy; + free(root); } -- cgit v1.1