22#include "hdrl_cat_utils_sort.h"
24#include "hdrl_types.h"
37static int cmp_index_asc(
const void *a,
const void *b);
38static int cmp_int_asc(
const void *a,
const void *b);
39static int cmp_double_asc(
const void *a,
const void *b);
40static int cmp_cpl_size_asc(
const void *a,
const void *b);
41static int cmp_hdrl_value_asc(
const void *a,
const void *b);
43static int cmp_index_des(
const void *a,
const void *b);
44static int cmp_int_des(
const void *a,
const void *b);
45static int cmp_double_des(
const void *a,
const void *b);
46static int cmp_cpl_size_des(
const void *a,
const void *b);
47static int cmp_hdrl_value_des(
const void *a,
const void *b);
49static cpl_error_code sort_and_gen_index(
double *a, cpl_size nE, sort_index *a_index, cpl_sort_direction dir);
50static cpl_error_code sort_array_using_index(sort_index *a_index, cpl_size nE,
void *b, hdrl_sort_type type);
82cpl_error_code
sort_array_f(
void *a, cpl_size nE, cpl_size sE, f_compare f)
90 return CPL_ERROR_NONE;
107cpl_error_code
sort_array(
void *a, cpl_size nE, cpl_size sE, hdrl_sort_type type, cpl_sort_direction dir)
111 if (dir == CPL_SORT_ASCENDING) {
115 case HDRL_SORT_INT : f = &cmp_int_asc;
break;
116 case HDRL_SORT_DOUBLE : f = &cmp_double_asc;
break;
117 case HDRL_SORT_CPL_SIZE : f = &cmp_cpl_size_asc;
break;
118 case HDRL_SORT_HDRL_VALUE : f = &cmp_hdrl_value_asc;
break;
119 default :
return CPL_ERROR_ILLEGAL_INPUT;
126 case HDRL_SORT_INT : f = &cmp_int_des;
break;
127 case HDRL_SORT_DOUBLE : f = &cmp_double_des;
break;
128 case HDRL_SORT_CPL_SIZE : f = &cmp_cpl_size_des;
break;
129 case HDRL_SORT_HDRL_VALUE : f = &cmp_hdrl_value_des;
break;
130 default :
return CPL_ERROR_ILLEGAL_INPUT;
153cpl_error_code
sort_array_index(
double *a, cpl_size nE,
void *b, hdrl_sort_type type, cpl_sort_direction dir)
156 sort_index *a_index = cpl_malloc(nE *
sizeof(sort_index));
159 cpl_error_code e = sort_and_gen_index(a, nE, a_index, dir);
160 if (e == CPL_ERROR_NONE) {
163 e = sort_array_using_index(a_index, nE, b, type);
188cpl_error_code
sort_arrays_index(
double *a, cpl_size nE,
void **bs, cpl_size nA, hdrl_sort_type *types, cpl_sort_direction dir)
191 sort_index *a_index = cpl_malloc(nE *
sizeof(sort_index));
194 cpl_error_code e = sort_and_gen_index(a, nE, a_index, dir);
195 for (cpl_size i = 0; i < nA && e == CPL_ERROR_NONE; i++) {
198 e = sort_array_using_index(a_index, nE, bs[i], types[i]);
222static int cmp_index_asc(
const void *a,
const void *b) {
224 const sort_index *a_d = (
const sort_index*)a;
225 const sort_index *b_d = (
const sort_index*)b;
227 return a_d->data < b_d->data ? -1 : (a_d->data > b_d->data ? 1 : 0);
243static int cmp_int_asc(
const void * a,
const void * b)
245 const int a_d = *(
const int *)a;
246 const int b_d = *(
const int *)b;
248 return a_d < b_d ? -1 : (a_d > b_d ? 1 : 0);
264static int cmp_double_asc(
const void * a,
const void * b)
266 const double a_d = *(
const double *)a;
267 const double b_d = *(
const double *)b;
269 return a_d < b_d ? -1 : (a_d > b_d ? 1 : 0);
285static int cmp_cpl_size_asc(
const void *a,
const void *b)
287 const cpl_size a_cs = *(
const cpl_size *)a;
288 const cpl_size b_cs = *(
const cpl_size *)b;
290 return a_cs < b_cs ? -1 : (a_cs > b_cs ? 1 : 0);
306static int cmp_hdrl_value_asc(
const void *a,
const void *b)
308 const hdrl_value *a_hv = (
const hdrl_value*)a;
309 const hdrl_value *b_hv = (
const hdrl_value*)b;
311 return a_hv->data < b_hv->data ? -1 : (a_hv->data > b_hv->data ? 1 : 0);
327static int cmp_index_des(
const void *a,
const void *b) {
329 const sort_index *a_d = (
const sort_index*)a;
330 const sort_index *b_d = (
const sort_index*)b;
332 return a_d->data > b_d->data ? -1 : (a_d->data < b_d->data ? 1 : 0);
348static int cmp_int_des(
const void * a,
const void * b)
350 const int a_d = *(
const int *)a;
351 const int b_d = *(
const int *)b;
353 return a_d > b_d ? -1 : (a_d < b_d ? 1 : 0);
369static int cmp_double_des(
const void * a,
const void * b)
371 const double a_d = *(
const double *)a;
372 const double b_d = *(
const double *)b;
374 return a_d > b_d ? -1 : (a_d < b_d ? 1 : 0);
390static int cmp_cpl_size_des(
const void *a,
const void *b)
392 const cpl_size a_cs = *(
const cpl_size *)a;
393 const cpl_size b_cs = *(
const cpl_size *)b;
395 return a_cs > b_cs ? -1 : (a_cs < b_cs ? 1 : 0);
411static int cmp_hdrl_value_des(
const void *a,
const void *b)
413 const hdrl_value *a_hv = (
const hdrl_value*)a;
414 const hdrl_value *b_hv = (
const hdrl_value*)b;
416 return a_hv->data > b_hv->data ? -1 : (a_hv->data < b_hv->data ? 1 : 0);
432static cpl_error_code sort_and_gen_index(
double *a, cpl_size nE, sort_index *a_index, cpl_sort_direction dir){
436 for (cpl_size i = 0; i < nE; i++) {
437 a_index[i].data = a[i];
438 a_index[i].index = i;
443 if (dir == CPL_SORT_ASCENDING) {
444 e =
sort_array_f(a_index, nE,
sizeof(*a_index), &cmp_index_asc);
446 e =
sort_array_f(a_index, nE,
sizeof(*a_index), &cmp_index_des);
450 if (e == CPL_ERROR_NONE ) {
451 for (cpl_size i = 0; i < nE; i++) {
452 a[i] = a_index[i].data;
471static cpl_error_code sort_array_using_index(sort_index *a_index, cpl_size nE,
void *b, hdrl_sort_type type)
473 if (type == HDRL_SORT_INT) {
478 int *b_cast = (
int *)b;
480 for (cpl_size i = 0; i < nE; i++) {
481 b_aux[i] = b_cast[i];
483 for (cpl_size i = 0; i < nE; i++) {
484 b_cast[i] = b_aux[a_index[i].index];
487 }
else if (type == HDRL_SORT_DOUBLE) {
492 double *b_cast = (
double *)b;
494 for (cpl_size i = 0; i < nE; i++) {
495 b_aux[i] = b_cast[i];
497 for (cpl_size i = 0; i < nE; i++) {
498 b_cast[i] = b_aux[a_index[i].index];
501 }
else if (type == HDRL_SORT_CPL_SIZE) {
506 cpl_size *b_cast = (cpl_size *)b;
508 for (cpl_size i = 0; i < nE; i++) {
509 b_aux[i] = b_cast[i];
511 for (cpl_size i = 0; i < nE; i++) {
512 b_cast[i] = b_aux[a_index[i].index];
515 }
else if (type == HDRL_SORT_HDRL_VALUE) {
520 hdrl_value *b_cast = (hdrl_value *)b;
521 hdrl_value b_aux[nE];
522 for (cpl_size i = 0; i < nE; i++) {
523 b_aux[i] = b_cast[i];
525 for (cpl_size i = 0; i < nE; i++) {
526 b_cast[i] = b_aux[a_index[i].index];
530 return CPL_ERROR_ILLEGAL_INPUT;
533 return CPL_ERROR_NONE;
cpl_error_code sort_array_f(void *a, cpl_size nE, cpl_size sE, f_compare f)
sort_array_f Core sort algorith that it's called with the other sort function. If you need to changed...
cpl_error_code sort_array(void *a, cpl_size nE, cpl_size sE, hdrl_sort_type type, cpl_sort_direction dir)
sort_array hdrl function for order arrays with know types. Using the type parameter for select the co...
cpl_error_code sort_arrays_index(double *a, cpl_size nE, void **bs, cpl_size nA, hdrl_sort_type *types, cpl_sort_direction dir)
sort_arrays_index hdrl function for sort several arrays The alghorithm sort 'a' and in the same way s...
cpl_error_code sort_array_index(double *a, cpl_size nE, void *b, hdrl_sort_type type, cpl_sort_direction dir)
sort_array_index hdrl function for sort two arrays The alghorithm sort 'a' and in the same way sort t...