DO IF, LOOP cleanup.
[pspp-builds.git] / src / autorecode.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <stdlib.h>
23 #include "alloc.h"
24 #include "case.h"
25 #include "command.h"
26 #include "dictionary.h"
27 #include "error.h"
28 #include "hash.h"
29 #include "lexer.h"
30 #include "pool.h"
31 #include "str.h"
32 #include "var.h"
33 #include "vfm.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* FIXME: Implement PRINT subcommand. */
39
40 /* Explains how to recode one value.  `from' must be first element.  */
41 struct arc_item
42   {
43     union value from;           /* Original value. */
44     double to;                  /* Recoded value. */
45   };
46
47 /* Explains how to recode an AUTORECODE variable. */
48 struct arc_spec
49   {
50     struct variable *src;       /* Source variable. */
51     struct variable *dest;      /* Target variable. */
52     struct hsh_table *items;    /* Hash table of `freq's. */
53   };
54
55 /* AUTORECODE transformation. */
56 struct autorecode_trns
57   {
58     struct pool *pool;          /* Contains AUTORECODE specs. */
59     struct arc_spec *specs;     /* AUTORECODE specifications. */
60     size_t spec_cnt;            /* Number of specifications. */
61   };
62
63 /* Descending or ascending sort order. */
64 enum direction 
65   {
66     ASCENDING,
67     DESCENDING
68   };
69
70 /* AUTORECODE data. */
71 struct autorecode_pgm 
72   {
73     struct variable **src_vars;    /* Source variables. */
74     char **dst_names;              /* Target variable names. */
75     struct variable **dst_vars;    /* Target variables. */
76     struct hsh_table **src_values; /* `union value's of source vars. */
77     size_t var_cnt;                /* Number of variables. */
78     struct pool *src_values_pool;  /* Pool used by src_values. */
79     enum direction direction;      /* Sort order. */
80     int print;                     /* Print mapping table if nonzero. */
81   };
82
83 static trns_proc_func autorecode_trns_proc;
84 static trns_free_func autorecode_trns_free;
85 static int autorecode_proc_func (struct ccase *, void *);
86 static hsh_compare_func compare_alpha_value, compare_numeric_value;
87 static hsh_hash_func hash_alpha_value, hash_numeric_value;
88
89 static void recode (const struct autorecode_pgm *);
90 static void arc_free (struct autorecode_pgm *);
91
92 /* Performs the AUTORECODE procedure. */
93 int
94 cmd_autorecode (void)
95 {
96   struct autorecode_pgm arc;
97   size_t dst_cnt;
98   size_t i;
99
100   arc.src_vars = NULL;
101   arc.dst_names = NULL;
102   arc.dst_vars = NULL;
103   arc.src_values = NULL;
104   arc.var_cnt = 0;
105   arc.src_values_pool = NULL;
106   arc.direction = ASCENDING;
107   arc.print = 0;
108   dst_cnt = 0;
109
110   lex_match_id ("VARIABLES");
111   lex_match ('=');
112   if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
113                         PV_NO_DUPLICATE))
114     goto lossage;
115   if (!lex_force_match_id ("INTO"))
116     goto lossage;
117   lex_match ('=');
118   if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
119     goto lossage;
120   if (dst_cnt != arc.var_cnt)
121     {
122       size_t i;
123
124       msg (SE, _("Source variable count (%u) does not match "
125                  "target variable count (%u)."),
126            (unsigned) arc.var_cnt, (unsigned) dst_cnt);
127
128       for (i = 0; i < dst_cnt; i++)
129         free (arc.dst_names[i]);
130       free (arc.dst_names);
131       arc.dst_names = NULL;
132
133       goto lossage;
134     }
135   while (lex_match ('/'))
136     if (lex_match_id ("DESCENDING"))
137       arc.direction = DESCENDING;
138     else if (lex_match_id ("PRINT"))
139       arc.print = 1;
140   if (token != '.')
141     {
142       lex_error (_("expecting end of command"));
143       goto lossage;
144     }
145
146   for (i = 0; i < arc.var_cnt; i++)
147     {
148       int j;
149
150       if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
151         {
152           msg (SE, _("Target variable %s duplicates existing variable %s."),
153                arc.dst_names[i], arc.dst_names[i]);
154           goto lossage;
155         }
156       for (j = 0; j < i; j++)
157         if (!strcasecmp (arc.dst_names[i], arc.dst_names[j]))
158           {
159             msg (SE, _("Duplicate variable name %s among target variables."),
160                  arc.dst_names[i]);
161             goto lossage;
162           }
163     }
164
165   arc.src_values_pool = pool_create ();
166   arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
167   arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
168   for (i = 0; i < dst_cnt; i++)
169     if (arc.src_vars[i]->type == ALPHA)
170       arc.src_values[i] = hsh_create (10, compare_alpha_value,
171                                       hash_alpha_value, NULL, arc.src_vars[i]);
172     else
173       arc.src_values[i] = hsh_create (10, compare_numeric_value,
174                                       hash_numeric_value, NULL, NULL);
175
176   procedure (autorecode_proc_func, &arc);
177
178   for (i = 0; i < arc.var_cnt; i++)
179     {
180       arc.dst_vars[i] = dict_create_var_assert (default_dict,
181                                                 arc.dst_names[i], 0);
182       arc.dst_vars[i]->init = 0;
183     }
184
185   recode (&arc);
186   arc_free (&arc);
187   return CMD_SUCCESS;
188
189 lossage:
190   arc_free (&arc);
191   return CMD_FAILURE;
192 }
193
194 static void
195 arc_free (struct autorecode_pgm *arc) 
196 {
197   free (arc->src_vars);
198   if (arc->dst_names != NULL) 
199     {
200       size_t i;
201       
202       for (i = 0; i < arc->var_cnt; i++)
203         free (arc->dst_names[i]);
204       free (arc->dst_names);
205     }
206   free (arc->dst_vars);
207   if (arc->src_values != NULL) 
208     {
209       size_t i;
210
211       for (i = 0; i < arc->var_cnt; i++)
212         hsh_destroy (arc->src_values[i]);
213       free (arc->src_values);
214     }
215   pool_destroy (arc->src_values_pool);
216 }
217
218 \f
219 /* AUTORECODE transformation. */
220
221 static void
222 recode (const struct autorecode_pgm *arc)
223 {
224   struct autorecode_trns *trns;
225   size_t i;
226
227   trns = pool_create_container (struct autorecode_trns, pool);
228   trns->specs = pool_nalloc (trns->pool, arc->var_cnt, sizeof *trns->specs);
229   trns->spec_cnt = arc->var_cnt;
230   for (i = 0; i < arc->var_cnt; i++)
231     {
232       struct arc_spec *spec = &trns->specs[i];
233       void *const *p = hsh_sort (arc->src_values[i]);
234       int count = hsh_count (arc->src_values[i]);
235       int j;
236
237       spec->src = arc->src_vars[i];
238       spec->dest = arc->dst_vars[i];
239
240       if (arc->src_vars[i]->type == ALPHA)
241         spec->items = hsh_create (2 * count, compare_alpha_value,
242                                   hash_alpha_value, NULL, arc->src_vars[i]);
243       else
244         spec->items = hsh_create (2 * count, compare_numeric_value,
245                                   hash_numeric_value, NULL, NULL);
246
247       for (j = 0; *p; p++, j++)
248         {
249           struct arc_item *item = pool_alloc (trns->pool, sizeof *item);
250           union value *vp = *p;
251           
252           if (arc->src_vars[i]->type == NUMERIC)
253             item->from.f = vp->f;
254           else
255             item->from.c = pool_strdup (trns->pool, vp->c);
256           item->to = arc->direction == ASCENDING ? j + 1 : count - j;
257           hsh_force_insert (spec->items, item);
258         }
259     }
260   add_transformation (autorecode_trns_proc, autorecode_trns_free, trns);
261 }
262
263 static int
264 autorecode_trns_proc (void *trns_, struct ccase *c, int case_idx UNUSED)
265 {
266   struct autorecode_trns *trns = trns_;
267   size_t i;
268
269   for (i = 0; i < trns->spec_cnt; i++)
270     {
271       struct arc_spec *spec = &trns->specs[i];
272       struct arc_item *item;
273       union value v;
274
275       if (spec->src->type == NUMERIC)
276         v.f = case_num (c, spec->src->fv);
277       else
278         v.c = (char *) case_str (c, spec->src->fv);
279       item = hsh_force_find (spec->items, &v);
280
281       case_data_rw (c, spec->dest->fv)->f = item->to;
282     }
283   return -1;
284 }
285
286 static void
287 autorecode_trns_free (void *trns_)
288 {
289   struct autorecode_trns *trns = trns_;
290   size_t i;
291
292   for (i = 0; i < trns->spec_cnt; i++)
293     hsh_destroy (trns->specs[i].items);
294   pool_destroy (trns->pool);
295 }
296 \f
297 /* AUTORECODE procedure. */
298
299 static int
300 compare_alpha_value (const void *a_, const void *b_, void *v_)
301 {
302   const union value *a = a_;
303   const union value *b = b_;
304   const struct variable *v = v_;
305
306   return memcmp (a->c, b->c, v->width);
307 }
308
309 static unsigned
310 hash_alpha_value (const void *a_, void *v_)
311 {
312   const union value *a = a_;
313   const struct variable *v = v_;
314   
315   return hsh_hash_bytes (a->c, v->width);
316 }
317
318 static int
319 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
320 {
321   const union value *a = a_;
322   const union value *b = b_;
323
324   return a->f < b->f ? -1 : a->f > b->f;
325 }
326
327 static unsigned
328 hash_numeric_value (const void *a_, void *foo UNUSED)
329 {
330   const union value *a = a_;
331
332   return hsh_hash_double (a->f);
333 }
334
335 static int
336 autorecode_proc_func (struct ccase *c, void *arc_)
337 {
338   struct autorecode_pgm *arc = arc_;
339   size_t i;
340
341   for (i = 0; i < arc->var_cnt; i++)
342     {
343       union value v, *vp, **vpp;
344
345       if (arc->src_vars[i]->type == NUMERIC)
346         v.f = case_num (c, arc->src_vars[i]->fv);
347       else
348         v.c = (char *) case_str (c, arc->src_vars[i]->fv);
349
350       vpp = (union value **) hsh_probe (arc->src_values[i], &v);
351       if (*vpp == NULL)
352         {
353           vp = pool_alloc (arc->src_values_pool, sizeof *vp);
354           if (arc->src_vars[i]->type == NUMERIC)
355             vp->f = v.f;
356           else
357             vp->c = pool_strndup (arc->src_values_pool,
358                                   v.c, arc->src_vars[i]->width);
359           *vpp = vp;
360         }
361     }
362   return 1;
363 }