X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffrequencies.q;h=c65567e8eb0388951216ed6e210761b5094e9b5f;hb=9b4b94c6d3b5fd98bf396e23d015380b87d2d296;hp=98f5b5802586048530278c4d119358dff0677bc5;hpb=2bfc3a138f308ffb38634a92b23bdc7b62592324;p=pspp-builds.git diff --git a/src/frequencies.q b/src/frequencies.q index 98f5b580..c65567e8 100644 --- a/src/frequencies.q +++ b/src/frequencies.q @@ -24,7 +24,7 @@ */ #include -#include +#include "error.h" #include #include #include "alloc.h" @@ -37,7 +37,6 @@ #include "algorithm.h" #include "magic.h" #include "misc.h" -#include "stats.h" #include "output.h" #include "som.h" #include "str.h" @@ -160,9 +159,9 @@ static struct pool *gen_pool; /* General mode. */ static void determine_charts (void); -static void precalc (void); -static int calc (struct ccase *); -static void postcalc (void); +static void precalc (void *); +static int calc (struct ccase *, void *); +static void postcalc (void *); static void postprocess_freq_tab (struct variable *); static void dump_full (struct variable *); @@ -208,10 +207,9 @@ internal_cmd_frequencies (void) n_variables = 0; v_variables = NULL; - for (i = 0; i < default_dict.nvar; i++) - default_dict.var[i]->foo = 0; + for (i = 0; i < dict_get_var_cnt (default_dict); i++) + dict_get_var(default_dict, i)->p.frq.used = 0; - lex_match_id ("FREQUENCIES"); if (!parse_frequencies (&cmd)) return CMD_FAILURE; @@ -246,8 +244,7 @@ internal_cmd_frequencies (void) cmd.sort = FRQ_AVALUE; /* Do it! */ - update_weighting (&default_dict); - procedure (precalc, calc, postcalc); + procedure_with_splits (precalc, calc, postcalc, NULL); return CMD_SUCCESS; } @@ -356,15 +353,12 @@ determine_charts (void) /* Add data from case C to the frequency table. */ static int -calc (struct ccase *c) +calc (struct ccase *c, void *aux UNUSED) { double weight; int i; - if (default_dict.weight_index == -1) - weight = 1.0; - else - weight = c->data[default_dict.var[default_dict.weight_index]->fv].f; + weight = dict_get_case_weight (default_dict, c); for (i = 0; i < n_variables; i++) { @@ -412,7 +406,7 @@ calc (struct ccase *c) /* Prepares each variable that is the target of FREQUENCIES by setting up its hash table. */ static void -precalc (void) +precalc (void *aux UNUSED) { int i; @@ -455,7 +449,7 @@ precalc (void) /* Finishes up with the variables after frequencies have been calculated. Displays statistics, percentiles, ... */ static void -postcalc (void) +postcalc (void *aux UNUSED) { int i; @@ -518,8 +512,12 @@ get_freq_comparator (int frq_sort, int var_type) case FRQ_DFREQ | (ALPHA << 16): return compare_freq_alpha_d; default: assert (0); } + + return 0; } +/* Returns nonzero iff the value in struct freq F is non-missing + for variable V. */ static int not_missing (const void *f_, void *v_) { @@ -529,8 +527,9 @@ not_missing (const void *f_, void *v_) return !is_missing (&f->v, v); } +/* Summarizes the frequency table data for variable V. */ static void -postprocess_freq_tab (struct variable * v) +postprocess_freq_tab (struct variable *v) { hsh_compare_func *compare; struct freq_tab *ft; @@ -577,29 +576,32 @@ postprocess_freq_tab (struct variable * v) } } +/* Frees the frequency table for variable V. */ static void cleanup_freq_tab (struct variable *v) { assert (v->p.frq.tab.mode == FRQM_GENERAL); free (v->p.frq.tab.valid); + hsh_destroy (v->p.frq.tab.data); } /* Parses the VARIABLES subcommand, adding to {n_variables,v_variables}. */ static int -frq_custom_variables (struct cmd_frequencies *cmd unused) +frq_custom_variables (struct cmd_frequencies *cmd UNUSED) { int mode; - int min, max; + int min = 0, max = 0; int old_n_variables = n_variables; int i; lex_match ('='); - if (token != T_ALL && (token != T_ID || !is_varname (tokid))) + if (token != T_ALL && (token != T_ID + || dict_lookup_var (default_dict, tokid) == NULL)) return 2; - if (!parse_variables (NULL, &v_variables, &n_variables, + if (!parse_variables (default_dict, &v_variables, &n_variables, PV_APPEND | PV_NO_SCRATCH)) return 0; @@ -635,14 +637,14 @@ frq_custom_variables (struct cmd_frequencies *cmd unused) { struct variable *v = v_variables[i]; - if (v->foo != 0) + if (v->p.frq.used != 0) { msg (SE, _("Variable %s specified multiple times on VARIABLES " "subcommand."), v->name); return 0; } - v->foo = 1; /* Used simply as a marker. */ + v->p.frq.used = 1; /* Used simply as a marker. */ v->p.frq.tab.valid = v->p.frq.tab.missing = NULL; @@ -672,10 +674,11 @@ frq_custom_variables (struct cmd_frequencies *cmd unused) /* Parses the GROUPED subcommand, setting the frq.{n_grouped,grouped} fields of specified variables. */ static int -frq_custom_grouped (struct cmd_frequencies *cmd unused) +frq_custom_grouped (struct cmd_frequencies *cmd UNUSED) { lex_match ('='); - if ((token == T_ID && is_varname (tokid)) || token == T_ID) + if ((token == T_ID && dict_lookup_var (default_dict, tokid) != NULL) + || token == T_ID) for (;;) { int i; @@ -688,7 +691,8 @@ frq_custom_grouped (struct cmd_frequencies *cmd unused) int n; struct variable **v; - if (!parse_variables (NULL, &v, &n, PV_NO_DUPLICATE | PV_NUMERIC)) + if (!parse_variables (default_dict, &v, &n, + PV_NO_DUPLICATE | PV_NUMERIC)) return 0; if (lex_match ('(')) { @@ -714,12 +718,15 @@ frq_custom_grouped (struct cmd_frequencies *cmd unused) return 0; } } - else - nl = 0; + else + { + nl = 0; + dl = NULL; + } for (i = 0; i < n; i++) { - if (v[i]->foo == 0) + if (v[i]->p.frq.used == 0) msg (SE, _("Variables %s specified on GROUPED but not on " "VARIABLES."), v[i]->name); if (v[i]->p.frq.groups != NULL) @@ -734,7 +741,8 @@ frq_custom_grouped (struct cmd_frequencies *cmd unused) free (v); if (!lex_match ('/')) break; - if ((token != T_ID || !is_varname (tokid)) && token != T_ALL) + if ((token != T_ID || dict_lookup_var (default_dict, tokid) != NULL) + && token != T_ALL) { lex_put_back ('/'); break; @@ -777,7 +785,7 @@ add_percentile (double x) /* Parses the PERCENTILES subcommand, adding user-specified percentiles to the list. */ static int -frq_custom_percentiles (struct cmd_frequencies *cmd unused) +frq_custom_percentiles (struct cmd_frequencies *cmd UNUSED) { lex_match ('='); if (token != T_NUM) @@ -806,7 +814,7 @@ frq_custom_percentiles (struct cmd_frequencies *cmd unused) /* Parses the NTILES subcommand, adding the percentiles that correspond to the specified evenly-distributed ntiles. */ static int -frq_custom_ntiles (struct cmd_frequencies *cmd unused) +frq_custom_ntiles (struct cmd_frequencies *cmd UNUSED) { int i; @@ -823,7 +831,7 @@ frq_custom_ntiles (struct cmd_frequencies *cmd unused) /* Hash of numeric values. */ static unsigned -hash_value_numeric (const void *value_, void *foo unused) +hash_value_numeric (const void *value_, void *foo UNUSED) { const struct freq *value = value_; return hsh_hash_double (value->v.f); @@ -831,17 +839,17 @@ hash_value_numeric (const void *value_, void *foo unused) /* Hash of string values. */ static unsigned -hash_value_alpha (const void *value_, void *len_) +hash_value_alpha (const void *value_, void *v_) { const struct freq *value = value_; - int *len = len_; + struct variable *v = v_; - return hsh_hash_bytes (value->v.s, *len); + return hsh_hash_bytes (value->v.s, v->width); } /* Ascending numeric compare of values. */ static int -compare_value_numeric_a (const void *a_, const void *b_, void *foo unused) +compare_value_numeric_a (const void *a_, const void *b_, void *foo UNUSED) { const struct freq *a = a_; const struct freq *b = b_; @@ -867,7 +875,7 @@ compare_value_alpha_a (const void *a_, const void *b_, void *v_) /* Descending numeric compare of values. */ static int -compare_value_numeric_d (const void *a, const void *b, void *foo unused) +compare_value_numeric_d (const void *a, const void *b, void *foo UNUSED) { return -compare_value_numeric_a (a, b, foo); } @@ -882,14 +890,14 @@ compare_value_alpha_d (const void *a, const void *b, void *v) /* Ascending numeric compare of frequency; secondary key on ascending numeric value. */ static int -compare_freq_numeric_a (const void *a_, const void *b_, void *foo unused) +compare_freq_numeric_a (const void *a_, const void *b_, void *foo UNUSED) { const struct freq *a = a_; const struct freq *b = b_; - if (a->v.c > b->v.c) + if (a->c > b->c) return 1; - else if (a->v.c < b->v.c) + else if (a->c < b->c) return -1; if (a->v.f > b->v.f) @@ -909,9 +917,9 @@ compare_freq_alpha_a (const void *a_, const void *b_, void *v_) const struct freq *b = b_; const struct variable *v = v_; - if (a->v.c > b->v.c) + if (a->c > b->c) return 1; - else if (a->v.c < b->v.c) + else if (a->c < b->c) return -1; else return memcmp (a->v.s, b->v.s, v->width); @@ -920,14 +928,14 @@ compare_freq_alpha_a (const void *a_, const void *b_, void *v_) /* Descending numeric compare of frequency; secondary key on ascending numeric value. */ static int -compare_freq_numeric_d (const void *a_, const void *b_, void *foo unused) +compare_freq_numeric_d (const void *a_, const void *b_, void *foo UNUSED) { const struct freq *a = a_; const struct freq *b = b_; - if (a->v.c > b->v.c) + if (a->c > b->c) return -1; - else if (a->v.c < b->v.c) + else if (a->c < b->c) return 1; if (a->v.f > b->v.f) @@ -947,9 +955,9 @@ compare_freq_alpha_d (const void *a_, const void *b_, void *v_) const struct freq *b = b_; const struct variable *v = v_; - if (a->v.c > b->v.c) + if (a->c > b->c) return -1; - else if (a->v.c < b->v.c) + else if (a->c < b->c) return 1; else return memcmp (a->v.s, b->v.s, v->width); @@ -981,7 +989,7 @@ dump_full (struct variable * v) struct freq *f; struct tab_table *t; int r; - double cum_percent = 0.0; + double cum_total = 0.0; double cum_freq = 0.0; struct init @@ -1030,7 +1038,7 @@ dump_full (struct variable * v) percent = f->c / v->p.frq.tab.total_cases * 100.0; valid_percent = f->c / v->p.frq.tab.valid_cases * 100.0; - cum_percent += valid_percent; + cum_total += valid_percent; if (lab) { @@ -1043,7 +1051,7 @@ dump_full (struct variable * v) tab_float (t, 1 + lab, r, TAB_NONE, f->c, 8, 0); tab_float (t, 2 + lab, r, TAB_NONE, percent, 5, 1); tab_float (t, 3 + lab, r, TAB_NONE, valid_percent, 5, 1); - tab_float (t, 4 + lab, r, TAB_NONE, cum_percent, 5, 1); + tab_float (t, 4 + lab, r, TAB_NONE, cum_total, 5, 1); r++; } for (; f < &v->p.frq.tab.valid[n_categories]; f++) @@ -1107,7 +1115,7 @@ dump_condensed (struct variable * v) struct freq *f; struct tab_table *t; int r; - double cum_percent = 0.0; + double cum_total = 0.0; n_categories = v->p.frq.tab.n_valid + v->p.frq.tab.n_missing; t = tab_create (4, n_categories + 2, 0); @@ -1126,12 +1134,12 @@ dump_condensed (struct variable * v) double percent; percent = f->c / v->p.frq.tab.total_cases * 100.0; - cum_percent += f->c / v->p.frq.tab.valid_cases * 100.0; + cum_total += f->c / v->p.frq.tab.valid_cases * 100.0; tab_value (t, 0, r, TAB_NONE, &f->v, &v->print); tab_float (t, 1, r, TAB_NONE, f->c, 8, 0); tab_float (t, 2, r, TAB_NONE, percent, 3, 0); - tab_float (t, 3, r, TAB_NONE, cum_percent, 3, 0); + tab_float (t, 3, r, TAB_NONE, cum_total, 3, 0); r++; } for (; f < &v->p.frq.tab.valid[n_categories]; f++) @@ -1164,7 +1172,7 @@ calc_stats (struct variable * v, double d[frq_n_stats]) struct freq *f; int most_often; - double cum_percent; + double cum_total; int i = 0; double previous_value; @@ -1175,14 +1183,14 @@ calc_stats (struct variable * v, double d[frq_n_stats]) X_bar /= W; /* Calculate percentiles. */ - cum_percent = 0; + cum_total = 0; previous_value = SYSMIS; for (f = v->p.frq.tab.valid; f < v->p.frq.tab.missing; f++) { - cum_percent += f->c / v->p.frq.tab.valid_cases; + cum_total += f->c ; for (; i < n_percentiles; i++) { - if (cum_percent <= percentiles[i]) + if (cum_total / v->p.frq.tab.valid_cases < percentiles[i]) break; percentile_values[i] = previous_value;