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