libds

a collection of simple data structures
Log | Files | Refs | LICENSE

commit 1538f83e4534ca6c35e4c2fb5514e04d87e5e40d
parent a242631947101a34b1ebcd3ae742623a518ff6db
Author: sin <sin@2f30.org>
Date:   Tue,  8 Apr 2014 09:53:35 +0100

Rename vector to v

Diffstat:
Mvector.c | 88++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 44 insertions(+), 44 deletions(-)

diff --git a/vector.c b/vector.c @@ -13,16 +13,16 @@ struct vector { }; static int -grow(struct vector *vector) +grow(struct vector *v) { struct leaf **tmp, *l; size_t i, j, ne = 0; - tmp = calloc(vector->sz * 2, sizeof(*tmp)); + tmp = calloc(v->sz * 2, sizeof(*tmp)); if (!tmp) return -1; - for (i = 0; i < vector->sz / 2; i++) { - tmp[i] = calloc(vector->sz * 2, sizeof(**tmp)); + for (i = 0; i < v->sz / 2; i++) { + tmp[i] = calloc(v->sz * 2, sizeof(**tmp)); if (!tmp[i]) { while (--i >= 0) free(tmp[i]); @@ -30,94 +30,94 @@ grow(struct vector *vector) return -1; } } - for (i = 0; i < vector->sz; i++) { - l = tmp[ne / (vector->sz * 2)]; - for (j = 0; j < vector->sz; j++) { - l[ne % (vector->sz * 2)].data = vector->l[i][j].data; + for (i = 0; i < v->sz; i++) { + l = tmp[ne / (v->sz * 2)]; + for (j = 0; j < v->sz; j++) { + l[ne % (v->sz * 2)].data = v->l[i][j].data; ne++; } - free(vector->l[i]); + free(v->l[i]); } - free(vector->l); - vector->l = tmp; - vector->sz *= 2; + free(v->l); + v->l = tmp; + v->sz *= 2; return 0; } struct vector * vector_init(void) { - struct vector *vector; + struct vector *v; - vector = calloc(1, sizeof(*vector)); - if (!vector) + v = calloc(1, sizeof(*v)); + if (!v) return NULL; - vector->sz = 2; - vector->l = calloc(vector->sz, sizeof(*vector->l)); - if (!vector->l) { - free(vector); + v->sz = 2; + v->l = calloc(v->sz, sizeof(*v->l)); + if (!v->l) { + free(v); return NULL; } - return vector; + return v; } void -vector_free(struct vector *vector) +vector_free(struct vector *v) { size_t i; - for (i = 0; i < vector->sz; i++) - free(vector->l[i]); - free(vector->l); + for (i = 0; i < v->sz; i++) + free(v->l[i]); + free(v->l); } size_t -vector_add(struct vector *vector, void *data) +vector_add(struct vector *v, void *data) { size_t index, offset; struct leaf *l; - if (vector->ne == vector->sz * vector->sz) - if (grow(vector) < 0) + if (v->ne == v->sz * v->sz) + if (grow(v) < 0) return -1; - index = vector->ne / vector->sz; - offset = vector->ne % vector->sz; - if (!vector->l[index]) { - vector->l[index] = calloc(vector->sz, sizeof(**vector->l)); - if (!vector->l[index]) + index = v->ne / v->sz; + offset = v->ne % v->sz; + if (!v->l[index]) { + v->l[index] = calloc(v->sz, sizeof(**v->l)); + if (!v->l[index]) return -1; } - l = vector->l[index]; + l = v->l[index]; l[offset].data = data; - return vector->ne++; + return v->ne++; } void * -vector_get(struct vector *vector, size_t i) +vector_get(struct vector *v, size_t i) { size_t index, offset; struct leaf *l; - index = i / vector->sz; - offset = i % vector->sz; - if (vector->l[index]) { - l = vector->l[index]; + index = i / v->sz; + offset = i % v->sz; + if (v->l[index]) { + l = v->l[index]; return l[offset].data; } return NULL; } size_t -vector_len(struct vector *vector) +vector_len(struct vector *v) { - return vector->ne; + return v->ne; } void -vector_walk(struct vector *vector, void (*cb)(struct vector *, void *)) +vector_walk(struct vector *v, void (*cb)(struct vector *, void *)) { size_t i; - for (i = 0; i < vector->ne; i++) - cb(vector, vector->l[i / vector->sz][i % vector->sz].data); + for (i = 0; i < v->ne; i++) + cb(v, v->l[i / v->sz][i % v->sz].data); }