1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2012, 2013, 2014 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/>. */
22 #include "data/case.h"
23 #include "data/casereader.h"
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/transformations.h"
27 #include "data/variable.h"
28 #include "language/command.h"
29 #include "language/lexer/lexer.h"
30 #include "language/lexer/variable-parser.h"
31 #include "libpspp/array.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/hash-functions.h"
34 #include "libpspp/hmap.h"
35 #include "libpspp/i18n.h"
36 #include "libpspp/message.h"
37 #include "libpspp/pool.h"
38 #include "libpspp/str.h"
40 #include "gl/xalloc.h"
41 #include "gl/c-xvasprintf.h"
42 #include "gl/mbiter.h"
46 #define _(msgid) gettext (msgid)
48 /* FIXME: Implement PRINT subcommand. */
50 /* Explains how to recode one value. */
53 struct hmap_node hmap_node; /* Element in "struct arc_spec" hash table. */
54 union value from; /* Original value. */
55 int width; /* Width of the original value */
57 double to; /* Recoded value. */
60 /* Explains how to recode an AUTORECODE variable. */
63 int width; /* Variable width. */
64 int src_idx; /* Case index of source variable. */
65 struct variable *dst; /* Target variable. */
66 struct rec_items *items;
69 /* Descending or ascending sort order. */
78 struct hmap ht; /* Hash table of "struct arc_item"s. */
84 /* AUTORECODE data. */
87 struct arc_spec *specs;
90 /* Hash table of "struct arc_item"s. */
91 struct rec_items *global_items;
96 static trns_proc_func autorecode_trns_proc;
97 static trns_free_func autorecode_trns_free;
99 static int compare_arc_items (const void *, const void *, const void *aux);
100 static void arc_free (struct autorecode_pgm *);
101 static struct arc_item *find_arc_item (const struct arc_spec *, const union value *,
105 value_is_blank (const union value *val, int width, const struct dictionary *dict)
108 const char *str = CHAR_CAST_BUG (const char*, value_str (val, width));
109 char *text = recode_string (UTF8, dict_get_encoding (dict), str, width);
111 for (mbi_init (iter, text, width); mbi_avail (iter); mbi_advance (iter))
113 mbchar_t c = mbi_cur (iter);
115 if ( ! mb_isblank (c))
126 /* Performs the AUTORECODE procedure. */
128 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
130 struct autorecode_pgm *arc = NULL;
132 struct dictionary *dict = dataset_dict (ds);
133 const struct variable **src_vars = NULL;
134 char **dst_names = NULL;
138 enum arc_direction direction = ASCENDING;
140 struct casereader *input;
146 /* Create procedure. */
147 arc = xzalloc (sizeof *arc);
148 arc->blank_valid = true;
150 /* Parse variable lists. */
151 lex_match_id (lexer, "VARIABLES");
152 lex_match (lexer, T_EQUALS);
153 if (!parse_variables_const (lexer, dict, &src_vars, &n_srcs,
154 PV_NO_DUPLICATE | PV_NO_SCRATCH))
156 lex_match (lexer, T_SLASH);
157 if (!lex_force_match_id (lexer, "INTO"))
159 lex_match (lexer, T_EQUALS);
160 if (!parse_DATA_LIST_vars (lexer, dict, &dst_names, &n_dsts,
163 if (n_dsts != n_srcs)
165 msg (SE, _("Source variable count (%zu) does not match "
166 "target variable count (%zu)."),
171 for (i = 0; i < n_dsts; i++)
173 const char *name = dst_names[i];
175 if (dict_lookup_var (dict, name) != NULL)
177 msg (SE, _("Target variable %s duplicates existing variable %s."),
184 while (lex_match (lexer, T_SLASH))
186 if (lex_match_id (lexer, "DESCENDING"))
187 direction = DESCENDING;
188 else if (lex_match_id (lexer, "PRINT"))
190 /* Not yet implemented. */
192 else if (lex_match_id (lexer, "GROUP"))
194 arc->global_items = xmalloc (sizeof (*arc->global_items));
195 arc->global_items->refcnt = 1;
196 hmap_init (&arc->global_items->ht);
198 else if (lex_match_id (lexer, "BLANK"))
200 lex_match (lexer, T_EQUALS);
201 if (lex_match_id (lexer, "VALID"))
203 arc->blank_valid = true;
205 else if (lex_match_id (lexer, "MISSING"))
207 arc->blank_valid = false;
216 if (lex_token (lexer) != T_ENDCMD)
218 lex_error (lexer, _("expecting end of command"));
222 arc->specs = xmalloc (n_dsts * sizeof *arc->specs);
223 arc->n_specs = n_dsts;
226 for (i = 0; i < n_dsts; i++)
228 struct arc_spec *spec = &arc->specs[i];
230 spec->width = var_get_width (src_vars[i]);
231 spec->src_idx = var_get_case_index (src_vars[i]);
233 if (arc->global_items)
235 arc->global_items->refcnt++;
236 spec->items = arc->global_items;
240 spec->items = xzalloc (sizeof (*spec->items));
241 spec->items->refcnt = 1;
242 hmap_init (&spec->items->ht);
247 /* Execute procedure. */
248 input = proc_open (ds);
249 for (; (c = casereader_read (input)) != NULL; case_unref (c))
250 for (i = 0; i < arc->n_specs; i++)
252 struct arc_spec *spec = &arc->specs[i];
253 int width = spec->width;
254 const union value *value = case_data_idx (c, spec->src_idx);
255 size_t hash = value_hash (value, width, 0);
256 struct arc_item *item;
258 item = find_arc_item (spec, value, hash);
262 || val_type_from_width (spec->width) == VAL_NUMERIC
263 || ! value_is_blank (value, width, dict))
266 item = xmalloc (sizeof *item);
268 value_clone (&item->from, value, width);
269 hmap_insert (&spec->items->ht, &item->hmap_node, hash);
272 ok = casereader_destroy (input);
273 ok = proc_commit (ds) && ok;
275 /* Re-fetch dictionary because it might have changed (if TEMPORARY was in
277 dict = dataset_dict (ds);
279 /* Create transformation. */
280 for (i = 0; i < arc->n_specs; i++)
282 struct arc_spec *spec = &arc->specs[i];
283 struct arc_item **items;
284 struct arc_item *item;
288 /* Create destination variable. */
289 spec->dst = dict_create_var_assert (dict, dst_names[i], 0);
291 /* Create array of pointers to items. */
292 n_items = hmap_count (&spec->items->ht);
293 items = xmalloc (n_items * sizeof *items);
295 HMAP_FOR_EACH (item, struct arc_item, hmap_node, &spec->items->ht)
298 assert (j == n_items);
300 /* Sort array by value. */
301 sort (items, n_items, sizeof *items, compare_arc_items, NULL);
303 /* Assign recoded values in sorted order. */
304 for (j = 0; j < n_items; j++)
306 const union value *from = &items[j]->from;
307 char *recoded_value = NULL;
308 const int src_width = items[j]->width;
312 value_init (&to_val, 0);
314 items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
316 to_val.f = items[j]->to;
318 /* Add value labels to the destination variable which indicate
319 the source value from whence the new value comes. */
322 const char *str = CHAR_CAST_BUG (const char*, value_str (from, src_width));
324 recoded_value = recode_string (UTF8, dict_get_encoding (dict),
328 recoded_value = c_xasprintf ("%.*g", DBL_DIG + 1, from->f);
330 /* Remove trailing whitespace */
331 len = strlen (recoded_value);
332 while (len > 0 && recoded_value[len - 1] == ' ')
333 recoded_value[--len] = '\0';
335 var_add_value_label (spec->dst, &to_val, recoded_value);
336 value_destroy (&to_val, 0);
337 free (recoded_value);
343 add_transformation (ds, autorecode_trns_proc, autorecode_trns_free, arc);
345 for (i = 0; i < n_dsts; i++)
350 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
353 for (i = 0; i < n_dsts; i++)
358 return CMD_CASCADING_FAILURE;
362 arc_free (struct autorecode_pgm *arc)
368 for (i = 0; i < arc->n_specs; i++)
370 struct arc_spec *spec = &arc->specs[i];
371 struct arc_item *item, *next;
373 HMAP_FOR_EACH_SAFE (item, next, struct arc_item, hmap_node,
376 value_destroy (&item->from, item->width);
377 hmap_delete (&spec->items->ht, &item->hmap_node);
382 for (i = 0; i < arc->n_specs; i++)
384 struct arc_spec *spec = &arc->specs[i];
386 if (--spec->items->refcnt == 0)
388 hmap_destroy (&spec->items->ht);
393 if (arc->global_items && --arc->global_items->refcnt == 0)
395 hmap_destroy (&arc->global_items->ht);
396 free (arc->global_items);
404 static struct arc_item *
405 find_arc_item (const struct arc_spec *spec, const union value *value,
408 struct arc_item *item;
410 HMAP_FOR_EACH_WITH_HASH (item, struct arc_item, hmap_node, hash, &spec->items->ht)
411 if (value_equal (value, &item->from, spec->width))
417 compare_arc_items (const void *a_, const void *b_, const void *aux UNUSED)
419 const struct arc_item *const *a = a_;
420 const struct arc_item *const *b = b_;
421 int width_a = (*a)->width;
422 int width_b = (*b)->width;
424 if ( width_a == width_b)
425 return value_compare_3way (&(*a)->from, &(*b)->from, width_a);
427 if ( width_a == 0 && width_b != 0)
430 if ( width_b == 0 && width_a != 0)
433 return buf_compare_rpad (CHAR_CAST_BUG (const char *, value_str (&(*a)->from, width_a)), width_a,
434 CHAR_CAST_BUG (const char *, value_str (&(*b)->from, width_b)), width_b);
438 autorecode_trns_proc (void *arc_, struct ccase **c,
439 casenumber case_idx UNUSED)
441 struct autorecode_pgm *arc = arc_;
444 *c = case_unshare (*c);
445 for (i = 0; i < arc->n_specs; i++)
447 const struct arc_spec *spec = &arc->specs[i];
448 const union value *value = case_data_idx (*c, spec->src_idx);
449 unsigned int hash = value_hash (value, spec->width, 0);
450 const struct arc_item *item = find_arc_item (spec, value, hash);
452 case_data_rw (*c, spec->dst)->f = item ? item->to : SYSMIS;
455 return TRNS_CONTINUE;
459 autorecode_trns_free (void *arc_)
461 struct autorecode_pgm *arc = arc_;