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 /* The weight variable */
65 const struct variable *wv;
68 /* An array of var_params */
69 struct var_params *vp;
71 /* The size of VP. (ie, the number of variables involved.) */
74 /* The number of categorical variables which contain entries.
75 In the absence of missing values, this will be equal to N_VP */
78 /* A map to enable the lookup of variables indexed by subscript */
79 int *reverse_variable_map;
85 /* Missing values to be excluded */
86 enum mv_class exclude;
91 categoricals_destroy ( struct categoricals *cat)
96 for (i = 0 ; i < cat->n_vp; ++i)
97 hmap_destroy (&cat->vp[i].map);
99 pool_destroy (cat->pool);
107 categoricals_dump (const struct categoricals *cat)
111 for (v = 0 ; v < cat->n_vp; ++v)
113 const struct var_params *vp = &cat->vp[v];
114 const struct hmap *m = &vp->map;
115 struct hmap_node *node ;
118 printf ("\n%s (%d) CC=%g n_cats=%d:\n",
119 var_get_name (vp->var), vp->base_subscript, vp->cc, vp->n_cats);
121 printf ("Reverse map\n");
122 for (x = 0 ; x < vp->n_cats; ++x)
125 const struct value_node *vn = vp->reverse_value_map[x];
127 var_append_value_name (vp->var, &vn->value, &s);
128 printf ("Value for %d is %s\n", x, ds_cstr(&s));
132 printf ("\nForward map\n");
133 for (node = hmap_first (m); node; node = hmap_next (m, node))
136 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
138 var_append_value_name (vp->var, &vn->value, &s);
139 printf ("Value: %s; Index %d; CC %g\n",
141 vn->subscript, vn->cc);
146 assert (cat->n_vars <= cat->n_vp);
149 printf ("Number of categorical variables: %d\n", cat->n_vp);
150 printf ("Number of non-empty categorical variables: %d\n", cat->n_vars);
151 printf ("Total number of categories: %d\n", cat->n_cats_total);
153 printf ("\nReverse variable map:\n");
155 for (v = 0 ; v < cat->n_cats_total - cat->n_vars; ++v)
156 printf ("%d ", cat->reverse_variable_map[v]);
163 static struct value_node *
164 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
166 struct value_node *foo;
167 unsigned int width = var_get_width (var);
168 size_t hash = value_hash (val, width, 0);
170 HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
172 if (value_equal (val, &foo->value, width))
175 fprintf (stderr, "Warning: Hash table collision\n");
183 struct categoricals *
184 categoricals_create (const struct variable **v, size_t n_vars,
185 const struct variable *wv, enum mv_class exclude)
188 struct categoricals *cat = xmalloc (sizeof *cat);
192 cat->n_cats_total = 0;
194 cat->reverse_variable_map = NULL;
195 cat->pool = pool_create ();
196 cat->exclude = exclude;
198 cat->vp = pool_calloc (cat->pool, cat->n_vp, sizeof *cat->vp);
200 for (i = 0 ; i < cat->n_vp; ++i)
202 hmap_init (&cat->vp[i].map);
203 cat->vp[i].var = v[i];
212 categoricals_update (struct categoricals *cat, const struct ccase *c)
216 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
218 assert (NULL == cat->reverse_variable_map);
220 for (i = 0 ; i < cat->n_vp; ++i)
222 const struct variable *var = cat->vp[i].var;
223 unsigned int width = var_get_width (var);
224 const union value *val = case_data (c, var);
226 struct value_node *node ;
228 if ( var_is_value_missing (var, val, cat->exclude))
231 hash = value_hash (val, width, 0);
232 node = lookup_value (&cat->vp[i].map, var, val);
236 node = pool_malloc (cat->pool, sizeof *node);
238 value_init (&node->value, width);
239 value_copy (&node->value, val, width);
242 hmap_insert (&cat->vp[i].map, &node->node, hash);
245 if ( 0 == cat->vp[i].n_cats)
248 node->subscript = cat->vp[i].n_cats++ ;
252 cat->vp[i].cc += weight;
256 /* Return the number of categories (distinct values) for variable N */
258 categoricals_n_count (const struct categoricals *cat, size_t n)
260 return hmap_count (&cat->vp[n].map);
264 /* Return the total number of categories */
266 categoricals_total (const struct categoricals *cat)
268 return cat->n_cats_total;
272 /* This function must be called *before* any call to categoricals_get_*_by subscript an
273 *after* all calls to categoricals_update */
275 categoricals_done (struct categoricals *cat)
277 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
278 uses it will be more efficient that using a tree based structure, since it
279 is called only once, and means that subsequent lookups will be O(1).
281 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
285 cat->reverse_variable_map = pool_calloc (cat->pool,
286 cat->n_cats_total - cat->n_vars,
287 sizeof *cat->reverse_variable_map);
289 for (v = 0 ; v < cat->n_vp; ++v)
292 struct var_params *vp = &cat->vp[v];
293 int n_cats_total = categoricals_n_count (cat, v);
294 struct hmap_node *node ;
296 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
298 vp->base_subscript = idx;
300 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
302 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
303 vp->reverse_value_map[vn->subscript] = vn;
306 /* Populate the reverse variable map.
307 This implementation considers the first value of each categorical variable
308 as the basis. Therefore, this loop starts from 1 instead of 0 */
309 for (i = 1; i < vp->n_cats; ++i)
310 cat->reverse_variable_map[idx++] = v;
313 assert (cat->n_vars <= cat->n_vp);
318 reverse_variable_lookup (const struct categoricals *cat, int subscript)
320 assert (cat->reverse_variable_map);
321 assert (subscript >= 0);
322 assert (subscript < cat->n_cats_total - cat->n_vars);
324 return cat->reverse_variable_map[subscript];
328 /* Return the categorical variable corresponding to SUBSCRIPT */
329 const struct variable *
330 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
332 int index = reverse_variable_lookup (cat, subscript);
334 return cat->vp[index].var;
337 /* Return the value corresponding to SUBSCRIPT */
339 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
341 int vindex = reverse_variable_lookup (cat, subscript);
342 const struct var_params *vp = &cat->vp[vindex];
343 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
350 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
352 int vindex = reverse_variable_lookup (cat, subscript);
353 const struct var_params *vp = &cat->vp[vindex];
359 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
361 int vindex = reverse_variable_lookup (cat, subscript);
362 const struct var_params *vp = &cat->vp[vindex];
364 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
369 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
370 for that subscript */
372 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
373 const struct ccase *c)
375 const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
376 int width = var_get_width (var);
378 const union value *val = case_data (c, var);
380 return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);
385 categoricals_get_n_variables (const struct categoricals *cat)