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