From a21135a117e6e3b7e605ab6d8b6959a791f871df Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 19 Dec 2003 23:57:18 +0000 Subject: [PATCH] Fri Dec 19 15:50:45 2003 Ben Pfaff * descript.q: (display) Use sort() algorithm instead of qsort(). (compare_func) Removed. (descriptives_compare_variables) New function. --- src/ChangeLog | 6 ++++++ src/descript.q | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 886afc14..59746a79 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +Fri Dec 19 15:50:45 2003 Ben Pfaff + + * descript.q: (display) Use sort() algorithm instead of qsort(). + (compare_func) Removed. + (descriptives_compare_variables) New function. + Fri Dec 19 15:08:38 2003 Ben Pfaff Get rid of AVL trees. Hashes are more appropriate for everything diff --git a/src/descript.q b/src/descript.q index 168f29aa..748655ca 100644 --- a/src/descript.q +++ b/src/descript.q @@ -24,6 +24,7 @@ #include #include #include +#include "algorithm.h" #include "alloc.h" #include "bitvector.h" #include "command.h" @@ -756,7 +757,7 @@ postcalc (void) /* Statistical display. */ -static int compare_func (struct variable ** a, struct variable ** b); +static algo_compare_func descriptives_compare_variables; static void display (void) @@ -774,8 +775,8 @@ display (void) is zero. d_glob_miss_list is (potentially) nonzero. */ if (sortby_stat != -2) - qsort (v_variables, n_variables, sizeof (struct variable *), - (int (*)(const void *, const void *)) compare_func); + sort (v_variables, n_variables, sizeof *v_variables, + descriptives_compare_variables, &cmd); for (nc = i = 0; i < dsc_n_stats; i++) if (stats & BIT_INDEX (i)) @@ -834,29 +835,33 @@ display (void) tab_submit (t); } +/* Compares variables A and B according to the ordering specified + by CMD. */ static int -compare_func (struct variable ** a, struct variable ** b) +descriptives_compare_variables (const void *a_, const void *b_, void *cmd_) { - double temp; + struct variable *const *ap = a_; + struct variable *const *bp = b_; + struct variable *a = *ap; + struct variable *b = *bp; + struct cmd_descriptives *cmd = cmd_; - if (cmd.order == DSC_D) + int result; + + if (cmd->sortby == DSC_NAME) + result = strcmp (a->name, b->name); + else { - struct variable **t; - t = a; - a = b; - b = t; + double as = a->p.dsc.stats[sortby_stat]; + double bs = b->p.dsc.stats[sortby_stat]; + + result = as < bs ? -1 : as > bs; } - if (cmd.sortby == DSC_NAME) - return strcmp ((*a)->name, (*b)->name); - temp = ((*a)->p.dsc.stats[sortby_stat] - - (*b)->p.dsc.stats[sortby_stat]); - if (temp > 0) - return 1; - else if (temp < 0) - return -1; - else - return 0; + if (cmd->order == DSC_D) + result = -result; + + return result; } /* -- 2.30.2