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 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 collections/vector/vector.adoc (limited to 'collections/vector/vector.adoc') 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); +---- -- 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 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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); +---- -- 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 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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); +---- -- 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 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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); +---- -- 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 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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); +---- -- 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 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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] +---- -- 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 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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); +---- -- 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(-) (limited to 'collections/vector/vector.adoc') 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 ++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 79 deletions(-) (limited to 'collections/vector/vector.adoc') 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); ---- -- 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 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~ -- 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(-) (limited to 'collections/vector/vector.adoc') 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 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ -- 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 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- 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 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'collections/vector/vector.adoc') 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() -- 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 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'collections/vector/vector.adoc') 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)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.1