From 565eaa0f999c76ac62c87c6f2344c0765471205b Mon Sep 17 00:00:00 2001 From: Julien Rische Date: Wed, 23 Aug 2017 01:11:30 +0200 Subject: [PATCH] always pass vectors by reference --- vec_decl.h | 24 ++++++++++++------------ vec_def.h | 60 ++++++++++++++++++++++++++++++------------------------------ 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/vec_decl.h b/vec_decl.h index 03d76e4..c39deee 100644 --- a/vec_decl.h +++ b/vec_decl.h @@ -22,31 +22,31 @@ typedef struct _typename_ { \ void _typename_##_free(_typename_* const vec) #define VECTOR_GETV_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _ctype_ _typename_##_getv(const _typename_ vec, const size_t index) + _ctype_ _typename_##_getv(const _typename_* const vec, const size_t index) #define VECTOR_GETP_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _ctype_* _typename_##_getp(const _typename_ vec, const size_t index) + _ctype_* _typename_##_getp(const _typename_* const vec, const size_t index) #define VECTOR_SET_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - void _typename_##_set(const _typename_ vec, const size_t index, _ctype_ const val) + void _typename_##_set(const _typename_* const vec, const size_t index, _ctype_ const val) #define VECTOR_PUSH_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ void _typename_##_push(_typename_* const vec, _ctype_ const val) #define VECTOR_PRINT_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - void _typename_##_print(const _typename_ vec, char const* const element_format, char const* const separator) + void _typename_##_print(const _typename_* const vec, char const* const element_format, char const* const separator) #define VECTOR_FRONT_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _ctype_ _typename_##_front(const _typename_ vec) + _ctype_ _typename_##_front(const _typename_* const vec) #define VECTOR_BACK_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _ctype_ _typename_##_back(const _typename_ vec) + _ctype_ _typename_##_back(const _typename_* const vec) #define VECTOR_DATA_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _ctype_* _typename_##_data(const _typename_ vec) + _ctype_* _typename_##_data(const _typename_* const vec) #define VECTOR_EMPTY_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - bool _typename_##_empty(const _typename_ vec) + bool _typename_##_empty(const _typename_* const vec) #define VECTOR_SHRINK_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ void _typename_##_shrink(_typename_* const vec) @@ -67,7 +67,7 @@ typedef struct _typename_ { \ void _typename_##_swap(_typename_* const vec1, _typename_* const vec2) #define VECTOR_INSERT_VECTOR_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - void _typename_##_insert_vector(_typename_* const vec, const size_t pos, const _typename_ ins_vec) + void _typename_##_insert_vector(_typename_* const vec, const size_t pos, const _typename_* const ins_vec) #define VECTOR_INSERT_ARRAY_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ void _typename_##_insert_array(_typename_* const vec, const size_t pos, _ctype_ const* const array, const size_t array_size) @@ -76,13 +76,13 @@ typedef struct _typename_ { \ void _typename_##_push_array(_typename_* const vec, _ctype_ const* const array, const size_t array_size) #define VECTOR_PUSH_VECTOR_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - void _typename_##_push_vector(_typename_* const vec, const _typename_ push_vec) + void _typename_##_push_vector(_typename_* const vec, const _typename_* const push_vec) #define VECTOR_COPY_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - _typename_ _typename_##_copy(const _typename_ vec) + _typename_ _typename_##_copy(const _typename_* const vec) #define VECTOR_EQV_FUNCTION_PROTOTYPE(_typename_, _ctype_) \ - bool _typename_##_eqv(const _typename_ vec1, const _typename_ vec2, const _typename_##_eqv_func eqv) + bool _typename_##_eqv(const _typename_* const vec1, const _typename_* const vec2, const _typename_##_eqv_func eqv) #define DECLARE_VECTOR(_typename_, _ctype_) \ DEFINE_VECTOR_TYPE(_typename_, _ctype_) \ diff --git a/vec_def.h b/vec_def.h index f712e0f..baa68a2 100644 --- a/vec_def.h +++ b/vec_def.h @@ -78,18 +78,18 @@ VECTOR_FREE_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ } \ \ VECTOR_GETV_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(index < vec.size); \ - return vec.data[index]; \ + assert(index < vec->size); \ + return vec->data[index]; \ } \ \ VECTOR_GETP_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(index < vec.size); \ - return &vec.data[index]; \ + assert(index < vec->size); \ + return &vec->data[index]; \ } \ \ VECTOR_SET_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(index < vec.size); \ - vec.data[index] = val; \ + assert(index < vec->size); \ + vec->data[index] = val; \ } \ \ VECTOR_PUSH_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ @@ -104,9 +104,9 @@ VECTOR_PUSH_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ VECTOR_PRINT_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ char const* const sep = ((separator == NULL) ? ", " : separator); \ printf("{ "); \ - for (size_t i=0; isize; i++) { \ + printf(element_format, vec->data[i]); \ + if (i != (vec->size - 1)) { \ printf("%s", sep); \ } \ } \ @@ -114,21 +114,21 @@ VECTOR_PRINT_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ } \ \ VECTOR_FRONT_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(vec.size > 0); \ - return vec.data[0]; \ + assert(vec->size > 0); \ + return vec->data[0]; \ } \ \ VECTOR_BACK_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(vec.size > 0); \ - return vec.data[vec.size - 1]; \ + assert(vec->size > 0); \ + return vec->data[vec->size - 1]; \ } \ \ VECTOR_DATA_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - return vec.data; \ + return vec->data; \ } \ \ VECTOR_EMPTY_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - return (vec.size == 0) ? true : false; \ + return (vec->size == 0) ? true : false; \ } \ \ VECTOR_SHRINK_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ @@ -184,11 +184,11 @@ VECTOR_INSERT_VECTOR_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ assert(pos < vec->size); \ assert(vec->size > 0); \ assert(vec->data != NULL); \ - assert(((ins_vec.capacity > 0) && (ins_vec.data != NULL) \ - && (((vec->data + vec->capacity - 1) < ins_vec.data) \ - || (vec->data > (ins_vec.data + ins_vec.capacity - 1)))) \ - || ((ins_vec.capacity == 0) && (ins_vec.data != NULL))); \ - _typename_##_insert_array(vec, pos, ins_vec.data, ins_vec.size); \ + assert(((ins_vec->capacity > 0) && (ins_vec->data != NULL) \ + && (((vec->data + vec->capacity - 1) < ins_vec->data) \ + || (vec->data > (ins_vec->data + ins_vec->capacity - 1)))) \ + || ((ins_vec->capacity == 0) && (ins_vec->data != NULL))); \ + _typename_##_insert_array(vec, pos, ins_vec->data, ins_vec->size); \ } \ \ VECTOR_INSERT_ARRAY_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ @@ -225,29 +225,29 @@ VECTOR_PUSH_ARRAY_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ } \ \ VECTOR_PUSH_VECTOR_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(push_vec.data != NULL); \ + assert(push_vec->data != NULL); \ assert(vec->data != NULL); \ - _typename_##_push_array(vec, push_vec.data, push_vec.size); \ + _typename_##_push_array(vec, push_vec->data, push_vec->size); \ } \ \ VECTOR_COPY_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert(vec.data != NULL); \ + assert(vec->data != NULL); \ _typename_ res; \ - res.size = vec.size; \ - res.capacity = vec.capacity; \ + res.size = vec->size; \ + res.capacity = vec->capacity; \ res.data = NULL; \ _typename_##_malloc(&res); \ - memcpy(res.data, vec.data, (vec.size * sizeof(_ctype_))); \ + memcpy(res.data, vec->data, (vec->size * sizeof(_ctype_))); \ return res; \ } \ \ VECTOR_EQV_FUNCTION_PROTOTYPE(_typename_, _ctype_) { \ - assert((vec1.data != NULL) && (vec2.data != NULL)); \ - if (vec1.size != vec2.size) { \ + assert((vec1->data != NULL) && (vec2->data != NULL)); \ + if (vec1->size != vec2->size) { \ return false; \ } \ - for(size_t i=0; isize; i++) { \ + if (!eqv(vec1->data[i], vec2->data[i])) { \ return false; \ } \ } \ -- 2.11.4.GIT