diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-02 17:32:56 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-02 19:41:22 -0400 |
commit | 0d09851c392829a6cbbd047f9a374b5c66eb4c98 (patch) | |
tree | 1bc98adad193667f60f5c918a6deaa244a89fbbc | |
parent | bc9e78bb7e3fd57a3683e6c8a848b5769d2b21f7 (diff) |
Add push for vector
-rw-r--r-- | collections/vector/vector.adoc | 17 | ||||
-rw-r--r-- | collections/vector/vector.c | 14 | ||||
-rw-r--r-- | collections/vector/vector.h | 1 |
3 files changed, 31 insertions, 1 deletions
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 |