1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "categoricals.h"
23 #include <gl/xalloc.h>
24 #include <data/variable.h>
25 #include <data/case.h>
26 #include <data/value.h>
27 #include <libpspp/hmap.h>
28 #include <libpspp/pool.h>
33 struct hmap_node node; /* Node in hash map. */
34 union value value; /* The value being labeled. */
35 double cc; /* The total of the weights of cases with this value */
36 int subscript; /* A zero based integer, unique within the variable.
37 Can be used as an index into an array */
43 /* A map indexed by a union values */
48 /* The number of distinct values of this variable */
51 /* A map of values indexed by subscript */
52 const struct value_node **reverse_value_map;
58 const struct variable **vars;
61 const struct variable *wv;
63 /* An array of var_params */
64 struct var_params *vp;
66 /* A map to enable the lookup of variables indexed by subscript */
67 int *reverse_variable_map;
76 categoricals_destroy ( struct categoricals *cat)
79 for (i = 0 ; i < cat->n_vars; ++i)
80 hmap_destroy (&cat->vp[i].map);
82 pool_destroy (cat->pool);
88 categoricals_dump (const struct categoricals *cat)
92 for (v = 0 ; v < cat->n_vars; ++v)
94 const struct var_params *vp = &cat->vp[v];
95 const struct hmap *m = &vp->map;
96 size_t width = var_get_width (cat->vars[v]);
97 struct hmap_node *node ;
100 printf ("\n%s (%d):\n", var_get_name (cat->vars[v]), vp->base_subscript);
102 assert (vp->reverse_value_map);
104 printf ("Reverse map\n");
105 for (x = 0 ; x < vp->n_cats; ++x)
107 const struct value_node *vn = vp->reverse_value_map[x];
108 printf ("Value for %d is %s\n", x, value_str (&vn->value, width));
111 printf ("\nForward map\n");
112 for (node = hmap_first (m); node; node = hmap_next (m, node))
114 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
115 printf ("Value: %s; Index %d; CC %g\n",
116 value_str (&vn->value, width), vn->subscript, vn->cc);
123 static struct value_node *
124 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
126 struct value_node *foo;
127 unsigned int width = var_get_width (var);
128 size_t hash = value_hash (val, width, 0);
130 HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
132 if (value_equal (val, &foo->value, width))
135 fprintf (stderr, "Warning: Hash table collision\n");
143 struct categoricals *
144 categoricals_create (const struct variable **v, size_t n_vars, const struct variable *wv)
147 struct categoricals *cat = xmalloc (sizeof *cat);
150 cat->n_vars = n_vars;
152 cat->n_cats_total = 0;
153 cat->reverse_variable_map = NULL;
154 cat->pool = pool_create ();
156 cat->vp = pool_calloc (cat->pool, n_vars, sizeof *cat->vp);
158 for (i = 0 ; i < cat->n_vars; ++i)
159 hmap_init (&cat->vp[i].map);
167 categoricals_update (struct categoricals *cat, const struct ccase *c)
171 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
173 assert (NULL == cat->reverse_variable_map);
175 for (i = 0 ; i < cat->n_vars; ++i)
177 unsigned int width = var_get_width (cat->vars[i]);
178 const union value *val = case_data (c, cat->vars[i]);
179 size_t hash = value_hash (val, width, 0);
181 struct value_node *node = lookup_value (&cat->vp[i].map, cat->vars[i], val);
184 node = pool_malloc (cat->pool, sizeof *node);
186 value_init (&node->value, width);
187 value_copy (&node->value, val, width);
190 hmap_insert (&cat->vp[i].map, &node->node, hash);
191 cat->n_cats_total ++;
192 node->subscript = cat->vp[i].n_cats++ ;
199 /* Return the number of categories (distinct values) for variable N */
201 categoricals_n_count (const struct categoricals *cat, size_t n)
203 return hmap_count (&cat->vp[n].map);
207 /* Return the index for value VAL in the Nth variable */
209 categoricals_index (const struct categoricals *cat, size_t n, const union value *val)
211 struct value_node *vn = lookup_value (&cat->vp[n].map, cat->vars[n], val);
216 return vn->subscript;
220 /* Return the total number of categories */
222 categoricals_total (const struct categoricals *cat)
224 return cat->n_cats_total;
228 /* This function must be called *before* any call to categoricals_get_*_by subscript an
229 *after* all calls to categoricals_update */
231 categoricals_done (struct categoricals *cat)
233 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
234 uses it will be more efficient that using a tree based structure, since it
235 is called only once, and means that subsequent lookups will be O(1).
237 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
241 cat->reverse_variable_map = pool_calloc (cat->pool, cat->n_cats_total, sizeof *cat->reverse_variable_map);
243 for (v = 0 ; v < cat->n_vars; ++v)
246 struct var_params *vp = &cat->vp[v];
247 int n_cats_total = categoricals_n_count (cat, v);
248 struct hmap_node *node ;
250 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
252 vp->base_subscript = idx;
254 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
256 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
257 vp->reverse_value_map[vn->subscript] = vn;
260 for (i = 0; i < vp->n_cats; ++i)
261 cat->reverse_variable_map[idx++] = v;
267 /* Return the categorical variable corresponding to SUBSCRIPT */
268 const struct variable *
269 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
273 assert (cat->reverse_variable_map);
275 index = cat->reverse_variable_map[subscript];
277 return cat->vars[index];
281 /* Return the value corresponding to SUBSCRIPT */
283 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
285 int vindex = cat->reverse_variable_map[subscript];
286 const struct var_params *vp = &cat->vp[vindex];
287 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
293 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
294 for that subscript */
296 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
297 const struct ccase *c)
299 const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
300 int width = var_get_width (var);
302 const union value *val = case_data (c, var);
304 return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);