1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
21 #include "data/case.h"
22 #include "data/casereader.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/array.h"
31 #include "libpspp/i18n.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/hash-functions.h"
34 #include "libpspp/hmap.h"
35 #include "libpspp/message.h"
36 #include "libpspp/pool.h"
37 #include "libpspp/str.h"
39 #include "gl/xalloc.h"
40 #include "gl/vasnprintf.h"
43 #define _(msgid) gettext (msgid)
45 /* FIXME: Implement PRINT subcommand. */
47 /* Explains how to recode one value. */
50 struct hmap_node hmap_node; /* Element in "struct arc_spec" hash table. */
51 union value from; /* Original value. */
52 int width; /* Width of the 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 *dst; /* Target variable. */
62 struct hmap *items; /* Hash table of "struct arc_item"s. */
65 /* Descending or ascending sort order. */
72 /* AUTORECODE data. */
75 struct arc_spec *specs;
78 /* Hash table of "struct arc_item"s. */
79 struct hmap *global_items;
82 static trns_proc_func autorecode_trns_proc;
83 static trns_free_func autorecode_trns_free;
85 static int compare_arc_items (const void *, const void *, const void *aux);
86 static void arc_free (struct autorecode_pgm *);
87 static struct arc_item *find_arc_item (const struct arc_spec *, const union value *,
90 /* Performs the AUTORECODE procedure. */
92 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
94 struct autorecode_pgm *arc = NULL;
95 struct dictionary *dict = dataset_dict (ds);
97 const struct variable **src_vars = NULL;
98 char **dst_names = NULL;
102 enum arc_direction direction = ASCENDING;
105 struct casereader *input;
111 /* Create procedure. */
112 arc = xzalloc (sizeof *arc);
114 /* Parse variable lists. */
115 lex_match_id (lexer, "VARIABLES");
116 lex_match (lexer, '=');
117 if (!parse_variables_const (lexer, dict, &src_vars, &n_srcs,
120 if (!lex_force_match_id (lexer, "INTO"))
122 lex_match (lexer, '=');
123 if (!parse_DATA_LIST_vars (lexer, &dst_names, &n_dsts, PV_NO_DUPLICATE))
125 if (n_dsts != n_srcs)
127 msg (SE, _("Source variable count (%zu) does not match "
128 "target variable count (%zu)."),
133 for (i = 0; i < n_dsts; i++)
135 const char *name = dst_names[i];
137 if (dict_lookup_var (dict, name) != NULL)
139 msg (SE, _("Target variable %s duplicates existing variable %s."),
146 while (lex_match (lexer, '/'))
148 if (lex_match_id (lexer, "DESCENDING"))
149 direction = DESCENDING;
150 else if (lex_match_id (lexer, "PRINT"))
152 else if (lex_match_id (lexer, "GROUP"))
154 arc->global_items = xmalloc (sizeof (*arc->global_items));
155 hmap_init (arc->global_items);
159 if (lex_token (lexer) != '.')
161 lex_error (lexer, _("expecting end of command"));
165 arc->specs = xmalloc (n_dsts * sizeof *arc->specs);
166 arc->n_specs = n_dsts;
168 for (i = 0; i < n_dsts; i++)
170 struct arc_spec *spec = &arc->specs[i];
172 spec->src = src_vars[i];
173 if (arc->global_items)
175 spec->items = arc->global_items;
179 spec->items = xmalloc (sizeof (*spec->items));
180 hmap_init (spec->items);
185 /* Execute procedure. */
186 input = proc_open (ds);
187 for (; (c = casereader_read (input)) != NULL; case_unref (c))
188 for (i = 0; i < arc->n_specs; i++)
190 struct arc_spec *spec = &arc->specs[i];
191 int width = var_get_width (spec->src);
192 const union value *value = case_data (c, spec->src);
193 size_t hash = value_hash (value, width, 0);
194 struct arc_item *item;
196 item = find_arc_item (spec, value, hash);
199 item = xmalloc (sizeof *item);
201 value_clone (&item->from, value, width);
202 hmap_insert (spec->items, &item->hmap_node, hash);
205 ok = casereader_destroy (input);
206 ok = proc_commit (ds) && ok;
208 /* Create transformation. */
209 for (i = 0; i < arc->n_specs; i++)
211 struct arc_spec *spec = &arc->specs[i];
212 struct arc_item **items;
213 struct arc_item *item;
217 /* Create destination variable. */
218 spec->dst = dict_create_var_assert (dict, dst_names[i], 0);
220 /* Create array of pointers to items. */
221 n_items = hmap_count (spec->items);
222 items = xmalloc (n_items * sizeof *items);
224 HMAP_FOR_EACH (item, struct arc_item, hmap_node, spec->items)
227 assert (j == n_items);
229 /* Sort array by value. */
230 sort (items, n_items, sizeof *items, compare_arc_items, NULL);
232 /* Assign recoded values in sorted order. */
233 for (j = 0; j < n_items; j++)
235 const union value *from = &items[j]->from;
237 char *recoded_value = NULL;
239 const int src_width = items[j]->width;
241 value_init (&to_val, 0);
243 items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
245 to_val.f = items[j]->to;
247 /* Add value labels to the destination variable which indicate
248 the source value from whence the new value comes. */
251 const char *str = CHAR_CAST_BUG (const char*, value_str (from, src_width));
253 recoded_value = recode_string (UTF8, dict_get_encoding (dict), str, src_width);
256 recoded_value = asnprintf (NULL, &len, "%g", from->f);
258 /* Remove trailing whitespace */
259 for (c = recoded_value; *c != '\0'; c++)
266 var_add_value_label (spec->dst, &to_val, recoded_value);
267 value_destroy (&to_val, 0);
268 free (recoded_value);
274 add_transformation (ds, autorecode_trns_proc, autorecode_trns_free, arc);
276 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
279 for (i = 0; i < n_dsts; i++)
284 return CMD_CASCADING_FAILURE;
288 arc_free (struct autorecode_pgm *arc)
294 for (i = 0; i < arc->n_specs; i++)
296 struct arc_spec *spec = &arc->specs[i];
297 struct arc_item *item, *next;
299 HMAP_FOR_EACH_SAFE (item, next, struct arc_item, hmap_node,
302 value_destroy (&item->from, item->width);
303 hmap_delete (spec->items, &item->hmap_node);
308 if (arc->global_items)
310 free (arc->global_items);
314 for (i = 0; i < arc->n_specs; i++)
316 struct arc_spec *spec = &arc->specs[i];
319 hmap_destroy (spec->items);
330 static struct arc_item *
331 find_arc_item (const struct arc_spec *spec, const union value *value,
334 struct arc_item *item;
336 HMAP_FOR_EACH_WITH_HASH (item, struct arc_item, hmap_node, hash, spec->items)
337 if (value_equal (value, &item->from, var_get_width (spec->src)))
343 compare_arc_items (const void *a_, const void *b_, const void *aux UNUSED)
345 const struct arc_item *const *a = a_;
346 const struct arc_item *const *b = b_;
347 int width_a = (*a)->width;
348 int width_b = (*b)->width;
350 if ( width_a == width_b)
351 return value_compare_3way (&(*a)->from, &(*b)->from, width_a);
353 if ( width_a == 0 && width_b != 0)
356 if ( width_b == 0 && width_a != 0)
359 return buf_compare_rpad (CHAR_CAST_BUG (const char *, value_str (&(*a)->from, width_a)), width_a,
360 CHAR_CAST_BUG (const char *, value_str (&(*b)->from, width_b)), width_b);
364 autorecode_trns_proc (void *arc_, struct ccase **c,
365 casenumber case_idx UNUSED)
367 struct autorecode_pgm *arc = arc_;
370 *c = case_unshare (*c);
371 for (i = 0; i < arc->n_specs; i++)
373 const struct arc_spec *spec = &arc->specs[i];
374 int width = var_get_width (spec->src);
375 const union value *value = case_data (*c, spec->src);
376 struct arc_item *item;
378 item = find_arc_item (spec, value, value_hash (value, width, 0));
379 case_data_rw (*c, spec->dst)->f = item ? item->to : SYSMIS;
382 return TRNS_CONTINUE;
386 autorecode_trns_free (void *arc_)
388 struct autorecode_pgm *arc = arc_;