From 8e2d4025fc02e07866869a33ccc686f87389cb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 8 Feb 2019 21:13:08 +0100 Subject: V{calendar,event} merged into vcomponent, making it symmetic. --- vector.inc.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 vector.inc.h (limited to 'vector.inc.h') diff --git a/vector.inc.h b/vector.inc.h new file mode 100644 index 00000000..68f302cd --- /dev/null +++ b/vector.inc.h @@ -0,0 +1,51 @@ +#ifndef TYPE +#error "Set TYPE before including this file" +#else + +#include "macro.h" +#include "err.h" + +INIT_F(VECT(TYPE)) { + this->length = 0; + this->alloc = 0x10; + this->items = calloc(sizeof(*this->items), this->alloc); + return 0; +} + +FREE_F(VECT(TYPE)) { + for (unsigned int i = 0; i < this->length; i++) { + FFREE(TYPE, this->items[i]); + } + free(this->items); + return 0; +} + +int PUSH(VECT(TYPE))(VECT(TYPE)* this, TYPE* t) { + if (this->length + 1 > this->alloc) { + this->alloc <<= 1; + this->items = realloc(this->items, sizeof(*this->items) * this->alloc); + } + + this->items[this->length] = t; + ++this->length; + return 0; +} + +TYPE* GET(VECT(TYPE))(VECT(TYPE)* this, unsigned int idx) { + if (idx >= this->length) { + ERR("Index out of range"); + return NULL; + } + + return this->items[idx]; +} + +int EMPTY(VECT(TYPE))(VECT(TYPE)* this) { + return this->length == 0; +} + +unsigned int SIZE(VECT(TYPE))(VECT(TYPE)* this) { + return this->length; +} + +#endif /* TYPE */ -- cgit v1.2.3