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