X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fautorecode.c;h=41ac1058880a3a5a167486b17894b9036836fe3d;hb=3c5121dc68726f596565894f831e4fa311c99c64;hp=a4177be8e2a18ef4975b2a1a0a208a015244cc18;hpb=b0bf9b1b0f727fafac4296a048e3f45db5936f81;p=pspp diff --git a/src/language/stats/autorecode.c b/src/language/stats/autorecode.c index a4177be8e2..41ac105888 100644 --- a/src/language/stats/autorecode.c +++ b/src/language/stats/autorecode.c @@ -22,10 +22,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -33,17 +35,23 @@ #include #include #include -#include #include "gettext.h" #define _(msgid) gettext (msgid) /* FIXME: Implement PRINT subcommand. */ +/* An AUTORECODE variable's original value. */ +union arc_value + { + double f; /* Numeric. */ + char *c; /* Short or long string. */ + }; + /* Explains how to recode one value. `from' must be first element. */ struct arc_item { - union value from; /* Original value. */ + union arc_value from; /* Original value. */ double to; /* Recoded value. */ }; @@ -76,7 +84,7 @@ struct autorecode_pgm struct variable **src_vars; /* Source variables. */ char **dst_names; /* Target variable names. */ struct variable **dst_vars; /* Target variables. */ - struct hsh_table **src_values; /* `union value's of source vars. */ + struct hsh_table **src_values; /* `union arc_value's of source vars. */ size_t var_cnt; /* Number of variables. */ struct pool *src_values_pool; /* Pool used by src_values. */ enum direction direction; /* Sort order. */ @@ -85,16 +93,16 @@ struct autorecode_pgm static trns_proc_func autorecode_trns_proc; static trns_free_func autorecode_trns_free; -static bool autorecode_proc_func (struct ccase *, void *); +static bool autorecode_proc_func (const struct ccase *, void *, const struct dataset *); static hsh_compare_func compare_alpha_value, compare_numeric_value; static hsh_hash_func hash_alpha_value, hash_numeric_value; -static void recode (const struct autorecode_pgm *); +static void recode (struct dataset *, const struct autorecode_pgm *); static void arc_free (struct autorecode_pgm *); /* Performs the AUTORECODE procedure. */ int -cmd_autorecode (void) +cmd_autorecode (struct dataset *ds) { struct autorecode_pgm arc; size_t dst_cnt; @@ -113,7 +121,7 @@ cmd_autorecode (void) lex_match_id ("VARIABLES"); lex_match ('='); - if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt, + if (!parse_variables (dataset_dict (ds), &arc.src_vars, &arc.var_cnt, PV_NO_DUPLICATE)) goto lossage; if (!lex_force_match_id ("INTO")) @@ -151,7 +159,7 @@ cmd_autorecode (void) { int j; - if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL) + if (dict_lookup_var (dataset_dict (ds), arc.dst_names[i]) != NULL) { msg (SE, _("Target variable %s duplicates existing variable %s."), arc.dst_names[i], arc.dst_names[i]); @@ -177,13 +185,13 @@ cmd_autorecode (void) arc.src_values[i] = hsh_create (10, compare_numeric_value, hash_numeric_value, NULL, NULL); - ok = procedure (autorecode_proc_func, &arc); + ok = procedure (ds, autorecode_proc_func, &arc); for (i = 0; i < arc.var_cnt; i++) - arc.dst_vars[i] = dict_create_var_assert (default_dict, + arc.dst_vars[i] = dict_create_var_assert (dataset_dict (ds), arc.dst_names[i], 0); - recode (&arc); + recode (ds, &arc); arc_free (&arc); return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE; @@ -220,7 +228,7 @@ arc_free (struct autorecode_pgm *arc) /* AUTORECODE transformation. */ static void -recode (const struct autorecode_pgm *arc) +recode (struct dataset *ds, const struct autorecode_pgm *arc) { struct autorecode_trns *trns; size_t i; @@ -248,7 +256,7 @@ recode (const struct autorecode_pgm *arc) for (j = 0; *p; p++, j++) { struct arc_item *item = pool_alloc (trns->pool, sizeof *item); - union value *vp = *p; + union arc_value *vp = *p; if (arc->src_vars[i]->type == NUMERIC) item->from.f = vp->f; @@ -259,12 +267,13 @@ recode (const struct autorecode_pgm *arc) hsh_force_insert (spec->items, item); } } - add_transformation (autorecode_trns_proc, autorecode_trns_free, trns); + add_transformation (ds, + autorecode_trns_proc, autorecode_trns_free, trns); } /* Executes an AUTORECODE transformation. */ static int -autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED) +autorecode_trns_proc (void *trns_, struct ccase *c, casenumber case_idx UNUSED) { struct autorecode_trns *trns = trns_; size_t i; @@ -273,7 +282,7 @@ autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED) { struct arc_spec *spec = &trns->specs[i]; struct arc_item *item; - union value v; + union arc_value v; if (spec->src->type == NUMERIC) v.f = case_num (c, spec->src->fv); @@ -302,57 +311,57 @@ autorecode_trns_free (void *trns_) /* AUTORECODE procedure. */ static int -compare_alpha_value (const void *a_, const void *b_, void *v_) +compare_alpha_value (const void *a_, const void *b_, const void *v_) { - const union value *a = a_; - const union value *b = b_; + const union arc_value *a = a_; + const union arc_value *b = b_; const struct variable *v = v_; return memcmp (a->c, b->c, v->width); } static unsigned -hash_alpha_value (const void *a_, void *v_) +hash_alpha_value (const void *a_, const void *v_) { - const union value *a = a_; + const union arc_value *a = a_; const struct variable *v = v_; return hsh_hash_bytes (a->c, v->width); } static int -compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED) +compare_numeric_value (const void *a_, const void *b_, const void *aux UNUSED) { - const union value *a = a_; - const union value *b = b_; + const union arc_value *a = a_; + const union arc_value *b = b_; return a->f < b->f ? -1 : a->f > b->f; } static unsigned -hash_numeric_value (const void *a_, void *foo UNUSED) +hash_numeric_value (const void *a_, const void *aux UNUSED) { - const union value *a = a_; + const union arc_value *a = a_; return hsh_hash_double (a->f); } static bool -autorecode_proc_func (struct ccase *c, void *arc_) +autorecode_proc_func (const struct ccase *c, void *arc_, const struct dataset *ds UNUSED) { struct autorecode_pgm *arc = arc_; size_t i; for (i = 0; i < arc->var_cnt; i++) { - union value v, *vp, **vpp; + union arc_value v, *vp, **vpp; if (arc->src_vars[i]->type == NUMERIC) v.f = case_num (c, arc->src_vars[i]->fv); else v.c = (char *) case_str (c, arc->src_vars[i]->fv); - vpp = (union value **) hsh_probe (arc->src_values[i], &v); + vpp = (union arc_value **) hsh_probe (arc->src_values[i], &v); if (*vpp == NULL) { vp = pool_alloc (arc->src_values_pool, sizeof *vp);