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 double to; /* Recoded value. */
55 /* Explains how to recode an AUTORECODE variable. */
58 const struct variable *src; /* Source variable. */
59 struct variable *dst; /* Target variable. */
60 struct hmap items; /* Hash table of "struct arc_item"s. */
63 /* Descending or ascending sort order. */
70 /* AUTORECODE data. */
73 struct arc_spec *specs;
77 static trns_proc_func autorecode_trns_proc;
78 static trns_free_func autorecode_trns_free;
80 static int compare_arc_items (const void *, const void *, const void *width);
81 static void arc_free (struct autorecode_pgm *);
82 static struct arc_item *find_arc_item (struct arc_spec *, const union value *,
85 /* Performs the AUTORECODE procedure. */
87 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
89 struct autorecode_pgm *arc = NULL;
90 struct dictionary *dict = dataset_dict (ds);
92 const struct variable **src_vars = NULL;
93 char **dst_names = NULL;
97 enum arc_direction direction;
100 struct casereader *input;
106 /* Parse variable lists. */
107 lex_match_id (lexer, "VARIABLES");
108 lex_match (lexer, '=');
109 if (!parse_variables_const (lexer, dict, &src_vars, &n_srcs,
112 if (!lex_force_match_id (lexer, "INTO"))
114 lex_match (lexer, '=');
115 if (!parse_DATA_LIST_vars (lexer, &dst_names, &n_dsts, PV_NO_DUPLICATE))
117 if (n_dsts != n_srcs)
119 msg (SE, _("Source variable count (%zu) does not match "
120 "target variable count (%zu)."),
125 for (i = 0; i < n_dsts; i++)
127 const char *name = dst_names[i];
129 if (dict_lookup_var (dict, name) != NULL)
131 msg (SE, _("Target variable %s duplicates existing variable %s."),
138 direction = ASCENDING;
140 while (lex_match (lexer, '/'))
141 if (lex_match_id (lexer, "DESCENDING"))
142 direction = DESCENDING;
143 else if (lex_match_id (lexer, "PRINT"))
145 if (lex_token (lexer) != '.')
147 lex_error (lexer, _("expecting end of command"));
151 /* Create procedure. */
152 arc = xmalloc (sizeof *arc);
153 arc->specs = xmalloc (n_dsts * sizeof *arc->specs);
154 arc->n_specs = n_dsts;
155 for (i = 0; i < n_dsts; i++)
157 struct arc_spec *spec = &arc->specs[i];
159 spec->src = src_vars[i];
160 hmap_init (&spec->items);
163 /* Execute procedure. */
164 input = proc_open (ds);
165 for (; (c = casereader_read (input)) != NULL; case_unref (c))
166 for (i = 0; i < arc->n_specs; i++)
168 struct arc_spec *spec = &arc->specs[i];
169 int width = var_get_width (spec->src);
170 const union value *value = case_data (c, spec->src);
171 size_t hash = value_hash (value, width, 0);
172 struct arc_item *item;
174 item = find_arc_item (spec, value, hash);
177 item = xmalloc (sizeof *item);
178 value_clone (&item->from, value, width);
179 hmap_insert (&spec->items, &item->hmap_node, hash);
182 ok = casereader_destroy (input);
183 ok = proc_commit (ds) && ok;
185 /* Create transformation. */
186 for (i = 0; i < arc->n_specs; i++)
188 struct arc_spec *spec = &arc->specs[i];
189 struct arc_item **items;
190 struct arc_item *item;
195 /* Create destination variable. */
196 spec->dst = dict_create_var_assert (dict, dst_names[i], 0);
198 /* Create array of pointers to items. */
199 n_items = hmap_count (&spec->items);
200 items = xmalloc (n_items * sizeof *items);
202 HMAP_FOR_EACH (item, struct arc_item, hmap_node, &spec->items)
204 assert (j == n_items);
206 /* Sort array by value. */
207 src_width = var_get_width (spec->src);
208 sort (items, n_items, sizeof *items, compare_arc_items, &src_width);
210 /* Assign recoded values in sorted order. */
211 for (j = 0; j < n_items; j++)
213 const union value *from = &items[j]->from;
215 char *recoded_value = NULL;
219 value_init (&to_val, 0);
221 items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
223 to_val.f = items[j]->to;
225 /* Add value labels to the destination variable which indicate
226 the source value from whence the new value comes. */
230 recode_string (UTF8, dict_get_encoding (dict), (char *) value_str (from, src_width), src_width);
232 recoded_value = asnprintf (NULL, &len, "%g", from->f);
234 /* Remove trailing whitespace */
235 for (c = recoded_value; *c != '\0'; c++)
242 var_add_value_label (spec->dst, &to_val, recoded_value);
243 value_destroy (&to_val, 0);
244 free (recoded_value);
250 add_transformation (ds, autorecode_trns_proc, autorecode_trns_free, arc);
252 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
255 for (i = 0; i < n_dsts; i++)
260 return CMD_CASCADING_FAILURE;
264 arc_free (struct autorecode_pgm *arc)
270 for (i = 0; i < arc->n_specs; i++)
272 struct arc_spec *spec = &arc->specs[i];
273 int width = var_get_width (spec->src);
274 struct arc_item *item, *next;
276 HMAP_FOR_EACH_SAFE (item, next, struct arc_item, hmap_node,
279 value_destroy (&item->from, width);
280 hmap_delete (&spec->items, &item->hmap_node);
283 hmap_destroy (&spec->items);
290 static struct arc_item *
291 find_arc_item (struct arc_spec *spec, const union value *value,
294 struct arc_item *item;
296 HMAP_FOR_EACH_WITH_HASH (item, struct arc_item, hmap_node, hash,
298 if (value_equal (value, &item->from, var_get_width (spec->src)))
304 compare_arc_items (const void *a_, const void *b_, const void *width_)
306 const struct arc_item *const *a = a_;
307 const struct arc_item *const *b = b_;
308 const int *width = width_;
310 return value_compare_3way (&(*a)->from, &(*b)->from, *width);
314 autorecode_trns_proc (void *arc_, struct ccase **c,
315 casenumber case_idx UNUSED)
317 struct autorecode_pgm *arc = arc_;
320 *c = case_unshare (*c);
321 for (i = 0; i < arc->n_specs; i++)
323 struct arc_spec *spec = &arc->specs[i];
324 int width = var_get_width (spec->src);
325 const union value *value = case_data (*c, spec->src);
326 struct arc_item *item;
328 item = find_arc_item (spec, value, value_hash (value, width, 0));
329 case_data_rw (*c, spec->dst)->f = item ? item->to : SYSMIS;
332 return TRNS_CONTINUE;
336 autorecode_trns_free (void *arc_)
338 struct autorecode_pgm *arc = arc_;