From 65ddeea23e1303d0b2d9656601b90df138d19e6c Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 1 Jul 2020 16:19:16 -0400 Subject: Add start to vector --- collections/vector/vector.adoc | 65 ++++++++++++++++++++++++++++++++++++++++++ collections/vector/vector.c | 53 ++++++++++++++++++++++++++++++++++ collections/vector/vector.h | 9 ++++++ 3 files changed, 127 insertions(+) create mode 100644 collections/vector/vector.adoc create mode 100644 collections/vector/vector.c create mode 100644 collections/vector/vector.h diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc new file mode 100644 index 0000000..b9101ca --- /dev/null +++ b/collections/vector/vector.adoc @@ -0,0 +1,65 @@ +Vector +====== +Tucker Evans +v0.1, 2020-07-01 + +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); +---- diff --git a/collections/vector/vector.c b/collections/vector/vector.c new file mode 100644 index 0000000..2c75bc1 --- /dev/null +++ b/collections/vector/vector.c @@ -0,0 +1,53 @@ +#include "vector.h" + +#include +#include +#include + +#include + +#define START_SIZE 64; + +struct vector { + void **base; + int end, limit; +}; + +vec* vec_new() +{ + vec *root; + + root = malloc(sizeof(vec)); + assert(root); + + root->limit = START_SIZE; + root->base = malloc(root->limit * sizeof(void*)); + assert(root->base); + + return root; +} + +vec* vec_with_capacity(n) +int n; +{ + vec *root; + + root = malloc(sizeof(vec)); + assert(root); + + root->limit = n; + root->base = malloc(root->limit * sizeof(void*)); + assert(root->base); + + return root; +} + +int vec_size(root) +vec *root; +{ + if (!root) { + return -1; + } + return root->end; +} + diff --git a/collections/vector/vector.h b/collections/vector/vector.h new file mode 100644 index 0000000..5465108 --- /dev/null +++ b/collections/vector/vector.h @@ -0,0 +1,9 @@ +#ifndef VECTOR_H +#define VECTOR_H + +typedef struct vector vec; + +vec* vec_new(); +vec* vec_with_capacity(int); +int vec_size(vec*); +#endif -- cgit v1.1