From 39e1fb9e5a5b11ee3e11e3fa0329a6053a870eac Mon Sep 17 00:00:00 2001 From: Cheer Xiao Date: Tue, 8 Jan 2013 22:40:28 +0800 Subject: [PATCH] sort: Get rid of global comparer --- src/dhcpd-pools.h | 3 +-- src/sort.c | 27 +++++++++++---------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 6b175ac..0aa9210 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -223,11 +223,10 @@ int leasecomp(const void *restrict a, const void *restrict b); int rangecomp(const void *__restrict r1, const void *__restrict r2) __attribute__ ((nonnull(1, 2))); /* 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_tc(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, struct range_t *__restrict temp) __attribute__ ((nonnull(1, 3))); diff --git a/src/sort.c b/src/sort.c index 5415359..cdf3d9f 100644 --- a/src/sort.c +++ b/src/sort.c @@ -196,33 +196,27 @@ unsigned long int ret_tcperc(struct range_t r) * The sort algorithms are stabile, which means multiple sorts can be * specified and they do not mess the result of previous sort. The sort * 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) { case 'n': break; case 'i': - comparer = comp_ip; - break; + return comp_ip; case 'm': - comparer = comp_max; - break; + return comp_max; case 'c': - comparer = comp_cur; - break; + return comp_cur; case 'p': - comparer = comp_percent; - break; + return comp_percent; case 't': - comparer = comp_touched; - break; + return comp_touched; case 'T': - comparer = comp_tc; - break; + return comp_tc; case 'e': - comparer = comp_tcperc; - break; + return comp_tcperc; default: warnx("field_selector: unknown sort order `%c'", c); 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) { int i, len, ret; + comparer_t comparer; int cmp; 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 */ - field_selector(config.sort[i]); + comparer = field_selector(config.sort[i]); cmp = comparer(left, right); /* If fields are equal use next sort method */ if (cmp == 0) {