diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-03 00:52:26 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-04 22:58:10 -0400 |
commit | f900066b813fac2f34798c39f16dacda9a878610 (patch) | |
tree | 1138043f4fc623214443a4120aa05281ff8a0a70 /collections | |
parent | 40daf4d7bb71ab3af2b6da71b5868bc7760a6593 (diff) |
Add back function to vectors
back() returns the element in the last position without removing it.
Diffstat (limited to 'collections')
-rw-r--r-- | collections/vector/vector.adoc | 25 | ||||
-rw-r--r-- | collections/vector/vector.c | 10 | ||||
-rw-r--r-- | collections/vector/vector.h | 1 |
3 files changed, 35 insertions, 1 deletions
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 <string.h> + +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); |