bec21badbfc84e35f428cc03ce5a442ec2de66bc
[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 <assert.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 #include "debug-print.h"
34
35 /* FIXME: This module is less than ideally efficient, both in space
36    and time.  If anyone cares, it would be a good project. */
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 *arc;       /* AUTORECODE specifications. */
61     int n_arc;                  /* Number of specifications. */
62   };
63
64 /* Source and target variables, hash table translator. */
65 static struct variable **v_src;
66 static struct variable **v_dest;
67 static struct hsh_table **h_trans;
68 static int nv_src;
69
70 /* Pool for allocation of hash table entries. */
71 static struct pool *hash_pool;
72
73 /* Options. */
74 static int descend;
75 static int print;
76
77 static int autorecode_trns_proc (struct trns_header *, struct ccase *);
78 static void autorecode_trns_free (struct trns_header *);
79 static int autorecode_proc_func (struct ccase *);
80 static hsh_compare_func compare_alpha_value, compare_numeric_value;
81 static hsh_hash_func hash_alpha_value, hash_numeric_value;
82 static void recode (void);
83
84 /* Performs the AUTORECODE procedure. */
85 int
86 cmd_autorecode (void)
87 {
88   /* Dest var names. */
89   char **n_dest = NULL;
90   int nv_dest = 0;
91
92   int i;
93
94   v_src = NULL;
95   descend = print = 0;
96   h_trans = NULL;
97
98   lex_match_id ("AUTORECODE");
99   lex_match_id ("VARIABLES");
100   lex_match ('=');
101   if (!parse_variables (&default_dict, &v_src, &nv_src, PV_NO_DUPLICATE))
102     return CMD_FAILURE;
103   if (!lex_force_match_id ("INTO"))
104     return CMD_FAILURE;
105   lex_match ('=');
106   if (!parse_DATA_LIST_vars (&n_dest, &nv_dest, PV_NONE))
107     goto lossage;
108   if (nv_dest != nv_src)
109     {
110       msg (SE, _("Number of source variables (%d) does not match number "
111            "of target variables (%d)."), nv_src, nv_dest);
112       goto lossage;
113     }
114   while (lex_match ('/'))
115     if (lex_match_id ("DESCENDING"))
116       descend = 1;
117     else if (lex_match_id ("PRINT"))
118       print = 1;
119   if (token != '.')
120     {
121       lex_error (_("expecting end of command"));
122       goto lossage;
123     }
124
125   for (i = 0; i < nv_dest; i++)
126     {
127       int j;
128
129       if (is_varname (n_dest[i]))
130         {
131           msg (SE, _("Target variable %s duplicates existing variable %s."),
132                n_dest[i], n_dest[i]);
133           goto lossage;
134         }
135       for (j = 0; j < i; j++)
136         if (!strcmp (n_dest[i], n_dest[j]))
137           {
138             msg (SE, _("Duplicate variable name %s among target variables."),
139                  n_dest[i]);
140             goto lossage;
141           }
142     }
143
144   hash_pool = pool_create ();
145
146   v_dest = xmalloc (sizeof *v_dest * nv_dest);
147   h_trans = xmalloc (sizeof *h_trans * nv_dest);
148   for (i = 0; i < nv_dest; i++)
149     if (v_src[i]->type == ALPHA)
150       h_trans[i] = hsh_create (10, compare_alpha_value,
151                                hash_alpha_value, NULL, v_src[i]);
152     else
153       h_trans[i] = hsh_create (10, compare_numeric_value,
154                                hash_numeric_value, NULL, NULL);
155
156   procedure (NULL, autorecode_proc_func, NULL);
157
158   for (i = 0; i < nv_dest; i++)
159     {
160       v_dest[i] = force_create_variable (&default_dict, n_dest[i], NUMERIC, 0);
161       free (n_dest[i]);
162     }
163   free (n_dest);
164
165   recode ();
166   
167   free (v_src);
168   free (v_dest);
169
170   return CMD_SUCCESS;
171
172 lossage:
173   if (h_trans != NULL)
174     for (i = 0; i < nv_src; i++)
175       hsh_destroy (h_trans[i]);
176   for (i = 0; i < nv_dest; i++)
177     free (n_dest[i]);
178   free (n_dest);
179   free (v_src);
180   return CMD_FAILURE;
181 }
182 \f
183 /* AUTORECODE transformation. */
184
185 static void
186 recode (void)
187 {
188   struct autorecode_trns *t;
189   struct pool *arc_pool;
190   int i;
191
192   arc_pool = pool_create ();
193   t = xmalloc (sizeof *t);
194   t->h.proc = autorecode_trns_proc;
195   t->h.free = autorecode_trns_free;
196   t->owner = arc_pool;
197   t->arc = pool_alloc (arc_pool, sizeof *t->arc * nv_src);
198   t->n_arc = nv_src;
199   for (i = 0; i < nv_src; i++)
200     {
201       struct arc_spec *spec = &t->arc[i];
202       void **p = hsh_sort (h_trans[i]);
203       int count = hsh_count (h_trans[i]);
204       int j;
205
206       spec->src = v_src[i];
207       spec->dest = v_dest[i];
208
209       if (v_src[i]->type == ALPHA)
210         spec->items = hsh_create (2 * count, compare_alpha_value,
211                                   hash_alpha_value, NULL, v_src[i]);
212       else
213         spec->items = hsh_create (2 * count, compare_numeric_value,
214                                   hash_numeric_value, NULL, NULL);
215
216       for (j = 0; *p; p++, j++)
217         {
218           struct arc_item *item = pool_alloc (arc_pool, sizeof *item);
219           union value *vp = *p;
220           
221           if (v_src[i]->type == NUMERIC)
222             item->from.f = vp->f;
223           else
224             item->from.c = pool_strdup (arc_pool, vp->c);
225           item->to = !descend ? j + 1 : count - j;
226           hsh_force_insert (spec->items, item);
227         }
228       
229       hsh_destroy (h_trans[i]);
230     }
231   free (h_trans);
232   pool_destroy (hash_pool);
233   add_transformation ((struct trns_header *) t);
234 }
235
236 static int
237 autorecode_trns_proc (struct trns_header * trns, struct ccase * c)
238 {
239   struct autorecode_trns *t = (struct autorecode_trns *) trns;
240   int i;
241
242   for (i = 0; i < t->n_arc; i++)
243     {
244       struct arc_spec *spec = &t->arc[i];
245       struct arc_item *item;
246
247       if (spec->src->type == NUMERIC)
248         item = hsh_force_find (spec->items, &c->data[spec->src->fv].f);
249       else
250         {
251           union value v;
252           v.c = c->data[spec->src->fv].s;
253           item = hsh_force_find (spec->items, &v);
254         }
255
256       c->data[spec->dest->fv].f = item->to;
257     }
258   return -1;
259 }
260
261 static void
262 autorecode_trns_free (struct trns_header * trns)
263 {
264   struct autorecode_trns *t = (struct autorecode_trns *) trns;
265   int i;
266
267   for (i = 0; i < t->n_arc; i++)
268     hsh_destroy (t->arc[i].items);
269   pool_destroy (t->owner);
270 }
271 \f
272 /* AUTORECODE procedure. */
273
274 static int
275 compare_alpha_value (const void *a_, const void *b_, void *v_)
276 {
277   const union value *a = a_;
278   const union value *b = b_;
279   const struct variable *v = v_;
280
281   return memcmp (a->c, b->c, v->width);
282 }
283
284 static unsigned
285 hash_alpha_value (const void *a_, void *v_)
286 {
287   const union value *a = a_;
288   const struct variable *v = v_;
289   
290   return hsh_hash_bytes (a->c, v->width);
291 }
292
293 static int
294 compare_numeric_value (const void *a_, const void *b_, void *foo unused)
295 {
296   const union value *a = a_;
297   const union value *b = b_;
298
299   return a->f < b->f ? -1 : a->f > b->f;
300 }
301
302 static unsigned
303 hash_numeric_value (const void *a_, void *foo unused)
304 {
305   const union value *a = a_;
306
307   return hsh_hash_double (a->f);
308 }
309
310 static int
311 autorecode_proc_func (struct ccase * c)
312 {
313   int i;
314
315   for (i = 0; i < nv_src; i++)
316     {
317       union value v;
318       union value *vp;
319       union value **vpp;
320
321       if (v_src[i]->type == NUMERIC)
322         {
323           v.f = c->data[v_src[i]->fv].f;
324           vpp = (union value **) hsh_probe (h_trans[i], &v);
325           if (*vpp == NULL)
326             {
327               vp = pool_alloc (hash_pool, sizeof (union value));
328               vp->f = v.f;
329               *vpp = vp;
330             }
331         }
332       else
333         {
334           v.c = c->data[v_src[i]->fv].s;
335           vpp = (union value **) hsh_probe (h_trans[i], &v);
336           if (*vpp == NULL)
337             {
338               vp = pool_alloc (hash_pool, sizeof (union value));
339               vp->c = pool_strndup (hash_pool, v.c, v_src[i]->width);
340               *vpp = vp;
341             }
342         }
343     }
344   return 1;
345 }