1 #include "yagl_vector.h"
3 #define YAGL_VECTOR_DEFAULT_CAPACITY 10
5 void yagl_vector_init(struct yagl_vector *v,
11 v->elem_size = elem_size;
12 v->capacity = (initial_capacity < YAGL_VECTOR_DEFAULT_CAPACITY) ?
13 YAGL_VECTOR_DEFAULT_CAPACITY
15 v->data = g_malloc0(v->capacity * v->elem_size);
19 void yagl_vector_cleanup(struct yagl_vector *v)
22 memset(v, 0, sizeof(*v));
25 void *yagl_vector_detach(struct yagl_vector *v)
28 memset(v, 0, sizeof(*v));
32 int yagl_vector_size(struct yagl_vector *v)
37 int yagl_vector_capacity(struct yagl_vector *v)
42 void yagl_vector_push_back(struct yagl_vector *v, const void *elem)
44 if (v->size >= v->capacity) {
47 v->capacity = (v->size * 3) / 2;
49 tmp = g_malloc(v->capacity * v->elem_size);
50 memcpy(tmp, v->data, (v->size * v->elem_size));
57 memcpy((char*)v->data + (v->size * v->elem_size), elem, v->elem_size);
62 void yagl_vector_resize(struct yagl_vector *v, int new_size)
64 if (new_size <= v->capacity) {
69 v->capacity = (new_size * 3) / 2;
71 tmp = g_malloc(v->capacity * v->elem_size);
72 memcpy(tmp, v->data, (v->size * v->elem_size));
81 void *yagl_vector_data(struct yagl_vector *v)