[C] Made vector functions internal

This commit is contained in:
seleznevae 2018-09-01 19:42:57 +03:00
parent 985a84e57d
commit 806cfb5950
5 changed files with 194 additions and 136 deletions

View File

@ -133,6 +133,12 @@ if(FORT_CXX_BUILD)
endif(FORT_CXX_BUILD)
# ------------------------------------------------------------------------------
# Set preprocessor macros for all test builds
# ------------------------------------------------------------------------------
target_compile_definitions(${PROJECT_NAME}_test_dev PRIVATE FT_TEST_BUILD=1)
target_compile_definitions(${PROJECT_NAME}_test_cpp PRIVATE FT_TEST_BUILD=1)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE FT_TEST_BUILD=1)
# ------------------------------------------------------------------------------
# Sanitizers

View File

@ -203,26 +203,31 @@ int wsnprint_n_string(wchar_t *buf, size_t length, size_t n, const char *str);
/* #include "fort_utils.h" */ /* Commented by amalgamation script */
/*****************************************************************************
* VECTOR
* ***************************************************************************/
#define INVALID_VEC_INDEX ((size_t) -1)
extern vector_t *create_vector(size_t item_size, size_t capacity);
extern void destroy_vector(vector_t *);
extern vector_t *copy_vector(vector_t *);
FT_INTERNAL
vector_t *create_vector(size_t item_size, size_t capacity);
extern size_t vector_size(const vector_t *);
extern size_t vector_capacity(const vector_t *);
extern size_t vector_index_of(const vector_t *, const void *item);
FT_INTERNAL
void destroy_vector(vector_t *);
extern int vector_push(vector_t *, const void *item);
extern int vector_erase(vector_t *, size_t index);
extern void vector_clear(vector_t *);
extern const void *vector_at_c(const vector_t *vector, size_t index);
extern void *vector_at(vector_t *, size_t index);
extern fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos);
FT_INTERNAL
size_t vector_size(const vector_t *);
FT_INTERNAL
size_t vector_capacity(const vector_t *);
FT_INTERNAL
int vector_push(vector_t *, const void *item);
FT_INTERNAL
const void *vector_at_c(const vector_t *vector, size_t index);
FT_INTERNAL
void *vector_at(vector_t *, size_t index);
FT_INTERNAL
fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos);
#define FOR_EACH_(type, item, vector, index_name) \
@ -232,6 +237,13 @@ extern fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos
#define FOR_EACH(type, item, vector) \
FOR_EACH_(type, item, vector, UNIQUE_NAME(i))
#ifdef FT_TEST_BUILD
vector_t *copy_vector(vector_t *);
size_t vector_index_of(const vector_t *, const void *item);
int vector_erase(vector_t *, size_t index);
void vector_clear(vector_t *);
#endif
#endif /* VECTOR_H */
/********************************************************
@ -2643,7 +2655,7 @@ static int vector_reallocate_(vector_t *vector, size_t new_capacity)
}
/* ------------ Constructors & Destructors ----------------------------- */
FT_INTERNAL
vector_t *create_vector(size_t item_size, size_t capacity)
{
vector_t *vector = (vector_t *)F_MALLOC(sizeof(vector_t));
@ -2668,6 +2680,7 @@ vector_t *create_vector(size_t item_size, size_t capacity)
}
FT_INTERNAL
void destroy_vector(vector_t *vector)
{
assert(vector);
@ -2675,25 +2688,15 @@ void destroy_vector(vector_t *vector)
F_FREE(vector);
}
vector_t *copy_vector(vector_t *v)
{
if (v == NULL)
return NULL;
vector_t *new_vector = create_vector(v->m_item_size, v->m_capacity);
if (new_vector == NULL)
return NULL;
memcpy(new_vector->m_data, v->m_data, v->m_item_size * v->m_size);
new_vector->m_size = v->m_size ;
new_vector->m_item_size = v->m_item_size ;
return new_vector;
}
/*
FT_INTERNAL
*/
/* ----------- Nonmodifying functions --------------------------------- */
FT_INTERNAL
size_t vector_size(const vector_t *vector)
{
assert(vector);
@ -2701,30 +2704,15 @@ size_t vector_size(const vector_t *vector)
}
FT_INTERNAL
size_t vector_capacity(const vector_t *vector)
{
assert(vector);
return vector->m_capacity;
}
size_t vector_index_of(const vector_t *vector, const void *item)
{
assert(vector);
assert(item);
size_t i = 0;
for (i = 0; i < vector->m_size; ++i) {
void *data_pos = (char *)vector->m_data + i * vector->m_item_size;
if (memcmp(data_pos, item, vector->m_item_size) == 0) {
return i;
}
}
return INVALID_VEC_INDEX;
}
/* ----------- Modifying functions ------------------------------------- */
FT_INTERNAL
int vector_push(vector_t *vector, const void *item)
{
assert(vector);
@ -2745,26 +2733,7 @@ int vector_push(vector_t *vector, const void *item)
}
int vector_erase(vector_t *vector, size_t index)
{
assert(vector);
if (vector->m_size == 0 || index >= vector->m_size)
return FT_ERROR;
memmove((char *)vector->m_data + vector->m_item_size * index,
(char *)vector->m_data + vector->m_item_size * (index + 1),
(vector->m_size - 1 - index) * vector->m_item_size);
vector->m_size--;
return FT_SUCCESS;
}
void vector_clear(vector_t *vector)
{
vector->m_size = 0;
}
FT_INTERNAL
const void *vector_at_c(const vector_t *vector, size_t index)
{
if (index >= vector->m_size)
@ -2774,6 +2743,7 @@ const void *vector_at_c(const vector_t *vector, size_t index)
}
FT_INTERNAL
void *vector_at(vector_t *vector, size_t index)
{
if (index >= vector->m_size)
@ -2782,6 +2752,8 @@ void *vector_at(vector_t *vector, size_t index)
return (char *)vector->m_data + index * vector->m_item_size;
}
FT_INTERNAL
fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos)
{
assert(cur_vec);
@ -2836,7 +2808,59 @@ fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos)
}
#ifdef FT_TEST_BUILD
vector_t *copy_vector(vector_t *v)
{
if (v == NULL)
return NULL;
vector_t *new_vector = create_vector(v->m_item_size, v->m_capacity);
if (new_vector == NULL)
return NULL;
memcpy(new_vector->m_data, v->m_data, v->m_item_size * v->m_size);
new_vector->m_size = v->m_size ;
new_vector->m_item_size = v->m_item_size ;
return new_vector;
}
size_t vector_index_of(const vector_t *vector, const void *item)
{
assert(vector);
assert(item);
size_t i = 0;
for (i = 0; i < vector->m_size; ++i) {
void *data_pos = (char *)vector->m_data + i * vector->m_item_size;
if (memcmp(data_pos, item, vector->m_item_size) == 0) {
return i;
}
}
return INVALID_VEC_INDEX;
}
int vector_erase(vector_t *vector, size_t index)
{
assert(vector);
if (vector->m_size == 0 || index >= vector->m_size)
return FT_ERROR;
memmove((char *)vector->m_data + vector->m_item_size * index,
(char *)vector->m_data + vector->m_item_size * (index + 1),
(vector->m_size - 1 - index) * vector->m_item_size);
vector->m_size--;
return FT_SUCCESS;
}
void vector_clear(vector_t *vector)
{
vector->m_size = 0;
}
#endif
/********************************************************
End of file "vector.c"

View File

@ -26,7 +26,7 @@ static int vector_reallocate_(vector_t *vector, size_t new_capacity)
}
/* ------------ Constructors & Destructors ----------------------------- */
FT_INTERNAL
vector_t *create_vector(size_t item_size, size_t capacity)
{
vector_t *vector = (vector_t *)F_MALLOC(sizeof(vector_t));
@ -51,6 +51,7 @@ vector_t *create_vector(size_t item_size, size_t capacity)
}
FT_INTERNAL
void destroy_vector(vector_t *vector)
{
assert(vector);
@ -58,25 +59,15 @@ void destroy_vector(vector_t *vector)
F_FREE(vector);
}
vector_t *copy_vector(vector_t *v)
{
if (v == NULL)
return NULL;
vector_t *new_vector = create_vector(v->m_item_size, v->m_capacity);
if (new_vector == NULL)
return NULL;
memcpy(new_vector->m_data, v->m_data, v->m_item_size * v->m_size);
new_vector->m_size = v->m_size ;
new_vector->m_item_size = v->m_item_size ;
return new_vector;
}
/*
FT_INTERNAL
*/
/* ----------- Nonmodifying functions --------------------------------- */
FT_INTERNAL
size_t vector_size(const vector_t *vector)
{
assert(vector);
@ -84,30 +75,15 @@ size_t vector_size(const vector_t *vector)
}
FT_INTERNAL
size_t vector_capacity(const vector_t *vector)
{
assert(vector);
return vector->m_capacity;
}
size_t vector_index_of(const vector_t *vector, const void *item)
{
assert(vector);
assert(item);
size_t i = 0;
for (i = 0; i < vector->m_size; ++i) {
void *data_pos = (char *)vector->m_data + i * vector->m_item_size;
if (memcmp(data_pos, item, vector->m_item_size) == 0) {
return i;
}
}
return INVALID_VEC_INDEX;
}
/* ----------- Modifying functions ------------------------------------- */
FT_INTERNAL
int vector_push(vector_t *vector, const void *item)
{
assert(vector);
@ -128,26 +104,7 @@ int vector_push(vector_t *vector, const void *item)
}
int vector_erase(vector_t *vector, size_t index)
{
assert(vector);
if (vector->m_size == 0 || index >= vector->m_size)
return FT_ERROR;
memmove((char *)vector->m_data + vector->m_item_size * index,
(char *)vector->m_data + vector->m_item_size * (index + 1),
(vector->m_size - 1 - index) * vector->m_item_size);
vector->m_size--;
return FT_SUCCESS;
}
void vector_clear(vector_t *vector)
{
vector->m_size = 0;
}
FT_INTERNAL
const void *vector_at_c(const vector_t *vector, size_t index)
{
if (index >= vector->m_size)
@ -157,6 +114,7 @@ const void *vector_at_c(const vector_t *vector, size_t index)
}
FT_INTERNAL
void *vector_at(vector_t *vector, size_t index)
{
if (index >= vector->m_size)
@ -165,6 +123,8 @@ void *vector_at(vector_t *vector, size_t index)
return (char *)vector->m_data + index * vector->m_item_size;
}
FT_INTERNAL
fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos)
{
assert(cur_vec);
@ -219,4 +179,56 @@ fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos)
}
#ifdef FT_TEST_BUILD
vector_t *copy_vector(vector_t *v)
{
if (v == NULL)
return NULL;
vector_t *new_vector = create_vector(v->m_item_size, v->m_capacity);
if (new_vector == NULL)
return NULL;
memcpy(new_vector->m_data, v->m_data, v->m_item_size * v->m_size);
new_vector->m_size = v->m_size ;
new_vector->m_item_size = v->m_item_size ;
return new_vector;
}
size_t vector_index_of(const vector_t *vector, const void *item)
{
assert(vector);
assert(item);
size_t i = 0;
for (i = 0; i < vector->m_size; ++i) {
void *data_pos = (char *)vector->m_data + i * vector->m_item_size;
if (memcmp(data_pos, item, vector->m_item_size) == 0) {
return i;
}
}
return INVALID_VEC_INDEX;
}
int vector_erase(vector_t *vector, size_t index)
{
assert(vector);
if (vector->m_size == 0 || index >= vector->m_size)
return FT_ERROR;
memmove((char *)vector->m_data + vector->m_item_size * index,
(char *)vector->m_data + vector->m_item_size * (index + 1),
(vector->m_size - 1 - index) * vector->m_item_size);
vector->m_size--;
return FT_SUCCESS;
}
void vector_clear(vector_t *vector)
{
vector->m_size = 0;
}
#endif

