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