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>
30 #include <libpspp/str.h>
34 struct hmap_node node; /* Node in hash map. */
35 union value value; /* The value being labeled. */
36 double cc; /* The total of the weights of cases with this value */
37 int subscript; /* A zero based integer, unique within the variable.
38 Can be used as an index into an array */
44 /* A map indexed by a union values */
47 const struct variable *var;
51 /* The number of distinct values of this variable */
54 /* A map of values indexed by subscript */
55 const struct value_node **reverse_value_map;
57 /* Total of the weights of this variable */
64 const struct variable *wv;
68 /* An array of var_params */
69 struct var_params *vp;
71 /* A map to enable the lookup of variables indexed by subscript */
72 int *reverse_variable_map;
81 categoricals_destroy ( struct categoricals *cat)
84 for (i = 0 ; i < cat->n_vars; ++i)
85 hmap_destroy (&cat->vp[i].map);
87 pool_destroy (cat->pool);
93 categoricals_dump (const struct categoricals *cat)
97 for (v = 0 ; v < cat->n_vars; ++v)
99 const struct var_params *vp = &cat->vp[v];
100 const struct hmap *m = &vp->map;
101 // size_t width = var_get_width (vp->var);
102 struct hmap_node *node ;
105 printf ("\n%s (%d) CC=%g:\n", var_get_name (vp->var), vp->base_subscript, vp->cc);
107 assert (vp->reverse_value_map);
109 printf ("Reverse map\n");
110 for (x = 0 ; x < vp->n_cats; ++x)
113 const struct value_node *vn = vp->reverse_value_map[x];
115 var_append_value_name (vp->var, &vn->value, &s);
116 printf ("Value for %d is %s\n", x, ds_cstr(&s));
120 printf ("\nForward map\n");
121 for (node = hmap_first (m); node; node = hmap_next (m, node))
125 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
126 var_append_value_name (vp->var, &vn->value, &s);
127 printf ("Value: %s; Index %d; CC %g\n",
129 vn->subscript, vn->cc);
137 static struct value_node *
138 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
140 struct value_node *foo;
141 unsigned int width = var_get_width (var);
142 size_t hash = value_hash (val, width, 0);
144 HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
146 if (value_equal (val, &foo->value, width))
149 fprintf (stderr, "Warning: Hash table collision\n");
157 struct categoricals *
158 categoricals_create (const struct variable **v, size_t n_vars, const struct variable *wv)
161 struct categoricals *cat = xmalloc (sizeof *cat);
163 cat->n_vars = n_vars;
165 cat->n_cats_total = 0;
166 cat->reverse_variable_map = NULL;
167 cat->pool = pool_create ();
169 cat->vp = pool_calloc (cat->pool, n_vars, sizeof *cat->vp);
171 for (i = 0 ; i < cat->n_vars; ++i)
173 hmap_init (&cat->vp[i].map);
174 cat->vp[i].var = v[i];
183 categoricals_update (struct categoricals *cat, const struct ccase *c)
187 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
189 assert (NULL == cat->reverse_variable_map);
191 for (i = 0 ; i < cat->n_vars; ++i)
193 const struct variable *var = cat->vp[i].var;
194 unsigned int width = var_get_width (var);
195 const union value *val = case_data (c, var);
196 size_t hash = value_hash (val, width, 0);
198 struct value_node *node = lookup_value (&cat->vp[i].map, var, val);
203 node = pool_malloc (cat->pool, sizeof *node);
205 value_init (&node->value, width);
206 value_copy (&node->value, val, width);
209 hmap_insert (&cat->vp[i].map, &node->node, hash);
210 cat->n_cats_total ++;
211 node->subscript = cat->vp[i].n_cats++ ;
215 cat->vp[i].cc += weight;
219 /* Return the number of categories (distinct values) for variable N */
221 categoricals_n_count (const struct categoricals *cat, size_t n)
223 return hmap_count (&cat->vp[n].map);
227 /* Return the index for value VAL in the Nth variable */
229 categoricals_index (const struct categoricals *cat, size_t n, const union value *val)
231 struct value_node *vn = lookup_value (&cat->vp[n].map, cat->vp[n].var, val);
236 return vn->subscript;
240 /* Return the total number of categories */
242 categoricals_total (const struct categoricals *cat)
244 return cat->n_cats_total;
248 /* This function must be called *before* any call to categoricals_get_*_by subscript an
249 *after* all calls to categoricals_update */
251 categoricals_done (struct categoricals *cat)
253 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
254 uses it will be more efficient that using a tree based structure, since it
255 is called only once, and means that subsequent lookups will be O(1).
257 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
261 cat->reverse_variable_map = pool_calloc (cat->pool, cat->n_cats_total, sizeof *cat->reverse_variable_map);
263 for (v = 0 ; v < cat->n_vars; ++v)
266 struct var_params *vp = &cat->vp[v];
267 int n_cats_total = categoricals_n_count (cat, v);
268 struct hmap_node *node ;
270 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
272 vp->base_subscript = idx;
274 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
276 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
277 vp->reverse_value_map[vn->subscript] = vn;
280 for (i = 0; i < vp->n_cats; ++i)
281 cat->reverse_variable_map[idx++] = v;
287 /* Return the categorical variable corresponding to SUBSCRIPT */
288 const struct variable *
289 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
293 assert (cat->reverse_variable_map);
295 index = cat->reverse_variable_map[subscript];
297 return cat->vp[index].var;
301 /* Return the value corresponding to SUBSCRIPT */
303 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
305 int vindex = cat->reverse_variable_map[subscript];
306 const struct var_params *vp = &cat->vp[vindex];
307 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
313 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
314 for that subscript */
316 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
317 const struct ccase *c)
319 const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
320 int width = var_get_width (var);
322 const union value *val = case_data (c, var);
324 return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);