diff options
Diffstat (limited to 'collections')
-rw-r--r-- | collections/vector/vector.adoc | 158 | ||||
-rw-r--r-- | collections/vector/vector.c | 117 | ||||
-rw-r--r-- | collections/vector/vector.h | 16 |
3 files changed, 149 insertions, 142 deletions
diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 63cdd78..76c0f7a 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.7.1, 2020-07-03 +v0.7.2, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -65,26 +65,37 @@ vec_push_back(vector, NULL); assert(vec_size(vector) == 1); ---- -[[vec_push]] -+void vec_push(vec *self, void *item)+ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Pushes +item+ into back of +self+. This may cause a resize of the internal buffer. +[[vec_cp]] ++vec* vec_cp(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~ +Returns a copy of the vector +self+. All elements are kept in the same order. Examples ^^^^^^^^ [source,c] ---- #include "vector.h" +#include <string.h> -vec *vector = vec_new(); -vec_push(vector, NULL); -assert(vec_size(vector) == 1); +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_with_capacity(16); +vec_push_back(vector, str_dup(str1)); +vec_push_back(vector, str_dup(str2)); + +vec *new = vec_cp(vector); +assert(strcmp(vec_pop_back, str2) == 0); +assert(strcmp(vec_pop_back, str1) == 0); ---- -[[vec_index]] -+void* vec_index(vec *self, int index)+ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Returns the element at position +index+ of +self+. +[[vec_print]] ++void vec_print(vec *self, (char* to_string(void*)))+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Prints out the contents of the vector +self+ to +stdout+ (surounded by square +brackets and separated by commas ','). +to_string+ is a function that takes a +pointer to the type of elements stored in +self+ and returns a string +representation. Examples ^^^^^^^^ @@ -93,6 +104,14 @@ Examples #include "vector.h" #include <string.h> +char* to_string(str) +void *str; +{ + return str; +} + +int main() +{ char *str1 = "ONE"; char *str2 = "TWO"; char *str3 = "THREE"; @@ -102,7 +121,31 @@ vec_push(vector, str_dup(str1)); vec_push(vector, str_dup(str2)); vec_push(vector, str_dup(str3)); -assert(str_cmp(vec_index(vector, 1), str2) == 0); +printf("VEC CONTENTS:\n\t") +vec_print(vector, to_string) +} +---- + +Output: +---- +VEC_CONTENTS: + [ONE,TWO,THREE] +---- + +[[vec_push]] ++void vec_push(vec *self, void *item)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Pushes +item+ into back of +self+. This may cause a resize of the internal buffer. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_new(); +vec_push(vector, NULL); +assert(vec_size(vector) == 1); ---- [[vec_pop]] @@ -128,23 +171,28 @@ assert(str_cmp(vec_pop(vector), str2) == 0); assert(str_cmp(vec_pop(vector), str1) == 0); ---- -[[vec_free]] -+void vec_free(vec *self)+ -~~~~~~~~~~~~~~~~~~~~~~~~~~ -Frees all internal memory and +self+. - -NOTE: All item pointers are still valid after a call to -<<vec_free,+vec_free()+>>, <<vec_clear,+vec_clear()+>> should be called before -if they are no longer needed to avoid memory leaks. +[[vec_index]] ++void* vec_index(vec *self, int index)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Returns the element at position +index+ of +self+. Examples ^^^^^^^^ [source,c] ---- #include "vector.h" +#include <string.h> + +char *str1 = "ONE"; +char *str2 = "TWO"; +char *str3 = "THREE"; vec *vector = vec_new(); -vec_free(vector); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); +vec_push(vector, str_dup(str3)); + +assert(str_cmp(vec_index(vector, 1), str2) == 0); ---- [[vec_clear]] @@ -174,69 +222,21 @@ assert(vec_size(vector) == 0); vec_free(vector); ---- -[[vec_print]] -+void vec_print(vec *self, (char* to_string(void*)))+ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Prints out the contents of the vector +self+ to +stdout+ (surounded by square -brackets and separated by commas ','). +to_string+ is a function that takes a -pointer to the type of elements stored in +self+ and returns a string -representation. - -Examples -^^^^^^^^ -[source,c] ----- -#include "vector.h" -#include <string.h> - -char* to_string(str) -void *str; -{ - return str; -} - -int main() -{ -char *str1 = "ONE"; -char *str2 = "TWO"; -char *str3 = "THREE"; - -vec *vector = vec_new(); -vec_push(vector, str_dup(str1)); -vec_push(vector, str_dup(str2)); -vec_push(vector, str_dup(str3)); - -printf("VEC CONTENTS:\n\t") -vec_print(vector, to_string) -} ----- - -Output: ----- -VEC_CONTENTS: - [ONE,TWO,THREE] ----- +[[vec_free]] ++void vec_free(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~ +Frees all internal memory and +self+. -[[vec_cp]] -+vec* vec_cp(vec *self)+ -~~~~~~~~~~~~~~~~~~~~~~~~ -Returns a copy of the vector +self+. All elements are kept in the same order. +NOTE: All item pointers are still valid after a call to +<<vec_free,+vec_free()+>>, <<vec_clear,+vec_clear()+>> should be called before +if they are no longer needed to avoid memory leaks. Examples ^^^^^^^^ [source,c] ---- #include "vector.h" -#include <string.h> - -char *str1 = "ONE"; -char *str2 = "TWO"; -vec *vector = vec_with_capacity(16); -vec_push_back(vector, str_dup(str1)); -vec_push_back(vector, str_dup(str2)); - -vec *new = vec_cp(vector); -assert(strcmp(vec_pop_back, str2) == 0); -assert(strcmp(vec_pop_back, str1) == 0); +vec *vector = vec_new(); +vec_free(vector); ---- 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 <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> -#include <stdio.h> - #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); } diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 229b4b0..5ab3dc3 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -3,14 +3,22 @@ typedef struct vector vec; +/*constructors*/ vec* vec_new(); vec* vec_with_capacity(int); + +/*management*/ int vec_size(vec*); +vec* vec_cp(vec*); +void vec_print(vec*, char* (void*)); + +/*data*/ void vec_push(vec*, void*); -void* vec_index(vec*, int); void* vec_pop(vec*); -void vec_free(vec*); + +void* vec_index(vec*, int); + +/*memory*/ void vec_clear(vec*); -void vec_print(vec*, char* (void*)); -vec* vec_cp(vec*); +void vec_free(vec*); #endif |