1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
33 #include "debug-print.h"
35 /* FIXME: This module is less than ideally efficient, both in space
36 and time. If anyone cares, it would be a good project. */
38 /* FIXME: Implement PRINT subcommand. */
40 /* Explains how to recode one value. `from' must be first element. */
43 union value from; /* Original value. */
44 double to; /* Recoded value. */
47 /* Explains how to recode an AUTORECODE variable. */
50 struct variable *src; /* Source variable. */
51 struct variable *dest; /* Target variable. */
52 struct hsh_table *items; /* Hash table of `freq's. */
55 /* AUTORECODE transformation. */
56 struct autorecode_trns
59 struct pool *owner; /* Contains AUTORECODE specs. */
60 struct arc_spec *arc; /* AUTORECODE specifications. */
61 int n_arc; /* Number of specifications. */
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;
70 /* Pool for allocation of hash table entries. */
71 static struct pool *hash_pool;
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);
84 /* Performs the AUTORECODE procedure. */
98 lex_match_id ("AUTORECODE");
99 lex_match_id ("VARIABLES");
101 if (!parse_variables (&default_dict, &v_src, &nv_src, PV_NO_DUPLICATE))
103 if (!lex_force_match_id ("INTO"))
106 if (!parse_DATA_LIST_vars (&n_dest, &nv_dest, PV_NONE))
108 if (nv_dest != nv_src)
110 msg (SE, _("Number of source variables (%d) does not match number "
111 "of target variables (%d)."), nv_src, nv_dest);
114 while (lex_match ('/'))
115 if (lex_match_id ("DESCENDING"))
117 else if (lex_match_id ("PRINT"))
121 lex_error (_("expecting end of command"));
125 for (i = 0; i < nv_dest; i++)
129 if (is_varname (n_dest[i]))
131 msg (SE, _("Target variable %s duplicates existing variable %s."),
132 n_dest[i], n_dest[i]);
135 for (j = 0; j < i; j++)
136 if (!strcmp (n_dest[i], n_dest[j]))
138 msg (SE, _("Duplicate variable name %s among target variables."),
144 hash_pool = pool_create ();
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]);
153 h_trans[i] = hsh_create (10, compare_numeric_value,
154 hash_numeric_value, NULL, NULL);
156 procedure (NULL, autorecode_proc_func, NULL);
158 for (i = 0; i < nv_dest; i++)
160 v_dest[i] = force_create_variable (&default_dict, n_dest[i], NUMERIC, 0);
174 for (i = 0; i < nv_src; i++)
175 hsh_destroy (h_trans[i]);
176 for (i = 0; i < nv_dest; i++)
183 /* AUTORECODE transformation. */
188 struct autorecode_trns *t;
189 struct pool *arc_pool;
192 arc_pool = pool_create ();
193 t = xmalloc (sizeof *t);
194 t->h.proc = autorecode_trns_proc;
195 t->h.free = autorecode_trns_free;
197 t->arc = pool_alloc (arc_pool, sizeof *t->arc * nv_src);
199 for (i = 0; i < nv_src; i++)
201 struct arc_spec *spec = &t->arc[i];
202 void **p = hsh_sort (h_trans[i]);
203 int count = hsh_count (h_trans[i]);
206 spec->src = v_src[i];
207 spec->dest = v_dest[i];
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]);
213 spec->items = hsh_create (2 * count, compare_numeric_value,
214 hash_numeric_value, NULL, NULL);
216 for (j = 0; *p; p++, j++)
218 struct arc_item *item = pool_alloc (arc_pool, sizeof *item);
219 union value *vp = *p;
221 if (v_src[i]->type == NUMERIC)
222 item->from.f = vp->f;
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);
229 hsh_destroy (h_trans[i]);
232 pool_destroy (hash_pool);
233 add_transformation ((struct trns_header *) t);
237 autorecode_trns_proc (struct trns_header * trns, struct ccase * c)
239 struct autorecode_trns *t = (struct autorecode_trns *) trns;
242 for (i = 0; i < t->n_arc; i++)
244 struct arc_spec *spec = &t->arc[i];
245 struct arc_item *item;
247 if (spec->src->type == NUMERIC)
248 item = hsh_force_find (spec->items, &c->data[spec->src->fv].f);
252 v.c = c->data[spec->src->fv].s;
253 item = hsh_force_find (spec->items, &v);
256 c->data[spec->dest->fv].f = item->to;
262 autorecode_trns_free (struct trns_header * trns)
264 struct autorecode_trns *t = (struct autorecode_trns *) trns;
267 for (i = 0; i < t->n_arc; i++)
268 hsh_destroy (t->arc[i].items);
269 pool_destroy (t->owner);
272 /* AUTORECODE procedure. */
275 compare_alpha_value (const void *a_, const void *b_, void *v_)
277 const union value *a = a_;
278 const union value *b = b_;
279 const struct variable *v = v_;
281 return memcmp (a->c, b->c, v->width);
285 hash_alpha_value (const void *a_, void *v_)
287 const union value *a = a_;
288 const struct variable *v = v_;
290 return hsh_hash_bytes (a->c, v->width);
294 compare_numeric_value (const void *a_, const void *b_, void *foo unused)
296 const union value *a = a_;
297 const union value *b = b_;
299 return a->f < b->f ? -1 : a->f > b->f;
303 hash_numeric_value (const void *a_, void *foo unused)
305 const union value *a = a_;
307 return hsh_hash_double (a->f);
311 autorecode_proc_func (struct ccase * c)
315 for (i = 0; i < nv_src; i++)
321 if (v_src[i]->type == NUMERIC)
323 v.f = c->data[v_src[i]->fv].f;
324 vpp = (union value **) hsh_probe (h_trans[i], &v);
327 vp = pool_alloc (hash_pool, sizeof (union value));
334 v.c = c->data[v_src[i]->fv].s;
335 vpp = (union value **) hsh_probe (h_trans[i], &v);
338 vp = pool_alloc (hash_pool, sizeof (union value));
339 vp->c = pool_strndup (hash_pool, v.c, v_src[i]->width);