X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fdescriptives.c;h=3eb638bd79316e099752a23bd89a715ecef1850c;hb=92c09e564002d356d20fc1e2e131027ef89f6748;hp=1f7fed891edc9de5b78889130aff6ca6a426501b;hpb=9f650fc3d2946c216e6cd3c7922a8a63d0f97117;p=pspp diff --git a/src/language/stats/descriptives.c b/src/language/stats/descriptives.c index 1f7fed891e..3eb638bd79 100644 --- a/src/language/stats/descriptives.c +++ b/src/language/stats/descriptives.c @@ -16,16 +16,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -/* FIXME: Many possible optimizations. */ - #include #include #include #include -#include -#include +#include +#include #include #include #include @@ -63,7 +61,7 @@ enum dsc_missing_type calculating a Z-score. */ struct dsc_z_score { - struct variable *src_var; /* Variable on which z-score is based. */ + const struct variable *src_var; /* Variable on which z-score is based. */ struct variable *z_var; /* New z-score variable. */ double mean; /* Distribution mean. */ double std_dev; /* Distribution standard deviation. */ @@ -74,7 +72,7 @@ struct dsc_trns { struct dsc_z_score *z_scores; /* Array of Z-scores. */ int z_score_cnt; /* Number of Z-scores. */ - struct variable **vars; /* Variables for listwise missing checks. */ + const struct variable **vars; /* Variables for listwise missing checks. */ size_t var_cnt; /* Number of variables. */ enum dsc_missing_type missing_type; /* Treatment of missing values. */ enum mv_class exclude; /* Classes of missing values to exclude. */ @@ -126,7 +124,7 @@ static const struct dsc_statistic_info dsc_info[DSC_N_STATS] = /* A variable specified on DESCRIPTIVES. */ struct dsc_var { - struct variable *v; /* Variable to calculate on. */ + const struct variable *v; /* Variable to calculate on. */ char z_name[LONG_NAME_LEN + 1]; /* Name for z-score variable. */ double valid, missing; /* Valid, missing counts. */ struct moments *moments; /* Moments. */ @@ -175,14 +173,13 @@ static bool try_name (const struct dictionary *dict, struct dsc_proc *dsc, const char *name); static bool generate_z_varname (const struct dictionary *dict, struct dsc_proc *dsc, char *z_name, - const char *name, size_t *z_cnt); + const char *name, int *z_cnt); static void dump_z_table (struct dsc_proc *); static void setup_z_trns (struct dsc_proc *, struct dataset *); /* Procedure execution functions. */ -static bool calc_descriptives (const struct ccase *first, - const struct casefile *, void *dsc_, - const struct dataset *); +static void calc_descriptives (struct dsc_proc *, struct casereader *, + struct dataset *); static void display (struct dsc_proc *dsc); /* Parser and outline. */ @@ -193,13 +190,16 @@ cmd_descriptives (struct lexer *lexer, struct dataset *ds) { struct dictionary *dict = dataset_dict (ds); struct dsc_proc *dsc; - struct variable **vars = NULL; + const struct variable **vars = NULL; size_t var_cnt = 0; int save_z_scores = 0; - size_t z_cnt = 0; + int z_cnt = 0; size_t i; bool ok; + struct casegrouper *grouper; + struct casereader *group; + /* Create and initialize dsc. */ dsc = xmalloc (sizeof *dsc); dsc->vars = NULL; @@ -316,11 +316,11 @@ cmd_descriptives (struct lexer *lexer, struct dataset *ds) { int i; - if (!parse_variables (lexer, dataset_dict (ds), &vars, &var_cnt, + if (!parse_variables_const (lexer, dict, &vars, &var_cnt, PV_APPEND | PV_NO_DUPLICATE | PV_NUMERIC)) goto error; - dsc->vars = xnrealloc (dsc->vars, var_cnt, sizeof *dsc->vars); + dsc->vars = xnrealloc ((void *)dsc->vars, var_cnt, sizeof *dsc->vars); for (i = dsc->var_cnt; i < var_cnt; i++) { struct dsc_var *dv = &dsc->vars[i]; @@ -370,7 +370,7 @@ cmd_descriptives (struct lexer *lexer, struct dataset *ds) { if (save_z_scores) { - size_t gen_cnt = 0; + int gen_cnt = 0; for (i = 0; i < dsc->var_cnt; i++) if (dsc->vars[i].z_name[0] == 0) @@ -412,8 +412,12 @@ cmd_descriptives (struct lexer *lexer, struct dataset *ds) for (i = 0; i < dsc->var_cnt; i++) dsc->vars[i].moments = moments_create (dsc->max_moment); - /* Data pass. */ - ok = multipass_procedure_with_splits (ds, calc_descriptives, dsc); + /* Data pass. FIXME: error handling. */ + grouper = casegrouper_create_splits (proc_open (ds), dict); + while (casegrouper_get_next_group (grouper, &group)) + calc_descriptives (dsc, group, ds); + ok = casegrouper_destroy (grouper); + ok = proc_commit (ds) && ok; /* Z-scoring! */ if (ok && z_cnt) @@ -491,7 +495,7 @@ try_name (const struct dictionary *dict, struct dsc_proc *dsc, copies the new name into Z_NAME. On failure, returns false. */ static bool generate_z_varname (const struct dictionary *dict, struct dsc_proc *dsc, char *z_name, - const char *var_name, size_t *z_cnt) + const char *var_name, int *z_cnt) { char name[LONG_NAME_LEN + 1]; @@ -586,7 +590,7 @@ descriptives_trns_proc (void *trns_, struct ccase * c, { struct dsc_trns *t = trns_; struct dsc_z_score *z; - struct variable **vars; + const struct variable **vars; int all_sysmis = 0; if (t->missing_type == DSC_LISTWISE) @@ -688,17 +692,25 @@ static bool listwise_missing (struct dsc_proc *dsc, const struct ccase *c); /* Calculates and displays descriptive statistics for the cases in CF. */ -static bool -calc_descriptives (const struct ccase *first, - const struct casefile *cf, void *dsc_, - const struct dataset *ds) +static void +calc_descriptives (struct dsc_proc *dsc, struct casereader *group, + struct dataset *ds) { - struct dsc_proc *dsc = dsc_; - struct casereader *reader; + struct casereader *pass1, *pass2; struct ccase c; size_t i; - output_split_file_values (ds, first); + if (!casereader_peek (group, 0, &c)) + return; + output_split_file_values (ds, &c); + case_destroy (&c); + + group = casereader_create_filter_weight (group, dataset_dict (ds), + NULL, NULL); + + casereader_split (group, &pass1, &pass2); + if (dsc->max_moment <= MOMENT_MEAN) + casereader_destroy (pass2); for (i = 0; i < dsc->var_cnt; i++) { @@ -714,13 +726,9 @@ calc_descriptives (const struct ccase *first, dsc->valid = 0.; /* First pass to handle most of the work. */ - for (reader = casefile_get_reader (cf, NULL); - casereader_read (reader, &c); - case_destroy (&c)) + for (; casereader_read (pass1, &c); case_destroy (&c)) { - double weight = dict_get_case_weight (dataset_dict (ds), &c, &dsc->bad_warn); - if (weight <= 0.0) - continue; + double weight = dict_get_case_weight (dataset_dict (ds), &c, NULL); /* Check for missing values. */ if (listwise_missing (dsc, &c)) @@ -736,8 +744,7 @@ calc_descriptives (const struct ccase *first, struct dsc_var *dv = &dsc->vars[i]; double x = case_num (&c, dv->v); - if (dsc->missing_type != DSC_LISTWISE - && var_is_num_missing (dv->v, x, dsc->exclude)) + if (var_is_num_missing (dv->v, x, dsc->exclude)) { dv->missing += weight; continue; @@ -752,19 +759,15 @@ calc_descriptives (const struct ccase *first, dv->max = x; } } - casereader_destroy (reader); + if (!casereader_destroy (pass1)) + return; /* Second pass for higher-order moments. */ if (dsc->max_moment > MOMENT_MEAN) { - for (reader = casefile_get_reader (cf, NULL); - casereader_read (reader, &c); - case_destroy (&c)) + for (; casereader_read (pass2, &c); case_destroy (&c)) { - double weight = dict_get_case_weight (dataset_dict (ds), &c, - &dsc->bad_warn); - if (weight <= 0.0) - continue; + double weight = dict_get_case_weight (dataset_dict (ds), &c, NULL); /* Check for missing values. */ if (dsc->missing_type == DSC_LISTWISE && listwise_missing (dsc, &c)) @@ -775,17 +778,17 @@ calc_descriptives (const struct ccase *first, struct dsc_var *dv = &dsc->vars[i]; double x = case_num (&c, dv->v); - if (dsc->missing_type != DSC_LISTWISE - && var_is_num_missing (dv->v, x, dsc->exclude)) + if (var_is_num_missing (dv->v, x, dsc->exclude)) continue; if (dv->moments != NULL) moments_pass_two (dv->moments, x, weight); } } - casereader_destroy (reader); + if (!casereader_destroy (pass2)) + return; } - + /* Calculate results. */ for (i = 0; i < dsc->var_cnt; i++) { @@ -824,8 +827,6 @@ calc_descriptives (const struct ccase *first, /* Output results. */ display (dsc); - - return true; } /* Returns true if any of the descriptives variables in DSC's