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