View File

@ -4,26 +4,31 @@
#include "fort_utils.h"
/*****************************************************************************
* VECTOR
* ***************************************************************************/
#define INVALID_VEC_INDEX ((size_t) -1)
extern vector_t *create_vector(size_t item_size, size_t capacity);
extern void destroy_vector(vector_t *);
extern vector_t *copy_vector(vector_t *);
FT_INTERNAL
vector_t *create_vector(size_t item_size, size_t capacity);
extern size_t vector_size(const vector_t *);
extern size_t vector_capacity(const vector_t *);
extern size_t vector_index_of(const vector_t *, const void *item);
FT_INTERNAL
void destroy_vector(vector_t *);
extern int vector_push(vector_t *, const void *item);
extern int vector_erase(vector_t *, size_t index);
extern void vector_clear(vector_t *);
extern const void *vector_at_c(const vector_t *vector, size_t index);
extern void *vector_at(vector_t *, size_t index);
extern fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos);
FT_INTERNAL
size_t vector_size(const vector_t *);
FT_INTERNAL
size_t vector_capacity(const vector_t *);
FT_INTERNAL
int vector_push(vector_t *, const void *item);
FT_INTERNAL
const void *vector_at_c(const vector_t *vector, size_t index);
FT_INTERNAL
void *vector_at(vector_t *, size_t index);
FT_INTERNAL
fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos);
#define FOR_EACH_(type, item, vector, index_name) \
@ -33,4 +38,11 @@ extern fort_status_t vector_swap(vector_t *cur_vec, vector_t *mv_vec, size_t pos
#define FOR_EACH(type, item, vector) \
FOR_EACH_(type, item, vector, UNIQUE_NAME(i))
#ifdef FT_TEST_BUILD
vector_t *copy_vector(vector_t *);
size_t vector_index_of(const vector_t *, const void *item);
int vector_erase(vector_t *, size_t index);
void vector_clear(vector_t *);
#endif
#endif /* VECTOR_H */

View File

@ -45,6 +45,7 @@ void test_vector_basic(void)
assert_true(vector_capacity(vector) > init_capacity);
}
#ifdef FT_TEST_BUILD
WHEN("Checking indexes of items") {
item_t item = 6;
assert_true(vector_index_of(vector, &item) == 3);
@ -53,6 +54,7 @@ void test_vector_basic(void)
item = 25;
assert_true(vector_index_of(vector, &item) == INVALID_VEC_INDEX);
}
#endif
WHEN("Checking access to items") {
assert_true(*(item_t *)vector_at(vector, 0) == 0);
@ -60,6 +62,7 @@ void test_vector_basic(void)
assert_true((item_t *)vector_at(vector, 20) == NULL);
}
#ifdef FT_TEST_BUILD
WHEN("Erasing items") {
assert_true(vector_erase(vector, 20) != FT_SUCCESS);
@ -73,6 +76,7 @@ void test_vector_basic(void)
item = 26;
assert_true(vector_index_of(vector, &item) == 11);
}
#endif
}
WHEN("Clearing vector") {