From e193590e4ce819c028d76da11bd94836ce6ab8cf Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 9 Aug 2014 00:22:32 +0100 Subject: [PATCH] Reverted optional bounds checking, vec_at() and tests The bounds checking introduced some problems when a vector of structs was used. --- README.md | 9 --------- src/vec.h | 38 ++++++++------------------------------ test/build_test.py | 2 +- test/test_vec.c | 12 ------------ 4 files changed, 9 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 9fd9e47..96cd0b1 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,6 @@ To define a new vector type the `vec_t()` macro should be used: typedef vec_t(unsigned int) uint_vec_t; ``` -## Debug Mode -If the macro `VEC_DEBUG` is defined then all functions will do bounds checking -and abort if a check fails. - ## Functions All vector functions are macro functions. The parameter `v` in each function @@ -84,11 +80,6 @@ successful, otherwise -1 is returned and the vector remains unchanged. ### vec\_pop(v) Removes and returns the value at the end of the vector. -### vec\_at(v, idx) -Returns the value at the given index `idx`. Using `vec_at()` as opposed to -accessing the `.data` field directly has the advantage of out-of-bounds -checking when VEC_DEBUG is enabled. - ### vec\_splice(v, start, count) Removes the number of values specified by `count`, starting at the index `start`. diff --git a/src/vec.h b/src/vec.h index 8ff0ede..235da7d 100644 --- a/src/vec.h +++ b/src/vec.h @@ -14,14 +14,6 @@ #define VEC_VERSION "0.2.0" -#ifdef VEC_DEBUG - #include - #define vec_checkidx_(len, idx) assert((idx) >= 0 && (idx) < (len)) -#else - #define vec_checkidx_(len, idx) ((void) 0) -#endif - - #define vec_unpack_(v)\ (char**)&(v)->data, &(v)->length, &(v)->capacity, sizeof(*(v)->data) @@ -45,32 +37,21 @@ #define vec_pop(v)\ - ( vec_checkidx_((v)->length, 0),\ - (v)->data[--(v)->length] ) - - -#define vec_at(v, idx)\ - ( vec_checkidx_((v)->length, idx),\ - (v)->data[idx] ) + (v)->data[--(v)->length] #define vec_splice(v, start, count)\ - ( vec_checkidx_((v)->length, start),\ - vec_checkidx_((v)->length, (start) + (count) - 1),\ - vec_splice_(vec_unpack_(v), start, count),\ + ( vec_splice_(vec_unpack_(v), start, count),\ (v)->length -= (count) ) #define vec_swapsplice(v, start, count)\ - ( vec_checkidx_((v)->length, start),\ - vec_checkidx_((v)->length, (start) + (count) - 1),\ - vec_swapsplice_(vec_unpack_(v), start, count),\ + ( vec_swapsplice_(vec_unpack_(v), start, count),\ (v)->length -= (count) ) #define vec_insert(v, idx, val)\ - ( vec_checkidx_((v)->length + 1, idx),\ - vec_insert_(vec_unpack_(v), idx) ? -1 :\ + ( vec_insert_(vec_unpack_(v), idx) ? -1 :\ ((v)->data[idx] = (val)), (v)->length++, 0 ) @@ -79,9 +60,7 @@ #define vec_swap(v, idx1, idx2)\ - ( vec_checkidx_((v)->length, idx1),\ - vec_checkidx_((v)->length, idx2),\ - vec_swap_(vec_unpack_(v), idx1, idx2) ) + vec_swap_(vec_unpack_(v), idx1, idx2) #define vec_truncate(v, len)\ @@ -93,13 +72,11 @@ #define vec_first(v)\ - ( vec_checkidx_((v)->length, 0),\ - (v)->data[0] ) + (v)->data[0] #define vec_last(v)\ - ( vec_checkidx_((v)->length, 0),\ - (v)->data[(v)->length - 1] ) + (v)->data[(v)->length - 1] #define vec_reserve(v, n)\ @@ -186,6 +163,7 @@ void vec_swapsplice_(char **data, int *length, int *capacity, int memsz, void vec_swap_(char **data, int *length, int *capacity, int memsz, int idx1, int idx2); + typedef vec_t(void*) vec_void_t; typedef vec_t(char*) vec_str_t; typedef vec_t(int) vec_int_t; diff --git a/test/build_test.py b/test/build_test.py index d344ead..ff48f48 100755 --- a/test/build_test.py +++ b/test/build_test.py @@ -10,6 +10,6 @@ os.makedirs(BIN_DIR) os.system( - "gcc -Wall -Wextra -D VEC_DEBUG -I%s -o %s/test_vec.bin ../src/*.c test_vec.c" + "gcc -Wall -Wextra -I%s -o %s/test_vec.bin ../src/*.c test_vec.c" % (INCLUDE_DIR, BIN_DIR)) diff --git a/test/test_vec.c b/test/test_vec.c index 21e95fa..e8cf00c 100644 --- a/test/test_vec.c +++ b/test/test_vec.c @@ -62,18 +62,6 @@ int main(void) { vec_deinit(&v); } - { test_section("vec_at"); - vec_int_t v; - vec_init(&v); - vec_push(&v, 'a'); - vec_push(&v, 'b'); - vec_push(&v, 'c'); - test_assert(vec_at(&v, 0) == 'a'); - test_assert(vec_at(&v, 1) == 'b'); - test_assert(vec_at(&v, 2) == 'c'); - vec_deinit(&v); - } - { test_section("vec_splice"); vec_int_t v; vec_init(&v);