From 65ddeea23e1303d0b2d9656601b90df138d19e6c Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 1 Jul 2020 16:19:16 -0400 Subject: Add start to vector --- collections/vector/vector.adoc | 65 ++++++++++++++++++++++++++++++++++++++++++ collections/vector/vector.c | 53 ++++++++++++++++++++++++++++++++++ collections/vector/vector.h | 9 ++++++ 3 files changed, 127 insertions(+) create mode 100644 collections/vector/vector.adoc create mode 100644 collections/vector/vector.c create mode 100644 collections/vector/vector.h diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc new file mode 100644 index 0000000..b9101ca --- /dev/null +++ b/collections/vector/vector.adoc @@ -0,0 +1,65 @@ +Vector +====== +Tucker Evans +v0.1, 2020-07-01 + +A basic vector, that hold pointers to your data structures. + +NOTE: There is currently no way to distinquish between a failed retrieval +(pop, index, back, etc.) and returning a NULL value. Keep this in mind if +you plan on storing NULL values in the vector, there are plans to fix this in +the future. + +Types +----- + ++vec+ +~~~~~ +This structure holds all internal information regarding a vector. +All functions (except constructors) expect a pointer to this struct as their +first parameter. + +Functions +--------- + ++vec* vec_new()+ +~~~~~~~~~~~~~~~~ +Constructs an empty vector. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_new(); +---- + +`vec* vec_with_capacity(int capacity)` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Constructs an empty vector with space for +capacity+ items. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_with_capacity(16); +---- + ++int vec_size(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~ +Returns the number of elements in vector +self+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_new(); +assert(vec_size(vector) == 0); +vec_push_back(vector, NULL); +assert(vec_size(vector) == 1); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c new file mode 100644 index 0000000..2c75bc1 --- /dev/null +++ b/collections/vector/vector.c @@ -0,0 +1,53 @@ +#include "vector.h" + +#include +#include +#include + +#include + +#define START_SIZE 64; + +struct vector { + void **base; + int end, limit; +}; + +vec* vec_new() +{ + vec *root; + + root = malloc(sizeof(vec)); + assert(root); + + root->limit = START_SIZE; + root->base = malloc(root->limit * sizeof(void*)); + assert(root->base); + + return root; +} + +vec* vec_with_capacity(n) +int n; +{ + vec *root; + + root = malloc(sizeof(vec)); + assert(root); + + root->limit = n; + root->base = malloc(root->limit * sizeof(void*)); + assert(root->base); + + return root; +} + +int vec_size(root) +vec *root; +{ + if (!root) { + return -1; + } + return root->end; +} + diff --git a/collections/vector/vector.h b/collections/vector/vector.h new file mode 100644 index 0000000..5465108 --- /dev/null +++ b/collections/vector/vector.h @@ -0,0 +1,9 @@ +#ifndef VECTOR_H +#define VECTOR_H + +typedef struct vector vec; + +vec* vec_new(); +vec* vec_with_capacity(int); +int vec_size(vec*); +#endif -- cgit v1.1 From bc9e78bb7e3fd57a3683e6c8a848b5769d2b21f7 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 1 Jul 2020 16:43:07 -0400 Subject: Add resize function for vector --- collections/vector/vector.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 2c75bc1..c2668e4 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -51,3 +51,13 @@ vec *root; return root->end; } +void vec_resize(root) +vec *root; +{ + if (!root) + return; + + root->base = reallocarray(root->base, root->limit * 2, sizeof(void*)); + assert(root->base); +} + -- cgit v1.1 From 0d09851c392829a6cbbd047f9a374b5c66eb4c98 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 2 Jul 2020 17:32:56 -0400 Subject: Add push for vector --- collections/vector/vector.adoc | 17 ++++++++++++++++- collections/vector/vector.c | 14 ++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index b9101ca..cf001d3 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.1, 2020-07-01 +v0.2, 2020-07-02 A basic vector, that hold pointers to your data structures. @@ -63,3 +63,18 @@ assert(vec_size(vector) == 0); vec_push_back(vector, NULL); assert(vec_size(vector) == 1); ---- + ++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); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index c2668e4..afe99de 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -61,3 +61,17 @@ vec *root; assert(root->base); } +void vec_push(root, item) +vec *root; +void *item; +{ + if (!root) { + return; + } + + if (root->end >= root->limit) { + vec_resize(root); + } + + root->base[root->end++] = item; +} diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 5465108..b84217a 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -6,4 +6,5 @@ typedef struct vector vec; vec* vec_new(); vec* vec_with_capacity(int); int vec_size(vec*); +void vec_push(vec*, void*); #endif -- cgit v1.1 From 264857903db99135c8c09d12b3e8af93ac1c7f88 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 2 Jul 2020 19:07:46 -0400 Subject: Add index functionality to vectors --- collections/vector/vector.adoc | 25 ++++++++++++++++++++++++- collections/vector/vector.c | 11 +++++++++++ collections/vector/vector.h | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index cf001d3..1dce41b 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.2, 2020-07-02 +v0.3, 2020-07-02 A basic vector, that hold pointers to your data structures. @@ -78,3 +78,26 @@ vec *vector = vec_new(); vec_push(vector, NULL); assert(vec_size(vector) == 1); ---- + ++void* vec_index(vec *self, int index)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Returns the element at position +index+ of +self+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +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)); + +assert(str_cmp(vec_index(vector, 1), str2) == 0); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index afe99de..9d895d5 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -75,3 +75,14 @@ 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]; +} diff --git a/collections/vector/vector.h b/collections/vector/vector.h index b84217a..63151ac 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -7,4 +7,5 @@ vec* vec_new(); vec* vec_with_capacity(int); int vec_size(vec*); void vec_push(vec*, void*); +void* vec_index(vec*, int); #endif -- cgit v1.1 From 98454f841b5ac87d2253c2fae0aba2525853f907 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 2 Jul 2020 19:16:19 -0400 Subject: Add pop to vectors --- collections/vector/vector.adoc | 24 +++++++++++++++++++++++- collections/vector/vector.c | 10 ++++++++++ collections/vector/vector.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 1dce41b..8ec8b49 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.3, 2020-07-02 +v0.4, 2020-07-02 A basic vector, that hold pointers to your data structures. @@ -101,3 +101,25 @@ vec_push(vector, str_dup(str3)); assert(str_cmp(vec_index(vector, 1), str2) == 0); ---- + ++void* vec_pop(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~ +Pops an item off of the back of the vector +self+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); + +assert(str_cmp(vec_pop(vector), str2) == 0); +assert(str_cmp(vec_pop(vector), str1) == 0); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 9d895d5..936c0c8 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -86,3 +86,13 @@ int index; return root->base[index]; } + +void* vec_pop(root) +vec *root; +{ + if (!root || root->end == 0) { + return NULL; + } + + return root->base[--root->end]; +} diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 63151ac..81eea22 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -8,4 +8,5 @@ vec* vec_with_capacity(int); int vec_size(vec*); void vec_push(vec*, void*); void* vec_index(vec*, int); +void* vec_pop(vec*); #endif -- cgit v1.1 From e4ea23a6c23acdec86e5b5fcd34c653118f6898e Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 2 Jul 2020 19:20:05 -0400 Subject: Adds free & clear functions to vectors --- collections/vector/vector.adoc | 48 +++++++++++++++++++++++++++++++++++++++++- collections/vector/vector.c | 22 +++++++++++++++++++ collections/vector/vector.h | 2 ++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 8ec8b49..3ffd10c 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.4, 2020-07-02 +v0.5, 2020-07-02 A basic vector, that hold pointers to your data structures. @@ -123,3 +123,49 @@ vec_push(vector, str_dup(str2)); 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 +<>, <> should be called before +if they are no longer needed to avoid memory leaks. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_new(); +vec_free(vector); +---- + +[[vec_clear]] ++void vec_clear(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Free all elements within vector +self+, and sets vector to empty (size 0). + +NOTE: Does not free internal memory of +self+ or +self+ itself, if this is desired +<> should be called immediatly after this. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push_back(vector, str_dup(str1)); +vec_push_back(vector, str_dup(str2)); + +vec_clear(vector); +assert(vec_size(vector) == 0); +vec_free(vector); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 936c0c8..9544917 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -96,3 +96,25 @@ vec *root; return root->base[--root->end]; } + +void vec_free(root) +vec *root; +{ + free(root->base); + root->base = NULL; + + free(root); +} + +void vec_clear(root) +vec *root; +{ + int i; + + for (i = 0; i < root->end; i++) { + free(vec_index(root, i)); + } + + root->end = 0; +} + diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 81eea22..3996030 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -9,4 +9,6 @@ int vec_size(vec*); void vec_push(vec*, void*); void* vec_index(vec*, int); void* vec_pop(vec*); +void vec_free(vec*); +void vec_clear(vec*); #endif -- cgit v1.1 From 80d98a39a6bc4b71c74f240df617495e816b98b9 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Thu, 2 Jul 2020 20:21:28 -0400 Subject: Add printing functions for vectors --- collections/vector/vector.adoc | 45 +++++++++++++++++++++++++++++++++++++++++- collections/vector/vector.c | 26 ++++++++++++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 3ffd10c..fbb0b32 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.5, 2020-07-02 +v0.6, 2020-07-02 A basic vector, that hold pointers to your data structures. @@ -169,3 +169,46 @@ vec_clear(vector); 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 + +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] +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 9544917..b3751dd 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -118,3 +118,29 @@ 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"); +} diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 3996030..54e40a7 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -11,4 +11,5 @@ void* vec_index(vec*, int); void* vec_pop(vec*); void vec_free(vec*); void vec_clear(vec*); +void vec_print(vec*, char* (void*)); #endif -- cgit v1.1 From 8d5b381b540fe1d780311e4f4059dce014a6cb91 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:13:04 -0400 Subject: Add copy function for vector --- collections/vector/vector.adoc | 26 +++++++++++++++++++++++++- collections/vector/vector.c | 20 ++++++++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index fbb0b32..4920893 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.6, 2020-07-02 +v0.7, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -212,3 +212,27 @@ Output: VEC_CONTENTS: [ONE,TWO,THREE] ---- + +[[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 + +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); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c index b3751dd..8aaffb6 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -144,3 +144,23 @@ vec *root; } fprintf(stderr, "\n"); } + +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; +} diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 54e40a7..229b4b0 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -12,4 +12,5 @@ void* vec_pop(vec*); void vec_free(vec*); void vec_clear(vec*); void vec_print(vec*, char* (void*)); +vec* vec_cp(vec*); #endif -- cgit v1.1 From 7ed6d089f5533904ac556edac4accc7ef9a8badc Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:13:19 -0400 Subject: Add anchors for functions in vector documentation --- collections/vector/vector.adoc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 4920893..63cdd78 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.7, 2020-07-03 +v0.7.1, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -14,14 +14,14 @@ Types ----- +vec+ -~~~~~ + This structure holds all internal information regarding a vector. All functions (except constructors) expect a pointer to this struct as their first parameter. Functions --------- - +[[vec_new]] +vec* vec_new()+ ~~~~~~~~~~~~~~~~ Constructs an empty vector. @@ -48,6 +48,7 @@ Examples vec *vector = vec_with_capacity(16); ---- +[[vec_size]] +int vec_size(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~ Returns the number of elements in vector +self+. @@ -64,6 +65,7 @@ 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. @@ -79,6 +81,7 @@ vec_push(vector, NULL); assert(vec_size(vector) == 1); ---- +[[vec_index]] +void* vec_index(vec *self, int index)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Returns the element at position +index+ of +self+. @@ -102,6 +105,7 @@ vec_push(vector, str_dup(str3)); assert(str_cmp(vec_index(vector, 1), str2) == 0); ---- +[[vec_pop]] +void* vec_pop(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ Pops an item off of the back of the vector +self+. -- cgit v1.1 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.adoc | 158 ++++++++++++++++++++--------------------- collections/vector/vector.c | 117 +++++++++++++++--------------- 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 -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 +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 -<>, <> 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 + +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 - -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 +<>, <> should be called before +if they are no longer needed to avoid memory leaks. Examples ^^^^^^^^ [source,c] ---- #include "vector.h" -#include - -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 #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); } 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 -- cgit v1.1 From 4832ab4b43a2e9fb72b1814b32421de1dba1edcb Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:40:50 -0400 Subject: Add function to get capacity of a vector --- collections/vector/vector.adoc | 17 ++++++++++++++++- collections/vector/vector.c | 10 ++++++++++ collections/vector/vector.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 76c0f7a..f1e29e2 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.7.2, 2020-07-03 +v0.8, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -65,6 +65,21 @@ vec_push_back(vector, NULL); assert(vec_size(vector) == 1); ---- ++int vec_capacity(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Returns the maximun number of elements in vector +self+ before the buffer needs +to be resized (does not take the current size into consideration). + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_with_capacity(16); +assert(vec_capacity(vector) == 16); +---- + [[vec_cp]] +vec* vec_cp(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 23e5c47..b8f83de 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -63,6 +63,16 @@ vec *root; return root->end; } +int vec_capacity(root) +vec *root; +{ + if (!root) { + return -1; + } + + return root->limit; +} + void vec_resize(root) vec *root; { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 5ab3dc3..4bcb620 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -9,6 +9,7 @@ vec* vec_with_capacity(int); /*management*/ int vec_size(vec*); +int vec_capacity(vec*); vec* vec_cp(vec*); void vec_print(vec*, char* (void*)); -- cgit v1.1 From 40daf4d7bb71ab3af2b6da71b5868bc7760a6593 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:49:35 -0400 Subject: Fix remove "_back" from pop/push calls in vector doc These were left in some examples after copying from double ended queue documentation. --- collections/vector/vector.adoc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index f1e29e2..3fe2dbf 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.8, 2020-07-03 +v0.8.1, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -61,7 +61,7 @@ Examples vec *vector = vec_new(); assert(vec_size(vector) == 0); -vec_push_back(vector, NULL); +vec_push(vector, NULL); assert(vec_size(vector) == 1); ---- @@ -96,12 +96,12 @@ 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_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); vec *new = vec_cp(vector); -assert(strcmp(vec_pop_back, str2) == 0); -assert(strcmp(vec_pop_back, str1) == 0); +assert(strcmp(vec_pop, str2) == 0); +assert(strcmp(vec_pop, str1) == 0); ---- [[vec_print]] @@ -229,8 +229,8 @@ char *str1 = "ONE"; char *str2 = "TWO"; vec *vector = vec_new(); -vec_push_back(vector, str_dup(str1)); -vec_push_back(vector, str_dup(str2)); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); vec_clear(vector); assert(vec_size(vector) == 0); -- cgit v1.1 From f900066b813fac2f34798c39f16dacda9a878610 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:52:26 -0400 Subject: Add back function to vectors back() returns the element in the last position without removing it. --- collections/vector/vector.adoc | 25 ++++++++++++++++++++++++- collections/vector/vector.c | 10 ++++++++++ collections/vector/vector.h | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 3fe2dbf..d439c85 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.8.1, 2020-07-03 +v0.9, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -186,6 +186,29 @@ assert(str_cmp(vec_pop(vector), str2) == 0); assert(str_cmp(vec_pop(vector), str1) == 0); ---- +[[vec_back]] ++void* vec_back(vec *self)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Returns the element at the back of the vector +self+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); + +assert(strcmp(vec_back(vector), str2) == 0); +---- + + [[vec_index]] +void* vec_index(vec *self, int index)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index b8f83de..0b816b7 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -142,6 +142,16 @@ vec *root; return root->base[--root->end]; } +void* vec_back(root) +vec *root; +{ + if (!root || root->end == 0) { + return NULL; + } + + return root->base[root->end - 1]; +} + void* vec_index(root, index) vec *root; int index; diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 4bcb620..f1eea64 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -16,6 +16,7 @@ void vec_print(vec*, char* (void*)); /*data*/ void vec_push(vec*, void*); void* vec_pop(vec*); +void* vec_back(vec*); void* vec_index(vec*, int); -- cgit v1.1 From b253b373ea74539a0f29a88ac2be728aa8158d2a Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 00:57:58 -0400 Subject: Add set index function for vectors --- collections/vector/vector.adoc | 26 +++++++++++++++++++++++++- collections/vector/vector.c | 11 +++++++++++ collections/vector/vector.h | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index d439c85..0409b05 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.9, 2020-07-03 +v0.10, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -208,6 +208,30 @@ vec_push(vector, str_dup(str2)); assert(strcmp(vec_back(vector), str2) == 0); ---- +[[vec_set]] ++void vec_set(vec *self, int index, void *item)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Sets the element at position +index+ in +self+ to +item+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); + +vec_set(vector, 0, str2); + +assert(str_cmp(vec_pop(vector), str2) == 0); +assert(str_cmp(vec_pop(vector), str2) == 0); +---- [[vec_index]] +void* vec_index(vec *self, int index)+ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 0b816b7..5cb56ef 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -152,6 +152,17 @@ vec *root; return root->base[root->end - 1]; } +void vec_set(root, index, item) +vec *root; +int index; +void *item; +{ + if (!root || index >= root->end || index < 0) + return; + + root->base[index] = item; +} + void* vec_index(root, index) vec *root; int index; diff --git a/collections/vector/vector.h b/collections/vector/vector.h index f1eea64..7d38694 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -18,6 +18,7 @@ void vec_push(vec*, void*); void* vec_pop(vec*); void* vec_back(vec*); +void vec_set(vec*, int, void*); void* vec_index(vec*, int); /*memory*/ -- cgit v1.1 From 72cb2e77024970509ff403670f9e4245bcefebd6 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 01:05:09 -0400 Subject: Add swap function to vectors --- collections/vector/vector.adoc | 28 +++++++++++++++++++++++++++- collections/vector/vector.c | 16 ++++++++++++++++ collections/vector/vector.h | 3 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 0409b05..cc83375 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.10, 2020-07-03 +v0.11, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -257,6 +257,32 @@ vec_push(vector, str_dup(str3)); assert(str_cmp(vec_index(vector, 1), str2) == 0); ---- +[[vec_swap]] ++void vec_swap(vec *self, int i, int j)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Swaps elements at positions +i+ and +j+, does nothing if +i+ or +j+ are out of +bounds. + +Examples +^^^^^^^^ +[source,c] +---- +#include "dvector.h" +#include + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); + +vec_swap(vector, 0, 1); + +assert(str_cmp(vec_index(vector, 0), str2) == 0); +assert(str_cmp(vec_back(vector), str1) == 0); +---- + [[vec_clear]] +void vec_clear(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 5cb56ef..a7e0845 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -174,6 +174,22 @@ int index; return root->base[index]; } +void vec_swap(root, i, j) +vec *root; +int i,j; +{ + void *tmp; + + if (!root || i >= root->end || i < 0 || j >= root->end || j < 0) + return; + + tmp = root->base[i]; + root->base[i] = root->base[j]; + root->base[j] = tmp; + + return; +} + void vec_clear(root) vec *root; { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 7d38694..2b1e8e9 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -21,6 +21,9 @@ void* vec_back(vec*); void vec_set(vec*, int, void*); void* vec_index(vec*, int); +void vec_swap(vec*, int, int); + + /*memory*/ void vec_clear(vec*); void vec_free(vec*); -- cgit v1.1 From 1a41fe605ab6bde15f622e72abb7aee846531fcf Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 01:18:17 -0400 Subject: Add swap_pop function for vectors --- collections/vector/vector.adoc | 30 +++++++++++++++++++++++++++++- collections/vector/vector.c | 14 ++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index cc83375..ee453ff 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.11, 2020-07-03 +v0.12, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -283,6 +283,34 @@ assert(str_cmp(vec_index(vector, 0), str2) == 0); assert(str_cmp(vec_back(vector), str1) == 0); ---- +[[vec_swap_pop]] ++void* vec_swap_pop(vec *self, int index)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Swaps back element with item at +index+, and pops item now at back. +Will return same element as +vec_remove(self, index)+. +Does not keep order of elements, but faster that <>. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +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)); + +assert(str_cmp(vec_swap_pop(vector, 2), str3) == 0); +assert(str_cmp(vec_back(vector, str2) == 0); +assert(vec_size == 2); +---- + [[vec_clear]] +void vec_clear(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index a7e0845..9ae30c7 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -190,6 +190,20 @@ int i,j; return; } +void* vec_swap_pop(root, i) +vec *root; +int i; +{ + void *tmp; + int j; + + if (!root || i >= root->end || i < 0 || root->end <= 0) + return NULL; + + vec_swap(root, i, root->end - 1); + return vec_pop(root); +} + void vec_clear(root) vec *root; { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 2b1e8e9..55e9481 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -22,6 +22,7 @@ void vec_set(vec*, int, void*); void* vec_index(vec*, int); void vec_swap(vec*, int, int); +void* vec_swap_pop(vec*, int); /*memory*/ -- cgit v1.1 From a691f4d4b0a57d62eb792e6ede0c0fa5334e3e61 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 01:18:39 -0400 Subject: Fix bounds checking for vector pop Would have failed given a negative index. --- collections/vector/vector.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 9ae30c7..79e31a4 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -135,7 +135,7 @@ void *item; void* vec_pop(root) vec *root; { - if (!root || root->end == 0) { + if (!root || root->end <= 0) { return NULL; } -- cgit v1.1 From e6af2f70ec910ee8cb3813fa1e5b1c7d6e3ead60 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 23:49:15 -0400 Subject: Add insert function for vectors --- collections/vector/vector.adoc | 29 ++++++++++++++++++++++++++++- collections/vector/vector.c | 17 +++++++++++++++++ collections/vector/vector.h | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index ee453ff..9e61a43 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.12, 2020-07-03 +v0.13, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -257,6 +257,33 @@ vec_push(vector, str_dup(str3)); assert(str_cmp(vec_index(vector, 1), str2) == 0); ---- +[[vec_insert]] ++void vec_insert(vec *self, int index, void *item)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Inserts +item+ into vector +self+ at index +index+, all items after the index +are pushed toward the end. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +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_insert(vector, 1, str_dup(str3)); + +assert(str_cmp(vec_index(vector, 1), str3) == 0); +assert(str_cmp(vec_index(vector, 2), str2) == 0); +---- + [[vec_swap]] +void vec_swap(vec *self, int i, int j)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 79e31a4..2ae748f 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -174,6 +174,23 @@ int index; return root->base[index]; } +void vec_insert(root, index, item) +vec *root; +int index; +void *item; +{ + if (!root || index > root->end || index < 0) + return; + + if (root->end >= root->limit) + vec_resize(root); + + memmove(root->base + index + 1, root->base + index, + (root->end++ - index) * sizeof(void*)); + + root->base[index] = item; +} + void vec_swap(root, i, j) vec *root; int i,j; diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 55e9481..1b47642 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -21,6 +21,8 @@ void* vec_back(vec*); void vec_set(vec*, int, void*); void* vec_index(vec*, int); +void vec_insert(vec*, int, void*); + void vec_swap(vec*, int, int); void* vec_swap_pop(vec*, int); -- cgit v1.1 From a1d4279c40612e6f98564db43192929b41c59d40 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Fri, 3 Jul 2020 23:57:45 -0400 Subject: Add remove function for vectors Removes element at an index. --- collections/vector/vector.adoc | 31 ++++++++++++++++++++++++++++++- collections/vector/vector.c | 16 ++++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 9e61a43..d423c37 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.13, 2020-07-03 +v0.14, 2020-07-03 A basic vector, that hold pointers to your data structures. @@ -284,6 +284,35 @@ assert(str_cmp(vec_index(vector, 1), str3) == 0); assert(str_cmp(vec_index(vector, 2), str2) == 0); ---- +[[vec_remove]] ++void vec_remove(vec *self, int index)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Element at position +index+ of +self+ is removed from vector and the remaining +items are shifted towards the front. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +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)); + +vec_remove(vector, 1); + +assert(vec_size == 2); +assert(strcmp(vec_pop(vector), str3) == 0); +assert(strcmp(vec_pop(vector), str1) == 0); +---- + [[vec_swap]] +void vec_swap(vec *self, int i, int j)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 2ae748f..5c473a4 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -191,6 +191,22 @@ void *item; root->base[index] = item; } +void* vec_remove(root, index) +vec *root; +int index; +{ + void *tmp; + + if (!root || index > root->end || index < 0) + return NULL; + + tmp = vec_index(root, index); + memmove(root->base + index, root->base + index + 1, + (--root->end - index) * sizeof(void*)); + + return tmp; +} + void vec_swap(root, i, j) vec *root; int i,j; diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 1b47642..083a1d0 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -22,6 +22,7 @@ void vec_set(vec*, int, void*); void* vec_index(vec*, int); void vec_insert(vec*, int, void*); +void* vec_remove(vec*, int); void vec_swap(vec*, int, int); void* vec_swap_pop(vec*, int); -- cgit v1.1 From 8874cf97227139ed10e75fe13108988b45492172 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 4 Jul 2020 22:06:49 -0400 Subject: Add truncate to vectors --- collections/vector/vector.adoc | 32 +++++++++++++++++++++++++++++++- collections/vector/vector.c | 10 ++++++++++ collections/vector/vector.h | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index d423c37..e3fa94a 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.14, 2020-07-03 +v0.15, 2020-07-04 A basic vector, that hold pointers to your data structures. @@ -367,6 +367,36 @@ assert(str_cmp(vec_back(vector, str2) == 0); assert(vec_size == 2); ---- +[[vec_truncate]] ++void vec_truncate(vec *self, int size)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Truncates vector +self+ to +size+ elements, elements in positions > +size+ will +no longer be accessable through +self+. Does nothing if +size+ > current number +of elements. + +NOTE: Does not currently reduce memory footprint + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" +#include + +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)); + +vec_truncate(vector, 1); + +assert(vec_size == 1); +---- + [[vec_clear]] +void vec_clear(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 5c473a4..c456d8a 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -237,6 +237,16 @@ int i; return vec_pop(root); } +void vec_truncate(root, size) +vec *root; +int size; +{ + if (!root || size < 0 || size >= root->end) + return; + + root->end = size; +} + void vec_clear(root) vec *root; { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 083a1d0..b997b46 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -29,6 +29,8 @@ void* vec_swap_pop(vec*, int); /*memory*/ +void vec_truncate(vec*, int); + void vec_clear(vec*); void vec_free(vec*); #endif -- cgit v1.1 From 8aa9f42d914a453a0570752ee292b9a9cfcae9d6 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 4 Jul 2020 22:07:23 -0400 Subject: Fix vector print function now frees string representations --- collections/vector/vector.adoc | 7 +++++-- collections/vector/vector.c | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index e3fa94a..ce91c71 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.15, 2020-07-04 +v0.16, 2020-07-04 A basic vector, that hold pointers to your data structures. @@ -112,6 +112,9 @@ 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. +NOTE: Strings are freed after printing, therefore +strdup()+ should be used on +any strings that are not for the sole purpose of printing here. + Examples ^^^^^^^^ [source,c] @@ -122,7 +125,7 @@ Examples char* to_string(str) void *str; { - return str; + return strdup(str); } int main() diff --git a/collections/vector/vector.c b/collections/vector/vector.c index c456d8a..391b5a6 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -108,10 +108,13 @@ vec *root; char* to_string(void*); { int i; + char *tmp; printf("["); for(i = 0; i < root->end; i++) { - printf("%s", to_string(vec_index(root, i))); + printf("%s", tmp = to_string(vec_index(root, i))); + free(tmp); + tmp = NULL; } printf("\b]\n"); -- cgit v1.1 From 5fa1551b4a0acc28d5c656479806511ab0c81a18 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sat, 4 Jul 2020 22:17:27 -0400 Subject: Add reserve function to vectors --- collections/vector/vector.adoc | 20 +++++++++++++++++++- collections/vector/vector.c | 14 ++++++++++++++ collections/vector/vector.h | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index ce91c71..7c4bc7f 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@ Vector ====== Tucker Evans -v0.16, 2020-07-04 +v0.17, 2020-07-04 A basic vector, that hold pointers to your data structures. @@ -400,6 +400,24 @@ vec_truncate(vector, 1); assert(vec_size == 1); ---- +[[vec_reserve]] ++void vec_reserve(vec *self, int additional)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Reserves space for +additional+ items. May reserve more memory to avoid too +many reallocations. + +Examples +^^^^^^^^ +[source,c] +---- +#include "vector.h" + +vec *vector = vec_with_capacity(16); + +vec_reserve(vector, 20); +assert(vec_capacity(vector) >= 20); +---- + [[vec_clear]] +void vec_clear(vec *self)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 391b5a6..f14c302 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -250,6 +250,20 @@ int size; root->end = size; } +void vec_reserve(root, n) +vec *root; +int n; +{ + int i; + + if (!root || n + root->end <= root->limit) + return; + + for (i = root->limit; i < root->end + n; i*=2); + + root->base = reallocarray(root->base, i, sizeof(void*)); +} + void vec_clear(root) vec *root; { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index b997b46..4d6d04c 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -30,6 +30,7 @@ void* vec_swap_pop(vec*, int); /*memory*/ void vec_truncate(vec*, int); +void vec_reserve(vec*, int); void vec_clear(vec*); void vec_free(vec*); -- cgit v1.1