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/compiler.h"
32 #include "libpspp/hash-functions.h"
33 #include "libpspp/hmap.h"
34 #include "libpspp/message.h"
35 #include "libpspp/pool.h"
36 #include "libpspp/str.h"
38 #include "gl/xalloc.h"
41 #define _(msgid) gettext (msgid)
43 /* FIXME: Implement PRINT subcommand. */
45 /* Explains how to recode one value. */
48 struct hmap_node hmap_node; /* Element in "struct arc_spec" hash table. */
49 union value from; /* Original value. */
50 double to; /* Recoded value. */
53 /* Explains how to recode an AUTORECODE variable. */
56 const struct variable *src; /* Source variable. */
57 struct variable *dst; /* Target variable. */
58 struct hmap items; /* Hash table of "struct arc_item"s. */
61 /* Descending or ascending sort order. */
68 /* AUTORECODE data. */
71 struct arc_spec *specs;
75 static trns_proc_func autorecode_trns_proc;
76 static trns_free_func autorecode_trns_free;
78 static int compare_arc_items (const void *, const void *, const void *width);
79 static void arc_free (struct autorecode_pgm *);
80 static struct arc_item *find_arc_item (struct arc_spec *, const union value *,
83 /* Performs the AUTORECODE procedure. */
85 cmd_autorecode (struct lexer *lexer, struct dataset *ds)
87 struct autorecode_pgm *arc = NULL;
89 const struct variable **src_vars = NULL;
90 char **dst_names = NULL;
94 enum arc_direction direction;
97 struct casereader *input;
103 /* Parse variable lists. */
104 lex_match_id (lexer, "VARIABLES");
105 lex_match (lexer, '=');
106 if (!parse_variables_const (lexer, dataset_dict (ds), &src_vars, &n_srcs,
109 if (!lex_force_match_id (lexer, "INTO"))
111 lex_match (lexer, '=');
112 if (!parse_DATA_LIST_vars (lexer, &dst_names, &n_dsts, PV_NO_DUPLICATE))
114 if (n_dsts != n_srcs)
116 msg (SE, _("Source variable count (%zu) does not match "
117 "target variable count (%zu)."),
122 for (i = 0; i < n_dsts; i++)
124 const char *name = dst_names[i];
126 if (dict_lookup_var (dataset_dict (ds), name) != NULL)
128 msg (SE, _("Target variable %s duplicates existing variable %s."),
135 direction = ASCENDING;
137 while (lex_match (lexer, '/'))
138 if (lex_match_id (lexer, "DESCENDING"))
139 direction = DESCENDING;
140 else if (lex_match_id (lexer, "PRINT"))
142 if (lex_token (lexer) != '.')
144 lex_error (lexer, _("expecting end of command"));
148 /* Create procedure. */
149 arc = xmalloc (sizeof *arc);
150 arc->specs = xmalloc (n_dsts * sizeof *arc->specs);
151 arc->n_specs = n_dsts;
152 for (i = 0; i < n_dsts; i++)
154 struct arc_spec *spec = &arc->specs[i];
156 spec->src = src_vars[i];
157 hmap_init (&spec->items);
160 /* Execute procedure. */
161 input = proc_open (ds);
162 for (; (c = casereader_read (input)) != NULL; case_unref (c))
163 for (i = 0; i < arc->n_specs; i++)
165 struct arc_spec *spec = &arc->specs[i];
166 int width = var_get_width (spec->src);
167 const union value *value = case_data (c, spec->src);
168 size_t hash = value_hash (value, width, 0);
169 struct arc_item *item;
171 item = find_arc_item (spec, value, hash);
174 item = xmalloc (sizeof *item);
175 value_clone (&item->from, value, width);
176 hmap_insert (&spec->items, &item->hmap_node, hash);
179 ok = casereader_destroy (input);
180 ok = proc_commit (ds) && ok;
182 /* Create transformation. */
183 for (i = 0; i < arc->n_specs; i++)
185 struct arc_spec *spec = &arc->specs[i];
186 struct arc_item **items;
187 struct arc_item *item;
192 /* Create destination variable. */
193 spec->dst = dict_create_var_assert (dataset_dict (ds), dst_names[i], 0);
195 /* Create array of pointers to items. */
196 n_items = hmap_count (&spec->items);
197 items = xmalloc (n_items * sizeof *items);
199 HMAP_FOR_EACH (item, struct arc_item, hmap_node, &spec->items)
201 assert (j == n_items);
203 /* Sort array by value. */
204 src_width = var_get_width (spec->src);
205 sort (items, n_items, sizeof *items, compare_arc_items, &src_width);
207 /* Assign recoded values in sorted order. */
208 for (j = 0; j < n_items; j++)
209 items[j]->to = direction == ASCENDING ? j + 1 : n_items - j;
214 add_transformation (ds, autorecode_trns_proc, autorecode_trns_free, arc);
216 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
219 for (i = 0; i < n_dsts; i++)
224 return CMD_CASCADING_FAILURE;
228 arc_free (struct autorecode_pgm *arc)
234 for (i = 0; i < arc->n_specs; i++)
236 struct arc_spec *spec = &arc->specs[i];
237 int width = var_get_width (spec->src);
238 struct arc_item *item, *next;
240 HMAP_FOR_EACH_SAFE (item, next, struct arc_item, hmap_node,
243 value_destroy (&item->from, width);
244 hmap_delete (&spec->items, &item->hmap_node);
247 hmap_destroy (&spec->items);
254 static struct arc_item *
255 find_arc_item (struct arc_spec *spec, const union value *value,
258 struct arc_item *item;
260 HMAP_FOR_EACH_WITH_HASH (item, struct arc_item, hmap_node, hash,
262 if (value_equal (value, &item->from, var_get_width (spec->src)))
268 compare_arc_items (const void *a_, const void *b_, const void *width_)
270 const struct arc_item *const *a = a_;
271 const struct arc_item *const *b = b_;
272 const int *width = width_;
274 return value_compare_3way (&(*a)->from, &(*b)->from, *width);
278 autorecode_trns_proc (void *arc_, struct ccase **c,
279 casenumber case_idx UNUSED)
281 struct autorecode_pgm *arc = arc_;
284 *c = case_unshare (*c);
285 for (i = 0; i < arc->n_specs; i++)
287 struct arc_spec *spec = &arc->specs[i];
288 int width = var_get_width (spec->src);
289 const union value *value = case_data (*c, spec->src);
290 struct arc_item *item;
292 item = find_arc_item (spec, value, value_hash (value, width, 0));
293 case_data_rw (*c, spec->dst)->f = item ? item->to : SYSMIS;
296 return TRNS_CONTINUE;
300 autorecode_trns_free (void *arc_)
302 struct autorecode_pgm *arc = arc_;