1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "dictionary.h"
36 #define _(msgid) gettext (msgid)
38 /* FIXME: Implement PRINT subcommand. */
40 /* Explains how to recode one value. `from' must be first element. */
43 union value from; /* Original value. */
44 double to; /* Recoded value. */
47 /* Explains how to recode an AUTORECODE variable. */
50 struct variable *src; /* Source variable. */
51 struct variable *dest; /* Target variable. */
52 struct hsh_table *items; /* Hash table of `freq's. */
55 /* AUTORECODE transformation. */
56 struct autorecode_trns
58 struct pool *pool; /* Contains AUTORECODE specs. */
59 struct arc_spec *specs; /* AUTORECODE specifications. */
60 size_t spec_cnt; /* Number of specifications. */
63 /* Descending or ascending sort order. */
70 /* AUTORECODE data. */
73 struct variable **src_vars; /* Source variables. */
74 char **dst_names; /* Target variable names. */
75 struct variable **dst_vars; /* Target variables. */
76 struct hsh_table **src_values; /* `union value's of source vars. */
77 size_t var_cnt; /* Number of variables. */
78 struct pool *src_values_pool; /* Pool used by src_values. */
79 enum direction direction; /* Sort order. */
80 int print; /* Print mapping table if nonzero. */
83 static trns_proc_func autorecode_trns_proc;
84 static trns_free_func autorecode_trns_free;
85 static int autorecode_proc_func (struct ccase *, void *);
86 static hsh_compare_func compare_alpha_value, compare_numeric_value;
87 static hsh_hash_func hash_alpha_value, hash_numeric_value;
89 static void recode (const struct autorecode_pgm *);
90 static void arc_free (struct autorecode_pgm *);
92 /* Performs the AUTORECODE procedure. */
96 struct autorecode_pgm arc;
101 arc.dst_names = NULL;
103 arc.src_values = NULL;
105 arc.src_values_pool = NULL;
106 arc.direction = ASCENDING;
110 lex_match_id ("VARIABLES");
112 if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
115 if (!lex_force_match_id ("INTO"))
118 if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
120 if (dst_cnt != arc.var_cnt)
124 msg (SE, _("Source variable count (%u) does not match "
125 "target variable count (%u)."),
126 (unsigned) arc.var_cnt, (unsigned) dst_cnt);
128 for (i = 0; i < dst_cnt; i++)
129 free (arc.dst_names[i]);
130 free (arc.dst_names);
131 arc.dst_names = NULL;
135 while (lex_match ('/'))
136 if (lex_match_id ("DESCENDING"))
137 arc.direction = DESCENDING;
138 else if (lex_match_id ("PRINT"))
142 lex_error (_("expecting end of command"));
146 for (i = 0; i < arc.var_cnt; i++)
150 if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
152 msg (SE, _("Target variable %s duplicates existing variable %s."),
153 arc.dst_names[i], arc.dst_names[i]);
156 for (j = 0; j < i; j++)
157 if (!strcasecmp (arc.dst_names[i], arc.dst_names[j]))
159 msg (SE, _("Duplicate variable name %s among target variables."),
165 arc.src_values_pool = pool_create ();
166 arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
167 arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
168 for (i = 0; i < dst_cnt; i++)
169 if (arc.src_vars[i]->type == ALPHA)
170 arc.src_values[i] = hsh_create (10, compare_alpha_value,
171 hash_alpha_value, NULL, arc.src_vars[i]);
173 arc.src_values[i] = hsh_create (10, compare_numeric_value,
174 hash_numeric_value, NULL, NULL);
176 procedure (autorecode_proc_func, &arc);
178 for (i = 0; i < arc.var_cnt; i++)
180 arc.dst_vars[i] = dict_create_var_assert (default_dict,
181 arc.dst_names[i], 0);
182 arc.dst_vars[i]->init = 0;
195 arc_free (struct autorecode_pgm *arc)
197 free (arc->src_vars);
198 if (arc->dst_names != NULL)
202 for (i = 0; i < arc->var_cnt; i++)
203 free (arc->dst_names[i]);
204 free (arc->dst_names);
206 free (arc->dst_vars);
207 if (arc->src_values != NULL)
211 for (i = 0; i < arc->var_cnt; i++)
212 hsh_destroy (arc->src_values[i]);
213 free (arc->src_values);
215 pool_destroy (arc->src_values_pool);
219 /* AUTORECODE transformation. */
222 recode (const struct autorecode_pgm *arc)
224 struct autorecode_trns *trns;
227 trns = pool_create_container (struct autorecode_trns, pool);
228 trns->specs = pool_nalloc (trns->pool, arc->var_cnt, sizeof *trns->specs);
229 trns->spec_cnt = arc->var_cnt;
230 for (i = 0; i < arc->var_cnt; i++)
232 struct arc_spec *spec = &trns->specs[i];
233 void *const *p = hsh_sort (arc->src_values[i]);
234 int count = hsh_count (arc->src_values[i]);
237 spec->src = arc->src_vars[i];
238 spec->dest = arc->dst_vars[i];
240 if (arc->src_vars[i]->type == ALPHA)
241 spec->items = hsh_create (2 * count, compare_alpha_value,
242 hash_alpha_value, NULL, arc->src_vars[i]);
244 spec->items = hsh_create (2 * count, compare_numeric_value,
245 hash_numeric_value, NULL, NULL);
247 for (j = 0; *p; p++, j++)
249 struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
250 union value *vp = *p;
252 if (arc->src_vars[i]->type == NUMERIC)
253 item->from.f = vp->f;
255 item->from.c = pool_strdup (trns->pool, vp->c);
256 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
257 hsh_force_insert (spec->items, item);
260 add_transformation (autorecode_trns_proc, autorecode_trns_free, trns);
264 autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
266 struct autorecode_trns *trns = trns_;
269 for (i = 0; i < trns->spec_cnt; i++)
271 struct arc_spec *spec = &trns->specs[i];
272 struct arc_item *item;
275 if (spec->src->type == NUMERIC)
276 v.f = case_num (c, spec->src->fv);
278 v.c = (char *) case_str (c, spec->src->fv);
279 item = hsh_force_find (spec->items, &v);
281 case_data_rw (c, spec->dest->fv)->f = item->to;
287 autorecode_trns_free (void *trns_)
289 struct autorecode_trns *trns = trns_;
292 for (i = 0; i < trns->spec_cnt; i++)
293 hsh_destroy (trns->specs[i].items);
294 pool_destroy (trns->pool);
297 /* AUTORECODE procedure. */
300 compare_alpha_value (const void *a_, const void *b_, void *v_)
302 const union value *a = a_;
303 const union value *b = b_;
304 const struct variable *v = v_;
306 return memcmp (a->c, b->c, v->width);
310 hash_alpha_value (const void *a_, void *v_)
312 const union value *a = a_;
313 const struct variable *v = v_;
315 return hsh_hash_bytes (a->c, v->width);
319 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
321 const union value *a = a_;
322 const union value *b = b_;
324 return a->f < b->f ? -1 : a->f > b->f;
328 hash_numeric_value (const void *a_, void *foo UNUSED)
330 const union value *a = a_;
332 return hsh_hash_double (a->f);
336 autorecode_proc_func (struct ccase *c, void *arc_)
338 struct autorecode_pgm *arc = arc_;
341 for (i = 0; i < arc->var_cnt; i++)
343 union value v, *vp, **vpp;
345 if (arc->src_vars[i]->type == NUMERIC)
346 v.f = case_num (c, arc->src_vars[i]->fv);
348 v.c = (char *) case_str (c, arc->src_vars[i]->fv);
350 vpp = (union value **) hsh_probe (arc->src_values[i], &v);
353 vp = pool_alloc (arc->src_values_pool, sizeof *vp);
354 if (arc->src_vars[i]->type == NUMERIC)
357 vp->c = pool_clone (arc->src_values_pool,
358 v.c, arc->src_vars[i]->width);