vector.c (1584B)
1 /* Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail> 2 * See LICENSE file for license details. 3 */ 4 #include <string.h> 5 #include "util.h" 6 7 /* Blech. */ 8 #define VECTOR(type, nam, c) \ 9 void \ 10 vector_##c##init(Vector_##nam *v) { \ 11 memset(v, 0, sizeof *v); \ 12 } \ 13 \ 14 void \ 15 vector_##c##free(Vector_##nam *v) { \ 16 free(v->ary); \ 17 memset(v, 0, sizeof *v); \ 18 } \ 19 \ 20 void \ 21 vector_##c##push(Vector_##nam *v, type val) { \ 22 if(v->n == v->size) { \ 23 if(v->size == 0) \ 24 v->size = 2; \ 25 v->size <<= 2; \ 26 v->ary = erealloc(v->ary, v->size * sizeof *v->ary); \ 27 } \ 28 v->ary[v->n++] = val; \ 29 } \ 30 31 VECTOR(long, long, l) 32 VECTOR(Rectangle, rect, r) 33 VECTOR(void*, ptr, p)