Add DSC versions of the _sort function to reverve the sort (DSC=descending).
This commit is contained in:
1 parent
1d8df98339
commit
fc4dba4569
2 files changed
+29
-22
No files matched your search
@@ -230,7 +230,8 @@
|
|||||||
*max_p = max; \
|
*max_p = max; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
ALGOI_SORT_DEF(name, container_t, cont_oplist, type_t, type_oplist, it_t) \
|
ALGOI_SORT_DEF(name, container_t, cont_oplist, type_t, type_oplist, it_t, +, _sort) \
|
||||||
|
ALGOI_SORT_DEF(name, container_t, cont_oplist, type_t, type_oplist, it_t, -, _sort_dsc) \
|
||||||
\
|
\
|
||||||
M_IF_METHOD(IT_REMOVE, cont_oplist)( \
|
M_IF_METHOD(IT_REMOVE, cont_oplist)( \
|
||||||
static inline void \
|
static inline void \
|
||||||
@@ -335,14 +336,14 @@
|
|||||||
, /* NO_DIV METHOD */ ) \
|
, /* NO_DIV METHOD */ ) \
|
||||||
\
|
\
|
||||||
|
|
||||||
#define ALGOI_SORT_DEF(name, container_t, cont_oplist, type_t, type_oplist, it_t) \
|
#define ALGOI_SORT_DEF(name, container_t, cont_oplist, type_t, type_oplist, it_t, order, sort_name) \
|
||||||
\
|
\
|
||||||
static inline int M_C(name,_sort_cmp)(const type_t*a,const type_t*b) {\
|
static inline int M_C3(name,sort_name,_cmp)(const type_t*a,const type_t*b) { \
|
||||||
return M_GET_CMP type_oplist(*a, *b); \
|
return order M_GET_CMP type_oplist(*a, *b); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
static inline bool \
|
static inline bool \
|
||||||
M_C(name, _sort_p)(const container_t l) \
|
M_C3(name,sort_name,_p)(const container_t l) \
|
||||||
{ \
|
{ \
|
||||||
it_t it1; \
|
it_t it1; \
|
||||||
it_t it2; \
|
it_t it2; \
|
||||||
@@ -352,7 +353,7 @@
|
|||||||
while (!M_GET_IT_END_P cont_oplist (it2)) { \
|
while (!M_GET_IT_END_P cont_oplist (it2)) { \
|
||||||
const type_t *ref1 = M_GET_IT_CREF cont_oplist (it1); \
|
const type_t *ref1 = M_GET_IT_CREF cont_oplist (it1); \
|
||||||
const type_t *ref2 = M_GET_IT_CREF cont_oplist (it2); \
|
const type_t *ref2 = M_GET_IT_CREF cont_oplist (it2); \
|
||||||
if (!(M_C(name, _sort_cmp)(ref1, ref2) <= 0)) { \
|
if (!(M_C3(name,sort_name,_cmp)(ref1, ref2) <= 0)) { \
|
||||||
return false; \
|
return false; \
|
||||||
} \
|
} \
|
||||||
M_GET_IT_SET cont_oplist (it1, it2); \
|
M_GET_IT_SET cont_oplist (it1, it2); \
|
||||||
@@ -367,14 +368,14 @@
|
|||||||
/* - a selection sort */ \
|
/* - a selection sort */ \
|
||||||
M_IF_METHOD(SORT, cont_oplist)( \
|
M_IF_METHOD(SORT, cont_oplist)( \
|
||||||
/* optimized sort for container */ \
|
/* optimized sort for container */ \
|
||||||
static inline void M_C(name, _sort)(container_t l) \
|
static inline void M_C3(name,sort_name,)(container_t l) \
|
||||||
{ \
|
{ \
|
||||||
M_GET_SORT cont_oplist(l, M_C(name, _sort_cmp)); \
|
M_GET_SORT cont_oplist(l, M_C3(name,sort_name,_cmp)); \
|
||||||
} \
|
} \
|
||||||
, \
|
, \
|
||||||
M_IF_METHOD(IT_PREVIOUS, cont_oplist)( \
|
M_IF_METHOD(IT_PREVIOUS, cont_oplist)( \
|
||||||
/* generic insertion sort */ \
|
/* generic insertion sort */ \
|
||||||
static inline void M_C(name, _sort)(container_t l) \
|
static inline void M_C3(name,sort_name,)(container_t l) \
|
||||||
{ \
|
{ \
|
||||||
it_t it1; \
|
it_t it1; \
|
||||||
it_t it2; \
|
it_t it2; \
|
||||||
@@ -388,8 +389,8 @@
|
|||||||
M_GET_IT_SET cont_oplist (it21, it1); \
|
M_GET_IT_SET cont_oplist (it21, it1); \
|
||||||
M_GET_IT_PREVIOUS cont_oplist (it2); \
|
M_GET_IT_PREVIOUS cont_oplist (it2); \
|
||||||
while (!M_GET_IT_END_P cont_oplist (it2) \
|
while (!M_GET_IT_END_P cont_oplist (it2) \
|
||||||
&& !(M_C(name, _sort_cmp)(M_GET_IT_CREF cont_oplist (it2), \
|
&& !(M_C3(name,sort_name,_cmp)(M_GET_IT_CREF cont_oplist (it2), \
|
||||||
&x)) <= 0) { \
|
&x)) <= 0) { \
|
||||||
memcpy(M_GET_IT_REF cont_oplist (it21), \
|
memcpy(M_GET_IT_REF cont_oplist (it21), \
|
||||||
M_GET_IT_CREF cont_oplist (it2), sizeof (type_t) ); \
|
M_GET_IT_CREF cont_oplist (it2), sizeof (type_t) ); \
|
||||||
M_GET_IT_SET cont_oplist (it21, it2); \
|
M_GET_IT_SET cont_oplist (it21, it2); \
|
||||||
@@ -400,7 +401,7 @@
|
|||||||
} \
|
} \
|
||||||
, \
|
, \
|
||||||
/* generic selection sort */ \
|
/* generic selection sort */ \
|
||||||
static inline void M_C(name, _sort)(container_t l) \
|
static inline void M_C3(name,sort_name,)(container_t l) \
|
||||||
{ \
|
{ \
|
||||||
it_t it1; \
|
it_t it1; \
|
||||||
it_t it2; \
|
it_t it2; \
|
||||||
@@ -413,8 +414,8 @@
|
|||||||
for(M_GET_IT_NEXT cont_oplist (it2) ; \
|
for(M_GET_IT_NEXT cont_oplist (it2) ; \
|
||||||
!M_GET_IT_END_P cont_oplist (it2); \
|
!M_GET_IT_END_P cont_oplist (it2); \
|
||||||
M_GET_IT_NEXT cont_oplist (it2)) { \
|
M_GET_IT_NEXT cont_oplist (it2)) { \
|
||||||
if (M_C(name, _sort_cmp) (M_GET_IT_CREF cont_oplist (it2), \
|
if (M_C3(name,sort_name,_cmp) (M_GET_IT_CREF cont_oplist (it2), \
|
||||||
M_GET_IT_CREF cont_oplist (it_min)) < 0) { \
|
M_GET_IT_CREF cont_oplist (it_min)) < 0) { \
|
||||||
M_GET_IT_SET cont_oplist (it_min, it2); \
|
M_GET_IT_SET cont_oplist (it_min, it2); \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
@@ -432,19 +433,19 @@
|
|||||||
/* Compute the union of two ***sorted*** containers */ \
|
/* Compute the union of two ***sorted*** containers */ \
|
||||||
M_IF_METHOD(IT_INSERT, cont_oplist)( \
|
M_IF_METHOD(IT_INSERT, cont_oplist)( \
|
||||||
static inline void \
|
static inline void \
|
||||||
M_C(name, _sort_union)(container_t dst, const container_t src) \
|
M_C3(name,sort_name,_union)(container_t dst, const container_t src) \
|
||||||
{ \
|
{ \
|
||||||
it_t itSrc; \
|
it_t itSrc; \
|
||||||
it_t itDst; \
|
it_t itDst; \
|
||||||
assert(M_C(name, _sort_p)(dst)); \
|
assert(M_C3(name,sort_name,_p)(dst)); \
|
||||||
assert(M_C(name, _sort_p)(src)); \
|
assert(M_C3(name,sort_name,_p)(src)); \
|
||||||
M_GET_IT_FIRST cont_oplist (itSrc, src); \
|
M_GET_IT_FIRST cont_oplist (itSrc, src); \
|
||||||
M_GET_IT_FIRST cont_oplist (itDst, dst); \
|
M_GET_IT_FIRST cont_oplist (itDst, dst); \
|
||||||
while (!M_GET_IT_END_P cont_oplist (itSrc) \
|
while (!M_GET_IT_END_P cont_oplist (itSrc) \
|
||||||
&& !M_GET_IT_END_P cont_oplist (itDst)) { \
|
&& !M_GET_IT_END_P cont_oplist (itDst)) { \
|
||||||
const type_t *objSrc = M_GET_IT_CREF cont_oplist (itSrc); \
|
const type_t *objSrc = M_GET_IT_CREF cont_oplist (itSrc); \
|
||||||
const type_t *objDst = M_GET_IT_CREF cont_oplist (itDst); \
|
const type_t *objDst = M_GET_IT_CREF cont_oplist (itDst); \
|
||||||
int cmp = M_C(name, _sort_cmp)(objDst, objSrc); \
|
int cmp = M_C3(name,sort_name,_cmp)(objDst, objSrc); \
|
||||||
if (cmp == 0) { \
|
if (cmp == 0) { \
|
||||||
M_GET_IT_NEXT cont_oplist (itSrc); \
|
M_GET_IT_NEXT cont_oplist (itSrc); \
|
||||||
M_GET_IT_NEXT cont_oplist (itDst); \
|
M_GET_IT_NEXT cont_oplist (itDst); \
|
||||||
@@ -468,12 +469,12 @@
|
|||||||
/* Compute the intersection of two ***sorted*** containers */ \
|
/* Compute the intersection of two ***sorted*** containers */ \
|
||||||
M_IF_METHOD(IT_REMOVE, cont_oplist)( \
|
M_IF_METHOD(IT_REMOVE, cont_oplist)( \
|
||||||
static inline void \
|
static inline void \
|
||||||
M_C(name, _sort_intersect)(container_t dst, const container_t src) \
|
M_C3(name,sort_name,_intersect)(container_t dst, const container_t src) \
|
||||||
{ \
|
{ \
|
||||||
it_t itSrc; \
|
it_t itSrc; \
|
||||||
it_t itDst; \
|
it_t itDst; \
|
||||||
assert(M_C(name, _sort_p)(dst)); \
|
assert(M_C3(name,sort_name,_p)(dst)); \
|
||||||
assert(M_C(name, _sort_p)(src)); \
|
assert(M_C3(name,sort_name,_p)(src)); \
|
||||||
M_GET_IT_FIRST cont_oplist (itSrc, src); \
|
M_GET_IT_FIRST cont_oplist (itSrc, src); \
|
||||||
M_GET_IT_FIRST cont_oplist (itDst, dst); \
|
M_GET_IT_FIRST cont_oplist (itDst, dst); \
|
||||||
/* TODO: Not optimized at all for array ! O(n^2) */ \
|
/* TODO: Not optimized at all for array ! O(n^2) */ \
|
||||||
@@ -481,7 +482,7 @@
|
|||||||
&& !M_GET_IT_END_P cont_oplist (itDst)) { \
|
&& !M_GET_IT_END_P cont_oplist (itDst)) { \
|
||||||
const type_t *objSrc = M_GET_IT_CREF cont_oplist (itSrc); \
|
const type_t *objSrc = M_GET_IT_CREF cont_oplist (itSrc); \
|
||||||
const type_t *objDst = M_GET_IT_CREF cont_oplist (itDst); \
|
const type_t *objDst = M_GET_IT_CREF cont_oplist (itDst); \
|
||||||
int cmp = M_C(name, _sort_cmp)(objDst, objSrc); \
|
int cmp = M_C3(name,sort_name,_cmp)(objDst, objSrc); \
|
||||||
if (cmp == 0) { \
|
if (cmp == 0) { \
|
||||||
/* Keep it */ \
|
/* Keep it */ \
|
||||||
M_GET_IT_NEXT cont_oplist (itSrc); \
|
M_GET_IT_NEXT cont_oplist (itSrc); \
|
||||||
|
|||||||
@@ -197,6 +197,12 @@ static void test_array(void)
|
|||||||
|
|
||||||
ALGO_INIT_VA(l, ARRAY_OPLIST(array_int), 1, 2, 3, 4, 5);
|
ALGO_INIT_VA(l, ARRAY_OPLIST(array_int), 1, 2, 3, 4, 5);
|
||||||
assert (array_int_size(l) == 5);
|
assert (array_int_size(l) == 5);
|
||||||
|
assert (algo_array_sort_p(l) == true);
|
||||||
|
assert (algo_array_sort_dsc_p(l) == false);
|
||||||
|
algo_array_sort_dsc(l);
|
||||||
|
assert (algo_array_sort_p(l) == false);
|
||||||
|
assert (algo_array_sort_dsc_p(l) == true);
|
||||||
|
|
||||||
array_int_clear(l);
|
array_int_clear(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user