1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2012 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/dataset.h"
24 #include "data/dictionary.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/compiler.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/hmap.h"
34 #include "libpspp/i18n.h"
35 #include "libpspp/message.h"
36 #include "libpspp/pool.h"
37 #include "libpspp/str.h"
39 #include "gl/xalloc.h"
40 #include "gl/c-xvasprintf.h"
41 #include "gl/mbiter.h"
45 #define _(msgid) gettext (msgid)
47 /* FIXME: Implement PRINT subcommand. */
49 /* Explains how to recode one value. */
52 struct hmap_node hmap_node; /* Element in "struct arc_spec" hash table. */
53 union value from; /* Original value. */
54 int width; /* Width of the original value */
56 double to; /* Recoded value. */
59 /* Explains how to recode an AUTORECODE variable. */
62 const struct variable *src; /* Source variable. */
63 struct variable *dst; /* Target variable. */
64 struct rec_items *items;
67 /* Descending or ascending sort order. */
76 struct hmap ht; /* Hash table of "struct arc_item"s. */
82 /* AUTORECODE data. */
85 struct arc_spec *specs;
88 /* Hash table of "struct arc_item"s. */
89 struct rec_items *global_items;
92 const struct dictionary *dict;
95 static trns_proc_func autorecode_trns_proc;
96 static trns_free_func autorecode_trns_free;
98 static int compare_arc_items (const void *, const void *, const void *aux);
99 static void arc_free (struct autorecode_pgm *);
100 static struct arc_item *find_arc_item (const struct arc_spec *, const union value *,
104 value_is_blank (const union value *val, int width, const struct dictionary *dict)
107 const char *str = CHAR_CAST_BUG (const char*, value_str (val, width));
108 char *text = recode_string (UTF8, dict_get_encoding (dict), str, width);
110 for (mbi_init (iter, text, width); mbi_avail (iter); mbi_advance (iter))
112 mbchar_t c = mbi_cur (iter);
114 if ( ! mb_isblank (c))
125 /* Performs the AUTORECODE procedure. */
127 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
129 struct autorecode_pgm *arc = NULL;
131 struct dictionary *dict = dataset_dict (ds);
132 const struct variable **src_vars = NULL;
133 char **dst_names = NULL;
137 enum arc_direction direction = ASCENDING;
139 struct casereader *input;
145 /* Create procedure. */
146 arc = xzalloc (sizeof *arc);
147 arc->blank_valid = true;
148 arc->dict = dataset_dict (ds);
150 /* Parse variable lists. */
151 lex_match_id (lexer, "VARIABLES");
152 lex_match (lexer, T_EQUALS);
153 if (!parse_variables_const (lexer, arc->dict, &src_vars, &n_srcs,
156 if (!lex_force_match_id (lexer, "INTO"))
158 lex_match (lexer, T_EQUALS);
159 if (!parse_DATA_LIST_vars (lexer, arc->dict, &dst_names, &n_dsts,
162 if (n_dsts != n_srcs)
164 msg (SE, _("Source variable count (%zu) does not match "
165 "target variable count (%zu)."),
170 for (i = 0; i < n_dsts; i++)
172 const char *name = dst_names[i];
174 if (dict_lookup_var (arc->dict, name) != NULL)
176 msg (SE, _("Target variable %s duplicates existing variable %s."),
183 while (lex_match (lexer, T_SLASH))
185 if (lex_match_id (lexer, "DESCENDING"))
186 direction = DESCENDING;
187 else if (lex_match_id (lexer, "PRINT"))
189 /* Not yet implemented. */
191 else if (lex_match_id (lexer, "GROUP"))
193 arc->global_items = xmalloc (sizeof (*arc->global_items));
194 arc->global_items->refcnt = 1;
195 hmap_init (&arc->global_items->ht);
197 else if (lex_match_id (lexer, "BLANK"))
199 lex_match (lexer, T_EQUALS);
200 if (lex_match_id (lexer, "VALID"))
202 arc->blank_valid = true;
204 else if (lex_match_id (lexer, "MISSING"))
206 arc->blank_valid = false;
215 if (lex_token (lexer) != T_ENDCMD)
217 lex_error (lexer, _("expecting end of command"));
221 arc->specs = xmalloc (n_dsts * sizeof *arc->specs);
222 arc->n_specs = n_dsts;
225 for (i = 0; i < n_dsts; i++)
227 struct arc_spec *spec = &arc->specs[i];
229 spec->src = src_vars[i];
231 if (arc->global_items)
233 arc->global_items->refcnt++;
234 spec->items = arc->global_items;
238 spec->items = xzalloc (sizeof (*spec->items));
239 spec->items->refcnt = 1;
240 hmap_init (&spec->items->ht);
245 /* Execute procedure. */
246 input = proc_open (ds);
247 for (; (c = casereader_read (input)) != NULL; case_unref (c))
248 for (i = 0; i < arc->n_specs; i++)
250 struct arc_spec *spec = &arc->specs[i];
251 int width = var_get_width (spec->src);
252 const union value *value = case_data (c, spec->src);
253 size_t hash = value_hash (value, width, 0);
254 struct arc_item *item;
256 item = find_arc_item (spec, value, hash);
259 ( arc->blank_valid || var_is_numeric (spec->src) || ! value_is_blank (value, width, arc->dict))
262 item = xmalloc (sizeof *item);
264 value_clone (&item->from, value, width);
265 hmap_insert (&spec->items->ht, &item->hmap_node, hash);
268 ok = casereader_destroy (input);
269 ok = proc_commit (ds) && ok;
271 /* Create transformation. */
272 for (i = 0; i < arc->n_specs; i++)
274 struct arc_spec *spec = &arc->specs[i];
275 struct arc_item **items;
276 struct arc_item *item;
280 /* Create destination variable. */
281 spec->dst = dict_create_var_assert (dict, dst_names[i], 0);
283 /* Create array of pointers to items. */
284 n_items = hmap_count (&spec->items->ht);
285 items = xmalloc (n_items * sizeof *items);
287 HMAP_FOR_EACH (item, struct arc_item, hmap_node, &spec->items->ht)
290 assert (j == n_items);
292 /* Sort array by value. */
293 sort (items, n_items, sizeof *items, compare_arc_items, NULL);
295 /* Assign recoded values in sorted order. */
296 for (j = 0; j < n_items; j++)
298 const union value *from = &items[j]->from;
299 char *recoded_value = NULL;
301 const int src_width = items[j]->width;
303 value_init (&to_val, 0);
305 items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
307 to_val.f = items[j]->to;
309 /* Add value labels to the destination variable which indicate
310 the source value from whence the new value comes. */
313 const char *str = CHAR_CAST_BUG (const char*, value_str (from, src_width));
315 recoded_value = recode_string (UTF8, dict_get_encoding (arc->dict), str, src_width);
318 recoded_value = c_xasprintf ("%g", from->f);
320 /* Remove trailing whitespace */
321 for (c = recoded_value; *c != '\0'; c++)
328 var_add_value_label (spec->dst, &to_val, recoded_value);
329 value_destroy (&to_val, 0);
330 free (recoded_value);
336 add_transformation (ds, autorecode_trns_proc, autorecode_trns_free, arc);
338 for (i = 0; i < n_dsts; i++)
343 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
346 for (i = 0; i < n_dsts; i++)
351 return CMD_CASCADING_FAILURE;
355 arc_free (struct autorecode_pgm *arc)
361 for (i = 0; i < arc->n_specs; i++)
363 struct arc_spec *spec = &arc->specs[i];
364 struct arc_item *item, *next;
366 HMAP_FOR_EACH_SAFE (item, next, struct arc_item, hmap_node,
369 value_destroy (&item->from, item->width);
370 hmap_delete (&spec->items->ht, &item->hmap_node);
375 for (i = 0; i < arc->n_specs; i++)
377 struct arc_spec *spec = &arc->specs[i];
379 if (--spec->items->refcnt == 0)
381 hmap_destroy (&spec->items->ht);
386 if (arc->global_items && --arc->global_items->refcnt == 0)
388 hmap_destroy (&arc->global_items->ht);
389 free (arc->global_items);
397 static struct arc_item *
398 find_arc_item (const struct arc_spec *spec, const union value *value,
401 struct arc_item *item;
403 HMAP_FOR_EACH_WITH_HASH (item, struct arc_item, hmap_node, hash, &spec->items->ht)
404 if (value_equal (value, &item->from, var_get_width (spec->src)))
410 compare_arc_items (const void *a_, const void *b_, const void *aux UNUSED)
412 const struct arc_item *const *a = a_;
413 const struct arc_item *const *b = b_;
414 int width_a = (*a)->width;
415 int width_b = (*b)->width;
417 if ( width_a == width_b)
418 return value_compare_3way (&(*a)->from, &(*b)->from, width_a);
420 if ( width_a == 0 && width_b != 0)
423 if ( width_b == 0 && width_a != 0)
426 return buf_compare_rpad (CHAR_CAST_BUG (const char *, value_str (&(*a)->from, width_a)), width_a,
427 CHAR_CAST_BUG (const char *, value_str (&(*b)->from, width_b)), width_b);
431 autorecode_trns_proc (void *arc_, struct ccase **c,
432 casenumber case_idx UNUSED)
434 struct autorecode_pgm *arc = arc_;
437 *c = case_unshare (*c);
438 for (i = 0; i < arc->n_specs; i++)
440 const struct arc_spec *spec = &arc->specs[i];
441 int width = var_get_width (spec->src);
442 const union value *value = case_data (*c, spec->src);
443 const struct arc_item *item = find_arc_item (spec, value, value_hash (value, width, 0));
445 case_data_rw (*c, spec->dst)->f = item ? item->to : SYSMIS;
448 return TRNS_CONTINUE;
452 autorecode_trns_free (void *arc_)
454 struct autorecode_pgm *arc = arc_;