sort: Get rid of global comparer

This commit is contained in:
Cheer Xiao 2013-01-08 22:40:28 +08:00
parent 137c1d37c2
commit 39e1fb9e5a
2 changed files with 12 additions and 18 deletions

View file

@ -223,11 +223,10 @@ int leasecomp(const void *restrict a, const void *restrict b);
int rangecomp(const void *__restrict r1, const void *__restrict r2) int rangecomp(const void *__restrict r1, const void *__restrict r2)
__attribute__ ((nonnull(1, 2))); __attribute__ ((nonnull(1, 2)));
/* sort function pointer and functions */ /* sort function pointer and functions */
int (*comparer) (struct range_t *r1, struct range_t *r2); typedef int (*comparer_t) (struct range_t *r1, struct range_t *r2);
unsigned long int ret_percent(struct range_t r); unsigned long int ret_percent(struct range_t r);
unsigned long int ret_tc(struct range_t r); unsigned long int ret_tc(struct range_t r);
unsigned long int ret_tcperc(struct range_t r); unsigned long int ret_tcperc(struct range_t r);
void field_selector(char c);
void mergesort_ranges(struct range_t *__restrict orig, int size, void mergesort_ranges(struct range_t *__restrict orig, int size,
struct range_t *__restrict temp) struct range_t *__restrict temp)
__attribute__ ((nonnull(1, 3))); __attribute__ ((nonnull(1, 3)));

View file

@ -196,33 +196,27 @@ unsigned long int ret_tcperc(struct range_t r)
* The sort algorithms are stabile, which means multiple sorts can be * The sort algorithms are stabile, which means multiple sorts can be
* specified and they do not mess the result of previous sort. The sort * specified and they do not mess the result of previous sort. The sort
* algorithms are used via function pointer, that gets to be reassigned. * algorithms are used via function pointer, that gets to be reassigned.
* \return Return the selected compare function.
*/ */
void field_selector(char c) comparer_t field_selector(char c)
{ {
switch (c) { switch (c) {
case 'n': case 'n':
break; break;
case 'i': case 'i':
comparer = comp_ip; return comp_ip;
break;
case 'm': case 'm':
comparer = comp_max; return comp_max;
break;
case 'c': case 'c':
comparer = comp_cur; return comp_cur;
break;
case 'p': case 'p':
comparer = comp_percent; return comp_percent;
break;
case 't': case 't':
comparer = comp_touched; return comp_touched;
break;
case 'T': case 'T':
comparer = comp_tc; return comp_tc;
break;
case 'e': case 'e':
comparer = comp_tcperc; return comp_tcperc;
break;
default: default:
warnx("field_selector: unknown sort order `%c'", c); warnx("field_selector: unknown sort order `%c'", c);
errx(EXIT_FAILURE, "Try `%s --help' for more information.", errx(EXIT_FAILURE, "Try `%s --help' for more information.",
@ -238,6 +232,7 @@ void field_selector(char c)
static int merge(struct range_t *restrict left, struct range_t *restrict right) static int merge(struct range_t *restrict left, struct range_t *restrict right)
{ {
int i, len, ret; int i, len, ret;
comparer_t comparer;
int cmp; int cmp;
len = strlen(config.sort); len = strlen(config.sort);
@ -255,7 +250,7 @@ static int merge(struct range_t *restrict left, struct range_t *restrict right)
} }
/* Select which function is pointed by comparer */ /* Select which function is pointed by comparer */
field_selector(config.sort[i]); comparer = field_selector(config.sort[i]);
cmp = comparer(left, right); cmp = comparer(left, right);
/* If fields are equal use next sort method */ /* If fields are equal use next sort method */
if (cmp == 0) { if (cmp == 0) {