1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011 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/>. */
19 #include "math/categoricals.h"
23 #include "data/case.h"
24 #include "data/value.h"
25 #include "data/variable.h"
26 #include "libpspp/array.h"
27 #include "libpspp/hmap.h"
28 #include "libpspp/pool.h"
29 #include "libpspp/str.h"
31 #include "gl/xalloc.h"
35 struct hmap_node node; /* Node in hash map. */
36 union value value; /* The value being labeled. */
37 double cc; /* The total of the weights of cases with this value */
39 void *user_data; /* A pointer to data which the caller can store stuff */
41 int subscript; /* A zero based integer, unique within the variable.
42 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 */
65 /* Comparison function to sort the reverse_value_map in ascending order */
67 compare_value_node (const void *vn1_, const void *vn2_, const void *aux)
69 const struct value_node * const *vn1 = vn1_;
70 const struct value_node * const *vn2 = vn2_;
71 const struct var_params *vp = aux;
73 return value_compare_3way (&(*vn1)->value, &(*vn2)->value, var_get_width (vp->var));
79 /* The weight variable */
80 const struct variable *wv;
83 /* An array of var_params */
84 struct var_params *vp;
86 /* The size of VP. (ie, the number of variables involved.) */
89 /* The number of categorical variables which contain entries.
90 In the absence of missing values, this will be equal to N_VP */
93 /* A map to enable the lookup of variables indexed by subscript */
94 int *reverse_variable_map;
100 /* Missing values to be excluded */
101 enum mv_class exclude;
103 /* Function to be called on each update */
106 /* Function specified by the caller to create user_data */
107 user_data_create_func *user_data_create;
109 /* Auxilliary data to be passed to update and user_data_create_func*/
116 categoricals_destroy ( struct categoricals *cat)
121 for (i = 0 ; i < cat->n_vp; ++i)
122 hmap_destroy (&cat->vp[i].map);
124 pool_destroy (cat->pool);
132 categoricals_dump (const struct categoricals *cat)
136 for (v = 0 ; v < cat->n_vp; ++v)
138 const struct var_params *vp = &cat->vp[v];
139 const struct hmap *m = &vp->map;
140 struct hmap_node *node ;
143 printf ("\n%s (%d) CC=%g n_cats=%d:\n",
144 var_get_name (vp->var), vp->base_subscript, vp->cc, vp->n_cats);
146 printf ("Reverse map\n");
147 for (x = 0 ; x < vp->n_cats; ++x)
150 const struct value_node *vn = vp->reverse_value_map[x];
152 var_append_value_name (vp->var, &vn->value, &s);
153 printf ("Value for %d is %s\n", x, ds_cstr(&s));
157 printf ("\nForward map\n");
158 for (node = hmap_first (m); node; node = hmap_next (m, node))
161 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
163 var_append_value_name (vp->var, &vn->value, &s);
164 printf ("Value: %s; Index %d; CC %g\n",
166 vn->subscript, vn->cc);
171 assert (cat->n_vars <= cat->n_vp);
174 printf ("Number of categorical variables: %d\n", cat->n_vp);
175 printf ("Number of non-empty categorical variables: %d\n", cat->n_vars);
176 printf ("Total number of categories: %d\n", cat->n_cats_total);
178 printf ("\nReverse variable map:\n");
180 for (v = 0 ; v < cat->n_cats_total; ++v)
181 printf ("%d ", cat->reverse_variable_map[v]);
188 static struct value_node *
189 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
191 struct value_node *foo;
192 unsigned int width = var_get_width (var);
193 size_t hash = value_hash (val, width, 0);
195 HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
197 if (value_equal (val, &foo->value, width))
200 fprintf (stderr, "Warning: Hash table collision\n");
207 struct categoricals *
208 categoricals_create (const struct variable *const *v, size_t n_vars,
209 const struct variable *wv, enum mv_class exclude,
210 user_data_create_func *udf,
211 update_func *update, void *aux1, void *aux2
215 struct categoricals *cat = xmalloc (sizeof *cat);
219 cat->n_cats_total = 0;
221 cat->reverse_variable_map = NULL;
222 cat->pool = pool_create ();
223 cat->exclude = exclude;
224 cat->update = update;
225 cat->user_data_create = udf;
231 cat->vp = pool_calloc (cat->pool, cat->n_vp, sizeof *cat->vp);
233 for (i = 0 ; i < cat->n_vp; ++i)
235 hmap_init (&cat->vp[i].map);
236 cat->vp[i].var = v[i];
245 categoricals_update (struct categoricals *cat, const struct ccase *c)
249 const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
251 assert (NULL == cat->reverse_variable_map);
253 for (i = 0 ; i < cat->n_vp; ++i)
255 const struct variable *var = cat->vp[i].var;
256 unsigned int width = var_get_width (var);
257 const union value *val = case_data (c, var);
259 struct value_node *node ;
261 if ( var_is_value_missing (var, val, cat->exclude))
264 hash = value_hash (val, width, 0);
265 node = lookup_value (&cat->vp[i].map, var, val);
269 node = pool_malloc (cat->pool, sizeof *node);
271 value_init (&node->value, width);
272 value_copy (&node->value, val, width);
275 hmap_insert (&cat->vp[i].map, &node->node, hash);
278 if ( 0 == cat->vp[i].n_cats)
281 node->subscript = cat->vp[i].n_cats++ ;
283 if (cat->user_data_create)
284 node->user_data = cat->user_data_create (cat->aux1, cat->aux2);
288 cat->vp[i].cc += weight;
291 cat->update (node->user_data, cat->exclude, cat->wv, var, c, cat->aux1, cat->aux2);
295 /* Return the number of categories (distinct values) for variable N */
297 categoricals_n_count (const struct categoricals *cat, size_t n)
299 return hmap_count (&cat->vp[n].map);
303 /* Return the total number of categories */
305 categoricals_total (const struct categoricals *cat)
307 return cat->n_cats_total;
311 /* This function must be called *before* any call to categoricals_get_*_by subscript an
312 *after* all calls to categoricals_update */
314 categoricals_done (const struct categoricals *cat_)
316 /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
317 uses it will be more efficient that using a tree based structure, since it
318 is called only once, and means that subsequent lookups will be O(1).
320 1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
322 struct categoricals *cat = CONST_CAST (struct categoricals *, cat_);
325 cat->reverse_variable_map = pool_calloc (cat->pool,
327 sizeof *cat->reverse_variable_map);
329 for (v = 0 ; v < cat->n_vp; ++v)
332 struct var_params *vp = &cat->vp[v];
333 int n_cats_total = categoricals_n_count (cat, v);
334 struct hmap_node *node ;
336 vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
338 vp->base_subscript = idx;
340 for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
342 const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
343 vp->reverse_value_map[vn->subscript] = vn;
346 /* For some purposes (eg CONTRASTS in ONEWAY) the values need to be sorted */
347 sort (vp->reverse_value_map, vp->n_cats, sizeof (const struct value_node *),
348 compare_value_node, vp);
350 /* Populate the reverse variable map.
352 for (i = 0; i < vp->n_cats; ++i)
353 cat->reverse_variable_map[idx++] = v;
356 assert (cat->n_vars <= cat->n_vp);
362 reverse_variable_lookup (const struct categoricals *cat, int subscript)
364 assert (cat->reverse_variable_map);
365 assert (subscript >= 0);
366 assert (subscript < cat->n_cats_total);
368 return cat->reverse_variable_map[subscript];
372 /* Return the categorical variable corresponding to SUBSCRIPT */
373 const struct variable *
374 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
376 int index = reverse_variable_lookup (cat, subscript);
378 return cat->vp[index].var;
381 /* Return the value corresponding to SUBSCRIPT */
383 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
385 int vindex = reverse_variable_lookup (cat, subscript);
386 const struct var_params *vp = &cat->vp[vindex];
387 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
394 categoricals_get_weight_by_subscript (const struct categoricals *cat, int subscript)
396 int vindex = reverse_variable_lookup (cat, subscript);
397 const struct var_params *vp = &cat->vp[vindex];
403 categoricals_get_sum_by_subscript (const struct categoricals *cat, int subscript)
405 int vindex = reverse_variable_lookup (cat, subscript);
406 const struct var_params *vp = &cat->vp[vindex];
408 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
413 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
414 for that subscript */
416 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
417 const struct ccase *c)
419 const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
420 int width = var_get_width (var);
422 const union value *val = case_data (c, var);
424 return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);
429 categoricals_get_n_variables (const struct categoricals *cat)
437 categoricals_get_user_data_by_subscript (const struct categoricals *cat, int subscript)
439 int vindex = reverse_variable_lookup (cat, subscript);
440 const struct var_params *vp = &cat->vp[vindex];
442 const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
443 return vn->user_data;