1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <data/case.h>
23 #include <data/dictionary.h>
24 #include <data/procedure.h>
25 #include <data/transformations.h>
26 #include <data/variable.h>
27 #include <language/command.h>
28 #include <language/lexer/lexer.h>
29 #include <language/lexer/variable-parser.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/message.h>
35 #include <libpspp/pool.h>
36 #include <libpspp/str.h>
39 #define _(msgid) gettext (msgid)
41 /* FIXME: Implement PRINT subcommand. */
43 /* An AUTORECODE variable's original value. */
46 double f; /* Numeric. */
47 char *c; /* Short or long string. */
50 /* Explains how to recode one value. `from' must be first element. */
53 union arc_value from; /* Original value. */
54 double to; /* Recoded value. */
57 /* Explains how to recode an AUTORECODE variable. */
60 const struct variable *src; /* Source variable. */
61 struct variable *dest; /* Target variable. */
62 struct hsh_table *items; /* Hash table of `freq's. */
65 /* AUTORECODE transformation. */
66 struct autorecode_trns
68 struct pool *pool; /* Contains AUTORECODE specs. */
69 struct arc_spec *specs; /* AUTORECODE specifications. */
70 size_t spec_cnt; /* Number of specifications. */
73 /* Descending or ascending sort order. */
80 /* AUTORECODE data. */
83 const struct variable **src_vars; /* Source variables. */
84 char **dst_names; /* Target variable names. */
85 struct variable **dst_vars; /* Target variables. */
86 struct hsh_table **src_values; /* `union arc_value's of source vars. */
87 size_t var_cnt; /* Number of variables. */
88 struct pool *src_values_pool; /* Pool used by src_values. */
89 enum direction direction; /* Sort order. */
90 int print; /* Print mapping table if nonzero. */
93 static trns_proc_func autorecode_trns_proc;
94 static trns_free_func autorecode_trns_free;
95 static hsh_compare_func compare_alpha_value, compare_numeric_value;
96 static hsh_hash_func hash_alpha_value, hash_numeric_value;
98 static void recode (struct dataset *, const struct autorecode_pgm *);
99 static void arc_free (struct autorecode_pgm *);
101 /* Performs the AUTORECODE procedure. */
103 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
105 struct autorecode_pgm arc;
112 arc.dst_names = NULL;
114 arc.src_values = NULL;
116 arc.src_values_pool = NULL;
117 arc.direction = ASCENDING;
121 lex_match_id (lexer, "VARIABLES");
122 lex_match (lexer, '=');
123 if (!parse_variables_const (lexer, dataset_dict (ds), &arc.src_vars,
127 if (!lex_force_match_id (lexer, "INTO"))
129 lex_match (lexer, '=');
130 if (!parse_DATA_LIST_vars (lexer, &arc.dst_names, &dst_cnt, PV_NONE))
132 if (dst_cnt != arc.var_cnt)
136 msg (SE, _("Source variable count (%u) does not match "
137 "target variable count (%u)."),
138 (unsigned) arc.var_cnt, (unsigned) dst_cnt);
140 for (i = 0; i < dst_cnt; i++)
141 free (arc.dst_names[i]);
142 free (arc.dst_names);
143 arc.dst_names = NULL;
147 while (lex_match (lexer, '/'))
148 if (lex_match_id (lexer, "DESCENDING"))
149 arc.direction = DESCENDING;
150 else if (lex_match_id (lexer, "PRINT"))
152 if (lex_token (lexer) != '.')
154 lex_error (lexer, _("expecting end of command"));
158 for (i = 0; i < arc.var_cnt; i++)
162 if (dict_lookup_var (dataset_dict (ds), arc.dst_names[i]) != NULL)
164 msg (SE, _("Target variable %s duplicates existing variable %s."),
165 arc.dst_names[i], arc.dst_names[i]);
168 for (j = 0; j < i; j++)
169 if (!strcasecmp (arc.dst_names[i], arc.dst_names[j]))
171 msg (SE, _("Duplicate variable name %s among target variables."),
177 arc.src_values_pool = pool_create ();
178 arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
179 arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
180 for (i = 0; i < dst_cnt; i++)
182 /* FIXME: consolodate this hsh_create */
183 if (var_is_alpha (arc.src_vars[i]))
184 arc.src_values[i] = hsh_create (10, compare_alpha_value,
185 hash_alpha_value, NULL, arc.src_vars[i]);
187 arc.src_values[i] = hsh_create (10, compare_numeric_value,
188 hash_numeric_value, NULL, NULL);
192 while (proc_read (ds, &c))
193 for (i = 0; i < arc.var_cnt; i++)
195 union arc_value v, *vp, **vpp;
197 if (var_is_numeric (arc.src_vars[i]))
198 v.f = case_num (c, arc.src_vars[i]);
200 v.c = (char *) case_str (c, arc.src_vars[i]);
202 vpp = (union arc_value **) hsh_probe (arc.src_values[i], &v);
205 vp = pool_alloc (arc.src_values_pool, sizeof *vp);
206 if (var_is_numeric (arc.src_vars[i]))
209 vp->c = pool_clone (arc.src_values_pool,
210 v.c, var_get_width (arc.src_vars[i]));
214 ok = proc_close (ds);
216 for (i = 0; i < arc.var_cnt; i++)
217 arc.dst_vars[i] = dict_create_var_assert (dataset_dict (ds),
218 arc.dst_names[i], 0);
222 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
226 return CMD_CASCADING_FAILURE;
230 arc_free (struct autorecode_pgm *arc)
232 free (arc->src_vars);
233 if (arc->dst_names != NULL)
237 for (i = 0; i < arc->var_cnt; i++)
238 free (arc->dst_names[i]);
239 free (arc->dst_names);
241 free (arc->dst_vars);
242 if (arc->src_values != NULL)
246 for (i = 0; i < arc->var_cnt; i++)
247 hsh_destroy (arc->src_values[i]);
248 free (arc->src_values);
250 pool_destroy (arc->src_values_pool);
254 /* AUTORECODE transformation. */
257 recode (struct dataset *ds, const struct autorecode_pgm *arc)
259 struct autorecode_trns *trns;
262 trns = pool_create_container (struct autorecode_trns, pool);
263 trns->specs = pool_nalloc (trns->pool, arc->var_cnt, sizeof *trns->specs);
264 trns->spec_cnt = arc->var_cnt;
265 for (i = 0; i < arc->var_cnt; i++)
267 struct arc_spec *spec = &trns->specs[i];
268 void *const *p = hsh_sort (arc->src_values[i]);
269 int count = hsh_count (arc->src_values[i]);
272 spec->src = arc->src_vars[i];
273 spec->dest = arc->dst_vars[i];
275 if (var_is_alpha (arc->src_vars[i]))
276 spec->items = hsh_create (2 * count, compare_alpha_value,
277 hash_alpha_value, NULL, arc->src_vars[i]);
279 spec->items = hsh_create (2 * count, compare_numeric_value,
280 hash_numeric_value, NULL, NULL);
282 for (j = 0; *p; p++, j++)
284 struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
285 union arc_value *vp = *p;
287 if (var_is_numeric (arc->src_vars[i]))
288 item->from.f = vp->f;
290 item->from.c = pool_clone (trns->pool, vp->c,
291 var_get_width (arc->src_vars[i]));
292 item->to = arc->direction == ASCENDING ? j + 1 : count - j;
293 hsh_force_insert (spec->items, item);
296 add_transformation (ds,
297 autorecode_trns_proc, autorecode_trns_free, trns);
300 /* Executes an AUTORECODE transformation. */
302 autorecode_trns_proc (void *trns_, struct ccase *c, casenumber case_idx UNUSED)
304 struct autorecode_trns *trns = trns_;
307 for (i = 0; i < trns->spec_cnt; i++)
309 struct arc_spec *spec = &trns->specs[i];
310 struct arc_item *item;
313 if (var_is_numeric (spec->src))
314 v.f = case_num (c, spec->src);
316 v.c = (char *) case_str (c, spec->src);
317 item = hsh_force_find (spec->items, &v);
319 case_data_rw (c, spec->dest)->f = item->to;
321 return TRNS_CONTINUE;
324 /* Frees an AUTORECODE transformation. */
326 autorecode_trns_free (void *trns_)
328 struct autorecode_trns *trns = trns_;
331 for (i = 0; i < trns->spec_cnt; i++)
332 hsh_destroy (trns->specs[i].items);
333 pool_destroy (trns->pool);
337 /* AUTORECODE procedure. */
340 compare_alpha_value (const void *a_, const void *b_, const void *v_)
342 const union arc_value *a = a_;
343 const union arc_value *b = b_;
344 const struct variable *v = v_;
346 return memcmp (a->c, b->c, var_get_width (v));
350 hash_alpha_value (const void *a_, const void *v_)
352 const union arc_value *a = a_;
353 const struct variable *v = v_;
355 return hsh_hash_bytes (a->c, var_get_width (v));
359 compare_numeric_value (const void *a_, const void *b_, const void *aux UNUSED)
361 const union arc_value *a = a_;
362 const union arc_value *b = b_;
364 return a->f < b->f ? -1 : a->f > b->f;
368 hash_numeric_value (const void *a_, const void *aux UNUSED)
370 const union arc_value *a = a_;
372 return hsh_hash_double (a->f);