Vector
======
Tucker Evans
v0.6, 2020-07-02

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);
----

+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);
----

+void* vec_index(vec *self, int index)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the element at position +index+ of +self+.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

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);
----

+void* vec_pop(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~
Pops an item off of 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(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
<<vec_free,+vec_free()+>>, <<vec_clear,+vec_clear()+>> 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
<<vec_free,+vec_free()+>> should be called immediatly after this.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

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);
----

[[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 <string.h>

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]
----
