import config

class Vector:
    int count
    int element_size
    int allocated
    void *data

Vector *def vector_new(int size):
    Vector *v = ALLOCATE0(sizeof *v)
    v->element_size = size
    return v

def vector_del(Vector *v):
    if (v->data) FREE(v->data)
    FREE(v)

int def vector_cat(Vector *v, void const *data, int n):
    int i = v->count
    v->count += n
    if v->count > v->allocated:
        if v->allocated == 0: v->allocated++
        while v->count > v->allocated: v->allocated *= 2
        v->data = realloc(v->data, v->element_size * v->allocated)
    memcpy((char *)v->data + i * v->element_size, data, v->element_size * n)
    return i

int def vector_add(Vector *v, void *data):
    return vector_cat(v, data, 1) # the 1 is the number of elements (not bytes)

void *def vector_get(Vector *v, int i):
    return (char *)v->data + i * v->element_size

def vector_allocate(Vector *v, int size):
    v->allocated = v->count = size
    v->data = realloc(v->data, v->element_size * v->allocated)
    memset((char *)v->data, 0, v->element_size * v->allocated)