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
59 struct pool *owner; /* Contains AUTORECODE specs. */
60 struct arc_spec *specs; /* AUTORECODE specifications. */
61 int spec_cnt; /* Number of specifications. */
64 /* Descending or ascending sort order. */
71 /* AUTORECODE data. */
74 struct variable **src_vars; /* Source variables. */
75 char **dst_names; /* Target variable names. */
76 struct variable **dst_vars; /* Target variables. */
77 struct hsh_table **src_values; /* `union value's of source vars. */
78 int var_cnt; /* Number of variables. */
79 struct pool *src_values_pool; /* Pool used by src_values. */
80 enum direction direction; /* Sort order. */
81 int print; /* Print mapping table if nonzero. */
84 static trns_proc_func autorecode_trns_proc;
85 static trns_free_func autorecode_trns_free;
86 static int autorecode_proc_func (struct ccase *, void *);
87 static hsh_compare_func compare_alpha_value, compare_numeric_value;
88 static hsh_hash_func hash_alpha_value, hash_numeric_value;
90 static void recode (const struct autorecode_pgm *);
91 static void arc_free (struct autorecode_pgm *);
93 /* Performs the AUTORECODE procedure. */
97 struct autorecode_pgm arc;
102 arc.dst_names = NULL;
104 arc.src_values = NULL;
106 arc.src_values_pool = NULL;
107 arc.direction = ASCENDING;
111 lex_match_id ("VARIABLES");
113 if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
116 if (!lex_force_match_id ("INTO"))
119 if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
121 if (dst_cnt != arc.var_cnt)
125 msg (SE, _("Source variable count (%d) does not match "
126 "target variable count (%d)."), arc.var_cnt, 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 = xmalloc (sizeof *arc.dst_vars * arc.var_cnt);
167 arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt);
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 *t;
228 pool = pool_create ();
229 t = xmalloc (sizeof *t);
230 t->h.proc = autorecode_trns_proc;
231 t->h.free = autorecode_trns_free;
233 t->specs = pool_alloc (t->owner, sizeof *t->specs * arc->var_cnt);
234 t->spec_cnt = arc->var_cnt;
235 for (i = 0; i < arc->var_cnt; i++)
237 struct arc_spec *spec = &t->specs[i];
238 void *const *p = hsh_sort (arc->src_values[i]);
239 int count = hsh_count (arc->src_values[i]);
242 spec->src = arc->src_vars[i];
243 spec->dest = arc->dst_vars[i];
245 if (arc->src_vars[i]->type == ALPHA)
246 spec->items = hsh_create (2 * count, compare_alpha_value,
247 hash_alpha_value, NULL, arc->src_vars[i]);
249 spec->items = hsh_create (2 * count, compare_numeric_value,
250 hash_numeric_value, NULL, NULL);
252 for (j = 0; *p; p++, j++)
254 struct arc_item *item = pool_alloc (t->owner, sizeof *item);
255 union value *vp = *p;
257 if (arc->src_vars[i]->type == NUMERIC)
258 item->from.f = vp->f;
260 item->from.c = pool_strdup (t->owner, vp->c);
261 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
262 hsh_force_insert (spec->items, item);
265 add_transformation (&t->h);
269 autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
272 struct autorecode_trns *t = (struct autorecode_trns *) trns;
275 for (i = 0; i < t->spec_cnt; i++)
277 struct arc_spec *spec = &t->specs[i];
278 struct arc_item *item;
281 if (spec->src->type == NUMERIC)
282 v.f = case_num (c, spec->src->fv);
284 v.c = (char *) case_str (c, spec->src->fv);
285 item = hsh_force_find (spec->items, &v);
287 case_data_rw (c, spec->dest->fv)->f = item->to;
293 autorecode_trns_free (struct trns_header * trns)
295 struct autorecode_trns *t = (struct autorecode_trns *) trns;
298 for (i = 0; i < t->spec_cnt; i++)
299 hsh_destroy (t->specs[i].items);
300 pool_destroy (t->owner);
303 /* AUTORECODE procedure. */
306 compare_alpha_value (const void *a_, const void *b_, void *v_)
308 const union value *a = a_;
309 const union value *b = b_;
310 const struct variable *v = v_;
312 return memcmp (a->c, b->c, v->width);
316 hash_alpha_value (const void *a_, void *v_)
318 const union value *a = a_;
319 const struct variable *v = v_;
321 return hsh_hash_bytes (a->c, v->width);
325 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
327 const union value *a = a_;
328 const union value *b = b_;
330 return a->f < b->f ? -1 : a->f > b->f;
334 hash_numeric_value (const void *a_, void *foo UNUSED)
336 const union value *a = a_;
338 return hsh_hash_double (a->f);
342 autorecode_proc_func (struct ccase *c, void *arc_)
344 struct autorecode_pgm *arc = arc_;
347 for (i = 0; i < arc->var_cnt; i++)
349 union value v, *vp, **vpp;
351 if (arc->src_vars[i]->type == NUMERIC)
352 v.f = case_num (c, arc->src_vars[i]->fv);
354 v.c = (char *) case_str (c, arc->src_vars[i]->fv);
356 vpp = (union value **) hsh_probe (arc->src_values[i], &v);
359 vp = pool_alloc (arc->src_values_pool, sizeof (union value));
360 if (arc->src_vars[i]->type == NUMERIC)
363 vp->c = pool_strndup (arc->src_values_pool,
364 v.c, arc->src_vars[i]->width);