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
33 /* FIXME: Implement PRINT subcommand. */
35 /* Explains how to recode one value. `from' must be first element. */
38 union value from; /* Original value. */
39 double to; /* Recoded value. */
42 /* Explains how to recode an AUTORECODE variable. */
45 struct variable *src; /* Source variable. */
46 struct variable *dest; /* Target variable. */
47 struct hsh_table *items; /* Hash table of `freq's. */
50 /* AUTORECODE transformation. */
51 struct autorecode_trns
54 struct pool *owner; /* Contains AUTORECODE specs. */
55 struct arc_spec *specs; /* AUTORECODE specifications. */
56 int spec_cnt; /* Number of specifications. */
59 /* Descending or ascending sort order. */
66 /* AUTORECODE data. */
69 struct variable **src_vars; /* Source variables. */
70 char **dst_names; /* Target variable names. */
71 struct variable **dst_vars; /* Target variables. */
72 struct hsh_table **src_values; /* `union value's of source vars. */
73 int var_cnt; /* Number of variables. */
74 struct pool *src_values_pool; /* Pool used by src_values. */
75 enum direction direction; /* Sort order. */
76 int print; /* Print mapping table if nonzero. */
79 static trns_proc_func autorecode_trns_proc;
80 static trns_free_func autorecode_trns_free;
81 static int autorecode_proc_func (struct ccase *, void *);
82 static hsh_compare_func compare_alpha_value, compare_numeric_value;
83 static hsh_hash_func hash_alpha_value, hash_numeric_value;
85 static void recode (const struct autorecode_pgm *);
86 static void arc_free (struct autorecode_pgm *);
88 /* Performs the AUTORECODE procedure. */
92 struct autorecode_pgm arc;
99 arc.src_values = NULL;
101 arc.src_values_pool = NULL;
102 arc.direction = ASCENDING;
106 lex_match_id ("VARIABLES");
108 if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
111 if (!lex_force_match_id ("INTO"))
114 if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
116 if (dst_cnt != arc.var_cnt)
120 msg (SE, _("Source variable count (%d) does not match "
121 "target variable count (%d)."), arc.var_cnt, dst_cnt);
123 for (i = 0; i < dst_cnt; i++)
124 free (arc.dst_names[i]);
125 free (arc.dst_names);
126 arc.dst_names = NULL;
130 while (lex_match ('/'))
131 if (lex_match_id ("DESCENDING"))
132 arc.direction = DESCENDING;
133 else if (lex_match_id ("PRINT"))
137 lex_error (_("expecting end of command"));
141 for (i = 0; i < arc.var_cnt; i++)
145 if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
147 msg (SE, _("Target variable %s duplicates existing variable %s."),
148 arc.dst_names[i], arc.dst_names[i]);
151 for (j = 0; j < i; j++)
152 if (!strcmp (arc.dst_names[i], arc.dst_names[j]))
154 msg (SE, _("Duplicate variable name %s among target variables."),
160 arc.src_values_pool = pool_create ();
161 arc.dst_vars = xmalloc (sizeof *arc.dst_vars * arc.var_cnt);
162 arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt);
163 for (i = 0; i < dst_cnt; i++)
164 if (arc.src_vars[i]->type == ALPHA)
165 arc.src_values[i] = hsh_create (10, compare_alpha_value,
166 hash_alpha_value, NULL, arc.src_vars[i]);
168 arc.src_values[i] = hsh_create (10, compare_numeric_value,
169 hash_numeric_value, NULL, NULL);
171 procedure (autorecode_proc_func, &arc);
173 for (i = 0; i < arc.var_cnt; i++)
175 arc.dst_vars[i] = dict_create_var_assert (default_dict,
176 arc.dst_names[i], 0);
177 arc.dst_vars[i]->init = 0;
190 arc_free (struct autorecode_pgm *arc)
192 free (arc->src_vars);
193 if (arc->dst_names != NULL)
197 for (i = 0; i < arc->var_cnt; i++)
198 free (arc->dst_names[i]);
199 free (arc->dst_names);
201 free (arc->dst_vars);
202 if (arc->src_values != NULL)
206 for (i = 0; i < arc->var_cnt; i++)
207 hsh_destroy (arc->src_values[i]);
208 free (arc->src_values);
210 pool_destroy (arc->src_values_pool);
214 /* AUTORECODE transformation. */
217 recode (const struct autorecode_pgm *arc)
219 struct autorecode_trns *t;
223 pool = pool_create ();
224 t = xmalloc (sizeof *t);
225 t->h.proc = autorecode_trns_proc;
226 t->h.free = autorecode_trns_free;
228 t->specs = pool_alloc (t->owner, sizeof *t->specs * arc->var_cnt);
229 t->spec_cnt = arc->var_cnt;
230 for (i = 0; i < arc->var_cnt; i++)
232 struct arc_spec *spec = &t->specs[i];
233 void **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 (t->owner, 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 (t->owner, vp->c);
256 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
257 hsh_force_insert (spec->items, item);
260 add_transformation (&t->h);
264 autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
267 struct autorecode_trns *t = (struct autorecode_trns *) trns;
270 for (i = 0; i < t->spec_cnt; i++)
272 struct arc_spec *spec = &t->specs[i];
273 struct arc_item *item;
275 if (spec->src->type == NUMERIC)
276 item = hsh_force_find (spec->items, &c->data[spec->src->fv].f);
280 v.c = c->data[spec->src->fv].s;
281 item = hsh_force_find (spec->items, &v);
284 c->data[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 = c->data[arc->src_vars[i]->fv].f;
351 v.c = c->data[arc->src_vars[i]->fv].s;
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);