bf38d922122da5b49566873d9569dc001e433f71
[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 trns_header h;
59     struct pool *owner;         /* Contains AUTORECODE specs. */
60     struct arc_spec *specs;     /* AUTORECODE specifications. */
61     size_t spec_cnt;            /* Number of specifications. */
62   };
63
64 /* Descending or ascending sort order. */
65 enum direction 
66   {
67     ASCENDING,
68     DESCENDING
69   };
70
71 /* AUTORECODE data. */
72 struct autorecode_pgm 
73   {
74     struct variable **src_vars;    /* Source variables. */
75     char **dst_names;              /* Target variable names. */
76     struct variable **dst_vars;    /* Target variables. */
77     struct hsh_table **src_values; /* `union value's of source vars. */
78     size_t var_cnt;                /* Number of variables. */
79     struct pool *src_values_pool;  /* Pool used by src_values. */
80     enum direction direction;      /* Sort order. */
81     int print;                     /* Print mapping table if nonzero. */
82   };
83
84 static trns_proc_func autorecode_trns_proc;
85 static trns_free_func autorecode_trns_free;
86 static int autorecode_proc_func (struct ccase *, void *);
87 static hsh_compare_func compare_alpha_value, compare_numeric_value;
88 static hsh_hash_func hash_alpha_value, hash_numeric_value;
89
90 static void recode (const struct autorecode_pgm *);
91 static void arc_free (struct autorecode_pgm *);
92
93 /* Performs the AUTORECODE procedure. */
94 int
95 cmd_autorecode (void)
96 {
97   struct autorecode_pgm arc;
98   size_t dst_cnt;
99   size_t i;
100
101   arc.src_vars = NULL;
102   arc.dst_names = NULL;
103   arc.dst_vars = NULL;
104   arc.src_values = NULL;
105   arc.var_cnt = 0;
106   arc.src_values_pool = NULL;
107   arc.direction = ASCENDING;
108   arc.print = 0;
109   dst_cnt = 0;
110
111   lex_match_id ("VARIABLES");
112   lex_match ('=');
113   if (!parse_variables (default_dict, &arc.src_vars, &arc.var_cnt,
114                         PV_NO_DUPLICATE))
115     goto lossage;
116   if (!lex_force_match_id ("INTO"))
117     goto lossage;
118   lex_match ('=');
119   if (!parse_DATA_LIST_vars (&arc.dst_names, &dst_cnt, PV_NONE))
120     goto lossage;
121   if (dst_cnt != arc.var_cnt)
122     {
123       size_t i;
124
125       msg (SE, _("Source variable count (%u) does not match "
126                  "target variable count (%u)."),
127            (unsigned) arc.var_cnt, (unsigned) dst_cnt);
128
129       for (i = 0; i < dst_cnt; i++)
130         free (arc.dst_names[i]);
131       free (arc.dst_names);
132       arc.dst_names = NULL;
133
134       goto lossage;
135     }
136   while (lex_match ('/'))
137     if (lex_match_id ("DESCENDING"))
138       arc.direction = DESCENDING;
139     else if (lex_match_id ("PRINT"))
140       arc.print = 1;
141   if (token != '.')
142     {
143       lex_error (_("expecting end of command"));
144       goto lossage;
145     }
146
147   for (i = 0; i < arc.var_cnt; i++)
148     {
149       int j;
150
151       if (dict_lookup_var (default_dict, arc.dst_names[i]) != NULL)
152         {
153           msg (SE, _("Target variable %s duplicates existing variable %s."),
154                arc.dst_names[i], arc.dst_names[i]);
155           goto lossage;
156         }
157       for (j = 0; j < i; j++)
158         if (!strcasecmp (arc.dst_names[i], arc.dst_names[j]))
159           {
160             msg (SE, _("Duplicate variable name %s among target variables."),
161                  arc.dst_names[i]);
162             goto lossage;
163           }
164     }
165
166   arc.src_values_pool = pool_create ();
167   arc.dst_vars = xnmalloc (arc.var_cnt, sizeof *arc.dst_vars);
168   arc.src_values = xnmalloc (arc.var_cnt, sizeof *arc.src_values);
169   for (i = 0; i < dst_cnt; i++)
170     if (arc.src_vars[i]->type == ALPHA)
171       arc.src_values[i] = hsh_create (10, compare_alpha_value,
172                                       hash_alpha_value, NULL, arc.src_vars[i]);
173     else
174       arc.src_values[i] = hsh_create (10, compare_numeric_value,
175                                       hash_numeric_value, NULL, NULL);
176
177   procedure (autorecode_proc_func, &arc);
178
179   for (i = 0; i < arc.var_cnt; i++)
180     {
181       arc.dst_vars[i] = dict_create_var_assert (default_dict,
182                                                 arc.dst_names[i], 0);
183       arc.dst_vars[i]->init = 0;
184     }
185
186   recode (&arc);
187   arc_free (&arc);
188   return CMD_SUCCESS;
189
190 lossage:
191   arc_free (&arc);
192   return CMD_FAILURE;
193 }
194
195 static void
196 arc_free (struct autorecode_pgm *arc) 
197 {
198   free (arc->src_vars);
199   if (arc->dst_names != NULL) 
200     {
201       size_t i;
202       
203       for (i = 0; i < arc->var_cnt; i++)
204         free (arc->dst_names[i]);
205       free (arc->dst_names);
206     }
207   free (arc->dst_vars);
208   if (arc->src_values != NULL) 
209     {
210       size_t i;
211
212       for (i = 0; i < arc->var_cnt; i++)
213         hsh_destroy (arc->src_values[i]);
214       free (arc->src_values);
215     }
216   pool_destroy (arc->src_values_pool);
217 }
218
219 \f
220 /* AUTORECODE transformation. */
221
222 static void
223 recode (const struct autorecode_pgm *arc)
224 {
225   struct autorecode_trns *t;
226   struct pool *pool;
227   size_t i;
228
229   pool = pool_create ();
230   t = xmalloc (sizeof *t);
231   t->h.proc = autorecode_trns_proc;
232   t->h.free = autorecode_trns_free;
233   t->owner = pool;
234   t->specs = pool_nalloc (t->owner, arc->var_cnt, sizeof *t->specs);
235   t->spec_cnt = arc->var_cnt;
236   for (i = 0; i < arc->var_cnt; i++)
237     {
238       struct arc_spec *spec = &t->specs[i];
239       void *const *p = hsh_sort (arc->src_values[i]);
240       int count = hsh_count (arc->src_values[i]);
241       int j;
242
243       spec->src = arc->src_vars[i];
244       spec->dest = arc->dst_vars[i];
245
246       if (arc->src_vars[i]->type == ALPHA)
247         spec->items = hsh_create (2 * count, compare_alpha_value,
248                                   hash_alpha_value, NULL, arc->src_vars[i]);
249       else
250         spec->items = hsh_create (2 * count, compare_numeric_value,
251                                   hash_numeric_value, NULL, NULL);
252
253       for (j = 0; *p; p++, j++)
254         {
255           struct arc_item *item = pool_alloc (t->owner, sizeof *item);
256           union value *vp = *p;
257           
258           if (arc->src_vars[i]->type == NUMERIC)
259             item->from.f = vp->f;
260           else
261             item->from.c = pool_strdup (t->owner, vp->c);
262           item->to = arc->direction == ASCENDING ? j + 1 : count - j;
263           hsh_force_insert (spec->items, item);
264         }
265     }
266   add_transformation (&t->h);
267 }
268
269 static int
270 autorecode_trns_proc (struct trns_header * trns, struct ccase * c,
271                       int case_idx UNUSED)
272 {
273   struct autorecode_trns *t = (struct autorecode_trns *) trns;
274   size_t i;
275
276   for (i = 0; i < t->spec_cnt; i++)
277     {
278       struct arc_spec *spec = &t->specs[i];
279       struct arc_item *item;
280       union value v;
281
282       if (spec->src->type == NUMERIC)
283         v.f = case_num (c, spec->src->fv);
284       else
285         v.c = (char *) case_str (c, spec->src->fv);
286       item = hsh_force_find (spec->items, &v);
287
288       case_data_rw (c, spec->dest->fv)->f = item->to;
289     }
290   return -1;
291 }
292
293 static void
294 autorecode_trns_free (struct trns_header * trns)
295 {
296   struct autorecode_trns *t = (struct autorecode_trns *) trns;
297   size_t i;
298
299   for (i = 0; i < t->spec_cnt; i++)
300     hsh_destroy (t->specs[i].items);
301   pool_destroy (t->owner);
302 }
303 \f
304 /* AUTORECODE procedure. */
305
306 static int
307 compare_alpha_value (const void *a_, const void *b_, void *v_)
308 {
309   const union value *a = a_;
310   const union value *b = b_;
311   const struct variable *v = v_;
312
313   return memcmp (a->c, b->c, v->width);
314 }
315
316 static unsigned
317 hash_alpha_value (const void *a_, void *v_)
318 {
319   const union value *a = a_;
320   const struct variable *v = v_;
321   
322   return hsh_hash_bytes (a->c, v->width);
323 }
324
325 static int
326 compare_numeric_value (const void *a_, const void *b_, void *foo UNUSED)
327 {
328   const union value *a = a_;
329   const union value *b = b_;
330
331   return a->f < b->f ? -1 : a->f > b->f;
332 }
333
334 static unsigned
335 hash_numeric_value (const void *a_, void *foo UNUSED)
336 {
337   const union value *a = a_;
338
339   return hsh_hash_double (a->f);
340 }
341
342 static int
343 autorecode_proc_func (struct ccase *c, void *arc_)
344 {
345   struct autorecode_pgm *arc = arc_;
346   size_t i;
347
348   for (i = 0; i < arc->var_cnt; i++)
349     {
350       union value v, *vp, **vpp;
351
352       if (arc->src_vars[i]->type == NUMERIC)
353         v.f = case_num (c, arc->src_vars[i]->fv);
354       else
355         v.c = (char *) case_str (c, arc->src_vars[i]->fv);
356
357       vpp = (union value **) hsh_probe (arc->src_values[i], &v);
358       if (*vpp == NULL)
359         {
360           vp = pool_alloc (arc->src_values_pool, sizeof *vp);
361           if (arc->src_vars[i]->type == NUMERIC)
362             vp->f = v.f;
363           else
364             vp->c = pool_strndup (arc->src_values_pool,
365                                   v.c, arc->src_vars[i]->width);
366           *vpp = vp;
367         }
368     }
369   return 1;
370 }