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 */
38 void *user_data; /* A pointer to data which the caller can store stuff */
40 int subscript; /* A zero based integer, unique within the variable.
41 Can be used as an index into an array */
47 /* A map indexed by a union values */
50 const struct variable *var;
54 /* The number of distinct values of this variable */
57 /* A map of values indexed by subscript */
58 const struct value_node **reverse_value_map;
60 /* Total of the weights of this variable */
67 /* The weight variable */
68 const struct variable *wv;
71 /* An array of var_params */
72 struct var_params *vp;
74 /* The size of VP. (ie, the number of variables involved.) */
77 /* The number of categorical variables which contain entries.
78 In the absence of missing values, this will be equal to N_VP */
81 /* A map to enable the lookup of variables indexed by subscript */
82 int *reverse_variable_map;
88 /* Missing values to be excluded */
89 enum mv_class exclude;
91 /* Function to be called on each update */
94 /* Function specified by the caller to create user_data */
95 user_data_create_func *user_data_create;
97 /* Auxilliary data to be passed to update and user_data_create_func*/
104 categoricals_destroy ( struct categoricals *cat)
109 for (i = 0 ; i < cat->n_vp; ++i)
110 hmap_destroy (&cat->vp[i].map);
112 pool_destroy (cat->pool);
120 categoricals_dump (const struct categoricals *cat)
124 for (v = 0 ; v < cat->n_vp; ++v)
126 const struct var_params *vp = &cat->vp[v];
127 const struct hmap *m = &vp->map;
128 struct hmap_node *node ;
131 printf ("\n%s (%d) CC=%g n_cats=%d:\n",
132 var_get_name (vp->var), vp->base_subscript, vp->cc, vp->n_cats);
134 printf ("Reverse map\n");
135 for (x = 0 ; x < vp->n_cats; ++x)
138 const struct value_node *vn = vp->reverse_value_map[x];
140 var_append_value_name (vp->var, &vn->value, &s);
141 printf ("Value for %d is %s\n", x, ds_cstr(&s));
145 printf ("\nForward map\n");
146 for (node = hmap_first (m); node; node = hmap_next (m, node))
149 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
151 var_append_value_name (vp->var, &vn->value, &s);
152 printf ("Value: %s; Index %d; CC %g\n",
154 vn->subscript, vn->cc);
159 assert (cat->n_vars <= cat->n_vp);
162 printf ("Number of categorical variables: %d\n", cat->n_vp);
163 printf ("Number of non-empty categorical variables: %d\n", cat->n_vars);
164 printf ("Total number of categories: %d\n", cat->n_cats_total);
166 printf ("\nReverse variable map:\n");
168 for (v = 0 ; v < cat->n_cats_total; ++v)
169 printf ("%d ", cat->reverse_variable_map[v]);
176 static struct value_node *
177 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
179 struct value_node *foo;
180 unsigned int width = var_get_width (var);
181 size_t hash = value_hash (val, width, 0);
183 HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
185 if (value_equal (val, &foo->value, width))
188 fprintf (stderr, "Warning: Hash table collision\n");
195 struct categoricals *
196 categoricals_create (const struct variable *const *v, size_t n_vars,
197 const struct variable *wv, enum mv_class exclude,
198 user_data_create_func *udf,
199 update_func *update, void *aux1, void *aux2
203 struct categoricals *cat = xmalloc (sizeof *cat);
207 cat->n_cats_total = 0;
209 cat->reverse_variable_map = NULL;
210 cat->pool = pool_create ();
211 cat->exclude = exclude;
212 cat->update = update;
213 cat->user_data_create = udf;
219 cat->vp = pool_calloc (cat->pool, cat->n_vp, sizeof *cat->vp);
221 for (i = 0 ; i < cat->n_vp; ++i)
223 hmap_init (&cat->vp[i].map);
224 cat->vp[i].var = v[i];
233 categoricals_update (struct categoricals *cat, const struct ccase *c)
237 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
239 assert (NULL == cat->reverse_variable_map);
241 for (i = 0 ; i < cat->n_vp; ++i)
243 const struct variable *var = cat->vp[i].var;
244 unsigned int width = var_get_width (var);
245 const union value *val = case_data (c, var);
247 struct value_node *node ;
249 if ( var_is_value_missing (var, val, cat->exclude))
252 hash = value_hash (val, width, 0);
253 node = lookup_value (&cat->vp[i].map, var, val);
257 node = pool_malloc (cat->pool, sizeof *node);
259 value_init (&node->value, width);
260 value_copy (&node->value, val, width);
263 hmap_insert (&cat->vp[i].map, &node->node, hash);
266 if ( 0 == cat->vp[i].n_cats)
269 node->subscript = cat->vp[i].n_cats++ ;
271 if (cat->user_data_create)
272 node->user_data = cat->user_data_create (cat->aux1, cat->aux2);
276 cat->vp[i].cc += weight;
279 cat->update (node->user_data, cat->exclude, cat->wv, var, c, cat->aux1, cat->aux2);
283 /* Return the number of categories (distinct values) for variable N */
285 categoricals_n_count (const struct categoricals *cat, size_t n)
287 return hmap_count (&cat->vp[n].map);
291 /* Return the total number of categories */
293 categoricals_total (const struct categoricals *cat)
295 return cat->n_cats_total;
299 /* This function must be called *before* any call to categoricals_get_*_by subscript an
300 *after* all calls to categoricals_update */
302 categoricals_done (struct categoricals *cat)
304 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
305 uses it will be more efficient that using a tree based structure, since it
306 is called only once, and means that subsequent lookups will be O(1).
308 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
312 cat->reverse_variable_map = pool_calloc (cat->pool,
314 sizeof *cat->reverse_variable_map);
316 for (v = 0 ; v < cat->n_vp; ++v)
319 struct var_params *vp = &cat->vp[v];
320 int n_cats_total = categoricals_n_count (cat, v);
321 struct hmap_node *node ;
323 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
325 vp->base_subscript = idx;
327 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
329 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
330 vp->reverse_value_map[vn->subscript] = vn;
333 /* Populate the reverse variable map.
335 for (i = 0; i < vp->n_cats; ++i)
336 cat->reverse_variable_map[idx++] = v;
339 assert (cat->n_vars <= cat->n_vp);
341 // categoricals_dump (cat);
346 reverse_variable_lookup (const struct categoricals *cat, int subscript)
348 assert (cat->reverse_variable_map);
349 assert (subscript >= 0);
350 assert (subscript < cat->n_cats_total);
352 return cat->reverse_variable_map[subscript];
356 /* Return the categorical variable corresponding to SUBSCRIPT */
357 const struct variable *
358 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
360 int index = reverse_variable_lookup (cat, subscript);
362 return cat->vp[index].var;
365 /* Return the value corresponding to SUBSCRIPT */
367 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
369 int vindex = reverse_variable_lookup (cat, subscript);
370 const struct var_params *vp = &cat->vp[vindex];
371 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
378 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
380 int vindex = reverse_variable_lookup (cat, subscript);
381 const struct var_params *vp = &cat->vp[vindex];
387 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
389 int vindex = reverse_variable_lookup (cat, subscript);
390 const struct var_params *vp = &cat->vp[vindex];
392 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
397 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
398 for that subscript */
400 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
401 const struct ccase *c)
403 const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
404 int width = var_get_width (var);
406 const union value *val = case_data (c, var);
408 return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);
413 categoricals_get_n_variables (const struct categoricals *cat)
421 categoricals_get_user_data_by_subscript (const struct categoricals *cat, int subscript)
423 int vindex = reverse_variable_lookup (cat, subscript);
424 const struct var_params *vp = &cat->vp[vindex];
426 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
427 return vn->user_data;