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., 59 Temple Place - Suite 330, Boston, MA
26 #include "dictionary.h"
35 /* FIXME: Implement PRINT subcommand. */
37 /* Explains how to recode one value. `from' must be first element. */
40 union value from; /* Original value. */
41 double to; /* Recoded value. */
44 /* Explains how to recode an AUTORECODE variable. */
47 struct variable *src; /* Source variable. */
48 struct variable *dest; /* Target variable. */
49 struct hsh_table *items; /* Hash table of `freq's. */
52 /* AUTORECODE transformation. */
53 struct autorecode_trns
56 struct pool *owner; /* Contains AUTORECODE specs. */
57 struct arc_spec *specs; /* AUTORECODE specifications. */
58 int spec_cnt; /* Number of specifications. */
61 /* Descending or ascending sort order. */
68 /* AUTORECODE data. */
71 struct variable **src_vars; /* Source variables. */
72 char **dst_names; /* Target variable names. */
73 struct variable **dst_vars; /* Target variables. */
74 struct hsh_table **src_values; /* `union value's of source vars. */
75 int var_cnt; /* Number of variables. */
76 struct pool *src_values_pool; /* Pool used by src_values. */
77 enum direction direction; /* Sort order. */
78 int print; /* Print mapping table if nonzero. */
81 static trns_proc_func autorecode_trns_proc;
82 static trns_free_func autorecode_trns_free;
83 static int autorecode_proc_func (struct ccase *, void *);
84 static hsh_compare_func compare_alpha_value, compare_numeric_value;
85 static hsh_hash_func hash_alpha_value, hash_numeric_value;
87 static void recode (const struct autorecode_pgm *);
88 static void arc_free (struct autorecode_pgm *);
90 /* Performs the AUTORECODE procedure. */
94 struct autorecode_pgm arc;
101 arc.src_values = NULL;
103 arc.src_values_pool = NULL;
104 arc.direction = ASCENDING;
108 lex_match_id ("VARIABLES");
110 if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
113 if (!lex_force_match_id ("INTO"))
116 if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
118 if (dst_cnt != arc.var_cnt)
122 msg (SE, _("Source variable count (%d) does not match "
123 "target variable count (%d)."), arc.var_cnt, dst_cnt);
125 for (i = 0; i < dst_cnt; i++)
126 free (arc.dst_names[i]);
127 free (arc.dst_names);
128 arc.dst_names = NULL;
132 while (lex_match ('/'))
133 if (lex_match_id ("DESCENDING"))
134 arc.direction = DESCENDING;
135 else if (lex_match_id ("PRINT"))
139 lex_error (_("expecting end of command"));
143 for (i = 0; i < arc.var_cnt; i++)
147 if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
149 msg (SE, _("Target variable %s duplicates existing variable %s."),
150 arc.dst_names[i], arc.dst_names[i]);
153 for (j = 0; j < i; j++)
154 if (!strcmp (arc.dst_names[i], arc.dst_names[j]))
156 msg (SE, _("Duplicate variable name %s among target variables."),
162 arc.src_values_pool = pool_create ();
163 arc.dst_vars = xmalloc (sizeof *arc.dst_vars * arc.var_cnt);
164 arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt);
165 for (i = 0; i < dst_cnt; i++)
166 if (arc.src_vars[i]->type == ALPHA)
167 arc.src_values[i] = hsh_create (10, compare_alpha_value,
168 hash_alpha_value, NULL, arc.src_vars[i]);
170 arc.src_values[i] = hsh_create (10, compare_numeric_value,
171 hash_numeric_value, NULL, NULL);
173 procedure (autorecode_proc_func, &arc);
175 for (i = 0; i < arc.var_cnt; i++)
177 arc.dst_vars[i] = dict_create_var_assert (default_dict,
178 arc.dst_names[i], 0);
179 arc.dst_vars[i]->init = 0;
192 arc_free (struct autorecode_pgm *arc)
194 free (arc->src_vars);
195 if (arc->dst_names != NULL)
199 for (i = 0; i < arc->var_cnt; i++)
200 free (arc->dst_names[i]);
201 free (arc->dst_names);
203 free (arc->dst_vars);
204 if (arc->src_values != NULL)
208 for (i = 0; i < arc->var_cnt; i++)
209 hsh_destroy (arc->src_values[i]);
210 free (arc->src_values);
212 pool_destroy (arc->src_values_pool);
216 /* AUTORECODE transformation. */
219 recode (const struct autorecode_pgm *arc)
221 struct autorecode_trns *t;
225 pool = pool_create ();
226 t = xmalloc (sizeof *t);
227 t->h.proc = autorecode_trns_proc;
228 t->h.free = autorecode_trns_free;
230 t->specs = pool_alloc (t->owner, sizeof *t->specs * arc->var_cnt);
231 t->spec_cnt = arc->var_cnt;
232 for (i = 0; i < arc->var_cnt; i++)
234 struct arc_spec *spec = &t->specs[i];
235 void **p = hsh_sort (arc->src_values[i]);
236 int count = hsh_count (arc->src_values[i]);
239 spec->src = arc->src_vars[i];
240 spec->dest = arc->dst_vars[i];
242 if (arc->src_vars[i]->type == ALPHA)
243 spec->items = hsh_create (2 * count, compare_alpha_value,
244 hash_alpha_value, NULL, arc->src_vars[i]);
246 spec->items = hsh_create (2 * count, compare_numeric_value,
247 hash_numeric_value, NULL, NULL);
249 for (j = 0; *p; p++, j++)
251 struct arc_item *item = pool_alloc (t->owner, sizeof *item);
252 union value *vp = *p;
254 if (arc->src_vars[i]->type == NUMERIC)
255 item->from.f = vp->f;
257 item->from.c = pool_strdup (t->owner, vp->c);
258 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
259 hsh_force_insert (spec->items, item);
262 add_transformation (&t->h);
266 autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
269 struct autorecode_trns *t = (struct autorecode_trns *) trns;
272 for (i = 0; i < t->spec_cnt; i++)
274 struct arc_spec *spec = &t->specs[i];
275 struct arc_item *item;
278 if (spec->src->type == NUMERIC)
279 v.f = case_num (c, spec->src->fv);
281 v.c = (char *) case_str (c, spec->src->fv);
282 item = hsh_force_find (spec->items, &v);
284 case_data_rw (c, spec->dest->fv)->f = item->to;
290 autorecode_trns_free (struct trns_header * trns)
292 struct autorecode_trns *t = (struct autorecode_trns *) trns;
295 for (i = 0; i < t->spec_cnt; i++)
296 hsh_destroy (t->specs[i].items);
297 pool_destroy (t->owner);
300 /* AUTORECODE procedure. */
303 compare_alpha_value (const void *a_, const void *b_, void *v_)
305 const union value *a = a_;
306 const union value *b = b_;
307 const struct variable *v = v_;
309 return memcmp (a->c, b->c, v->width);
313 hash_alpha_value (const void *a_, void *v_)
315 const union value *a = a_;
316 const struct variable *v = v_;
318 return hsh_hash_bytes (a->c, v->width);
322 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
324 const union value *a = a_;
325 const union value *b = b_;
327 return a->f < b->f ? -1 : a->f > b->f;
331 hash_numeric_value (const void *a_, void *foo UNUSED)
333 const union value *a = a_;
335 return hsh_hash_double (a->f);
339 autorecode_proc_func (struct ccase *c, void *arc_)
341 struct autorecode_pgm *arc = arc_;
344 for (i = 0; i < arc->var_cnt; i++)
346 union value v, *vp, **vpp;
348 if (arc->src_vars[i]->type == NUMERIC)
349 v.f = case_num (c, arc->src_vars[i]->fv);
351 v.c = (char *) case_str (c, arc->src_vars[i]->fv);
353 vpp = (union value **) hsh_probe (arc->src_values[i], &v);
356 vp = pool_alloc (arc->src_values_pool, sizeof (union value));
357 if (arc->src_vars[i]->type == NUMERIC)
360 vp->c = pool_strndup (arc->src_values_pool,
361 v.c, arc->src_vars[i]->width);