From c3d0c99023bd3394de2b9072f7e4c8699f805490 Mon Sep 17 00:00:00 2001 From: Dvir Volk Date: Mon, 11 Jul 2016 15:54:39 +0300 Subject: [PATCH] added support for NULL insert on vectors --- rmutil/vector.c | 15 +++++++++------ rmutil/vector.h | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/rmutil/vector.c b/rmutil/vector.c index 5d4037e..1433e42 100644 --- a/rmutil/vector.c +++ b/rmutil/vector.c @@ -1,13 +1,12 @@ #include "vector.h" #include -int __vector_PushPtr(Vector *v, void *elem) { - if (v->top == v->cap - 1) { +inline int __vector_PushPtr(Vector *v, void *elem) { + if (v->top == v->cap) { Vector_Resize(v, v->cap ? v->cap * 2 : 1); } - __vector_PutPtr(v, v->top, elem); - v->top++; + __vector_PutPtr(v, v->top++, elem); return v->top; } @@ -33,13 +32,17 @@ inline int Vector_Pop(Vector *v, void *ptr) { return 0; } -int __vector_PutPtr(Vector *v, size_t pos, void *elem) { +inline int __vector_PutPtr(Vector *v, size_t pos, void *elem) { // resize if pos is out of bounds if (pos >= v->cap) { Vector_Resize(v, pos + 1); } - memcpy(v->data + pos * v->elemSize, elem, v->elemSize); + if (elem) { + memcpy(v->data + pos * v->elemSize, elem, v->elemSize); + } else { + memset(v->data + pos * v->elemSize, 0, v->elemSize); + } // move the end offset to pos if we grew if (pos > v->top) { v->top = pos; diff --git a/rmutil/vector.h b/rmutil/vector.h index b255787..5d6e097 100644 --- a/rmutil/vector.h +++ b/rmutil/vector.h @@ -47,11 +47,11 @@ int Vector_Pop(Vector *v, void *ptr); * Put an element at pos. * Note: If pos is outside the vector capacity, we resize it accordingly */ -#define Vector_Put(v, pos, elem) __vector_PutPtr(v, pos, &(typeof(elem)){elem}) +#define Vector_Put(v, pos, elem) __vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL) /* Push an element at the end of v, resizing it if needed. This macro wraps * __vector_PushPtr */ -#define Vector_Push(v, elem) __vector_PushPtr(v, &(typeof(elem)){elem}) +#define Vector_Push(v, elem) __vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL) int __vector_PushPtr(Vector *v, void *elem);