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
34 /* FIXME: Implement PRINT subcommand. */
36 /* Explains how to recode one value. `from' must be first element. */
39 union value from; /* Original value. */
40 double to; /* Recoded value. */
43 /* Explains how to recode an AUTORECODE variable. */
46 struct variable *src; /* Source variable. */
47 struct variable *dest; /* Target variable. */
48 struct hsh_table *items; /* Hash table of `freq's. */
51 /* AUTORECODE transformation. */
52 struct autorecode_trns
55 struct pool *owner; /* Contains AUTORECODE specs. */
56 struct arc_spec *specs; /* AUTORECODE specifications. */
57 int spec_cnt; /* Number of specifications. */
60 /* Descending or ascending sort order. */
67 /* AUTORECODE data. */
70 struct variable **src_vars; /* Source variables. */
71 char **dst_names; /* Target variable names. */
72 struct variable **dst_vars; /* Target variables. */
73 struct hsh_table **src_values; /* `union value's of source vars. */
74 int var_cnt; /* Number of variables. */
75 struct pool *src_values_pool; /* Pool used by src_values. */
76 enum direction direction; /* Sort order. */
77 int print; /* Print mapping table if nonzero. */
80 static trns_proc_func autorecode_trns_proc;
81 static trns_free_func autorecode_trns_free;
82 static int autorecode_proc_func (struct ccase *, void *);
83 static hsh_compare_func compare_alpha_value, compare_numeric_value;
84 static hsh_hash_func hash_alpha_value, hash_numeric_value;
86 static void recode (const struct autorecode_pgm *);
87 static void arc_free (struct autorecode_pgm *);
89 /* Performs the AUTORECODE procedure. */
93 struct autorecode_pgm arc;
100 arc.src_values = NULL;
102 arc.src_values_pool = NULL;
103 arc.direction = ASCENDING;
107 lex_match_id ("VARIABLES");
109 if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
112 if (!lex_force_match_id ("INTO"))
115 if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
117 if (dst_cnt != arc.var_cnt)
121 msg (SE, _("Source variable count (%d) does not match "
122 "target variable count (%d)."), arc.var_cnt, dst_cnt);
124 for (i = 0; i < dst_cnt; i++)
125 free (arc.dst_names[i]);
126 free (arc.dst_names);
127 arc.dst_names = NULL;
131 while (lex_match ('/'))
132 if (lex_match_id ("DESCENDING"))
133 arc.direction = DESCENDING;
134 else if (lex_match_id ("PRINT"))
138 lex_error (_("expecting end of command"));
142 for (i = 0; i < arc.var_cnt; i++)
146 if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
148 msg (SE, _("Target variable %s duplicates existing variable %s."),
149 arc.dst_names[i], arc.dst_names[i]);
152 for (j = 0; j < i; j++)
153 if (!strcmp (arc.dst_names[i], arc.dst_names[j]))
155 msg (SE, _("Duplicate variable name %s among target variables."),
161 arc.src_values_pool = pool_create ();
162 arc.dst_vars = xmalloc (sizeof *arc.dst_vars * arc.var_cnt);
163 arc.src_values = xmalloc (sizeof *arc.src_values * arc.var_cnt);
164 for (i = 0; i < dst_cnt; i++)
165 if (arc.src_vars[i]->type == ALPHA)
166 arc.src_values[i] = hsh_create (10, compare_alpha_value,
167 hash_alpha_value, NULL, arc.src_vars[i]);
169 arc.src_values[i] = hsh_create (10, compare_numeric_value,
170 hash_numeric_value, NULL, NULL);
172 procedure (autorecode_proc_func, &arc);
174 for (i = 0; i < arc.var_cnt; i++)
176 arc.dst_vars[i] = dict_create_var_assert (default_dict,
177 arc.dst_names[i], 0);
178 arc.dst_vars[i]->init = 0;
191 arc_free (struct autorecode_pgm *arc)
193 free (arc->src_vars);
194 if (arc->dst_names != NULL)
198 for (i = 0; i < arc->var_cnt; i++)
199 free (arc->dst_names[i]);
200 free (arc->dst_names);
202 free (arc->dst_vars);
203 if (arc->src_values != NULL)
207 for (i = 0; i < arc->var_cnt; i++)
208 hsh_destroy (arc->src_values[i]);
209 free (arc->src_values);
211 pool_destroy (arc->src_values_pool);
215 /* AUTORECODE transformation. */
218 recode (const struct autorecode_pgm *arc)
220 struct autorecode_trns *t;
224 pool = pool_create ();
225 t = xmalloc (sizeof *t);
226 t->h.proc = autorecode_trns_proc;
227 t->h.free = autorecode_trns_free;
229 t->specs = pool_alloc (t->owner, sizeof *t->specs * arc->var_cnt);
230 t->spec_cnt = arc->var_cnt;
231 for (i = 0; i < arc->var_cnt; i++)
233 struct arc_spec *spec = &t->specs[i];
234 void **p = hsh_sort (arc->src_values[i]);
235 int count = hsh_count (arc->src_values[i]);
238 spec->src = arc->src_vars[i];
239 spec->dest = arc->dst_vars[i];
241 if (arc->src_vars[i]->type == ALPHA)
242 spec->items = hsh_create (2 * count, compare_alpha_value,
243 hash_alpha_value, NULL, arc->src_vars[i]);
245 spec->items = hsh_create (2 * count, compare_numeric_value,
246 hash_numeric_value, NULL, NULL);
248 for (j = 0; *p; p++, j++)
250 struct arc_item *item = pool_alloc (t->owner, sizeof *item);
251 union value *vp = *p;
253 if (arc->src_vars[i]->type == NUMERIC)
254 item->from.f = vp->f;
256 item->from.c = pool_strdup (t->owner, vp->c);
257 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
258 hsh_force_insert (spec->items, item);
261 add_transformation (&t->h);
265 autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
268 struct autorecode_trns *t = (struct autorecode_trns *) trns;
271 for (i = 0; i < t->spec_cnt; i++)
273 struct arc_spec *spec = &t->specs[i];
274 struct arc_item *item;
277 if (spec->src->type == NUMERIC)
278 v.f = case_num (c, spec->src->fv);
280 v.c = (char *) case_str (c, spec->src->fv);
281 item = hsh_force_find (spec->items, &v);
283 case_data_rw (c, spec->dest->fv)->f = item->to;
289 autorecode_trns_free (struct trns_header * trns)
291 struct autorecode_trns *t = (struct autorecode_trns *) trns;
294 for (i = 0; i < t->spec_cnt; i++)
295 hsh_destroy (t->specs[i].items);
296 pool_destroy (t->owner);
299 /* AUTORECODE procedure. */
302 compare_alpha_value (const void *a_, const void *b_, void *v_)
304 const union value *a = a_;
305 const union value *b = b_;
306 const struct variable *v = v_;
308 return memcmp (a->c, b->c, v->width);
312 hash_alpha_value (const void *a_, void *v_)
314 const union value *a = a_;
315 const struct variable *v = v_;
317 return hsh_hash_bytes (a->c, v->width);
321 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
323 const union value *a = a_;
324 const union value *b = b_;
326 return a->f < b->f ? -1 : a->f > b->f;
330 hash_numeric_value (const void *a_, void *foo UNUSED)
332 const union value *a = a_;
334 return hsh_hash_double (a->f);
338 autorecode_proc_func (struct ccase *c, void *arc_)
340 struct autorecode_pgm *arc = arc_;
343 for (i = 0; i < arc->var_cnt; i++)
345 union value v, *vp, **vpp;
347 if (arc->src_vars[i]->type == NUMERIC)
348 v.f = case_num (c, arc->src_vars[i]->fv);
350 v.c = (char *) case_str (c, arc->src_vars[i]->fv);
352 vpp = (union value **) hsh_probe (arc->src_values[i], &v);
355 vp = pool_alloc (arc->src_values_pool, sizeof (union value));
356 if (arc->src_vars[i]->type == NUMERIC)
359 vp->c = pool_strndup (arc->src_values_pool,
360 v.c, arc->src_vars[i]->width);