Add function _fill, _filln, _filla, _fillan as algorithms

This commit is contained in:
Patrick Pelissier committed 2020-03-26 21:48:46 +01:00
1 parent 1fe9e4c14c
commit c3a5306266
2 files changed
+78

No files matched your search

+48
View File
@@ -259,6 +259,54 @@
} \
\
static inline void \
M_C(name, _fill) (container_t l, type_t const value) \
{ \
for M_EACH(item, l, cont_oplist) { \
M_CALL_SET(type_oplist, *item, value); \
} \
} \
\
M_IF_METHOD(PUSH, cont_oplist)( \
static inline void \
M_C(name, _filln) (container_t l, size_t n, type_t const value) \
{ \
M_CALL_CLEAN(cont_oplist, l); \
for(size_t i = 0; i < n; i++) { \
M_CALL_PUSH(cont_oplist, l, value); \
} \
} \
, /* PUSH method */ ) \
\
M_IF_METHOD(ADD, type_oplist)( \
static inline void \
M_C(name, _filla) (container_t l, type_t const value, type_t const inc) \
{ \
type_t tmp; \
M_CALL_INIT_SET(type_oplist, tmp, value); \
for M_EACH(item, l, cont_oplist) { \
M_CALL_SET(type_oplist, *item, tmp); \
M_CALL_ADD(type_oplist, tmp, tmp, inc); \
} \
M_CALL_CLEAR(type_oplist, tmp); \
} \
\
M_IF_METHOD(PUSH, cont_oplist)( \
static inline void \
M_C(name, _fillan) (container_t l, size_t n, type_t const value, type_t const inc) \
{ \
type_t tmp; \
M_CALL_INIT_SET(type_oplist, tmp, value); \
M_CALL_CLEAN(cont_oplist, l); \
for(size_t i = 0; i < n; i++) { \
M_CALL_PUSH(cont_oplist, l, tmp); \
M_CALL_ADD(type_oplist, tmp, tmp, inc); \
} \
M_CALL_CLEAR(type_oplist, tmp); \
} \
, /* PUSH method */ ) \
, /* ADD method */ ) \
\
static inline void \
M_C(name, _for_each) (container_t l, void (*f)(type_t) ) \
{ \
for M_EACH(item, l, cont_oplist) { \
+30
View File
@@ -333,6 +333,36 @@ static void test_array(void)
assert (array_int_size(arr) == 3);
assert (algo_array_sort_p(arr) == true);
assert (algo_array_sort_dsc_p(arr) == false);
algo_array_fill(arr, 1789);
assert(*array_int_get(arr, 0) == 1789);
assert(*array_int_get(arr, 1) == 1789);
assert(*array_int_get(arr, 2) == 1789);
algo_array_filla(arr, 1789, 1);
assert(*array_int_get(arr, 0) == 1789);
assert(*array_int_get(arr, 1) == 1790);
assert(*array_int_get(arr, 2) == 1791);
algo_array_filla(arr, 0, 2);
assert(*array_int_get(arr, 0) == 0);
assert(*array_int_get(arr, 1) == 2);
assert(*array_int_get(arr, 2) == 4);
algo_array_filln(arr, 5, 1742);
assert (array_int_size(arr) == 5);
assert(*array_int_get(arr, 0) == 1742);
assert(*array_int_get(arr, 1) == 1742);
assert(*array_int_get(arr, 2) == 1742);
assert(*array_int_get(arr, 3) == 1742);
assert(*array_int_get(arr, 4) == 1742);
algo_array_fillan(arr, 4, -1, 3);
assert (array_int_size(arr) == 4);
assert(*array_int_get(arr, 0) == -1);
assert(*array_int_get(arr, 1) == 2);
assert(*array_int_get(arr, 2) == 5);
assert(*array_int_get(arr, 3) == 8);
}
}