diff options
Diffstat (limited to '')
-rw-r--r-- | vector.inc.h | 51 |
1 files changed, 51 insertions, 0 deletions
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 */ |