Allow dump function to work with numeric variables
[pspp-builds.git] / src / math / categoricals.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20
21 #include "categoricals.h"
22
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>
29
30 #include <libpspp/str.h>
31
32 struct value_node
33 {
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 */
39 };
40
41
42 struct var_params
43 {
44   /* A map indexed by a union values */
45   struct hmap map;
46
47   int base_subscript;
48
49   /* The number of distinct values of this variable */
50   int n_cats;
51
52   /* A map of values indexed by subscript */
53   const struct value_node **reverse_value_map;
54 };
55
56
57 struct categoricals
58 {
59   const struct variable **vars;
60   size_t n_vars;
61
62   const struct variable *wv;
63
64   /* An array of var_params */
65   struct var_params *vp;
66
67   /* A map to enable the lookup of variables indexed by subscript */
68   int *reverse_variable_map;
69
70   size_t n_cats_total;
71
72   struct pool *pool;
73 };
74
75
76 void
77 categoricals_destroy ( struct categoricals *cat)
78 {
79   int i;
80   for (i = 0 ; i < cat->n_vars; ++i)
81     hmap_destroy (&cat->vp[i].map);
82
83   pool_destroy (cat->pool);
84   free (cat);
85 }
86
87
88 void
89 categoricals_dump (const struct categoricals *cat)
90 {
91   int v;
92
93   for (v = 0 ; v < cat->n_vars; ++v)
94     {
95       const struct var_params *vp = &cat->vp[v];
96       const struct hmap *m = &vp->map;
97       size_t width = var_get_width (cat->vars[v]);
98       struct hmap_node *node ;
99       int x;
100      
101       printf ("\n%s (%d):\n", var_get_name (cat->vars[v]), vp->base_subscript);
102
103       assert (vp->reverse_value_map);
104
105       printf ("Reverse map\n");
106       for (x = 0 ; x < vp->n_cats; ++x)
107         {
108           struct string s;
109           const struct value_node *vn = vp->reverse_value_map[x];
110           ds_init_empty (&s);
111           var_append_value_name (cat->vars[v], &vn->value, &s);
112           printf ("Value for %d is %s\n", x, ds_cstr(&s));
113           ds_destroy (&s);
114         }
115
116       printf ("\nForward map\n");
117       for (node = hmap_first (m); node; node = hmap_next (m, node))
118         {
119           struct string s;
120           ds_init_empty (&s);
121           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
122           var_append_value_name (cat->vars[v], &vn->value, &s);
123           printf ("Value: %s; Index %d; CC %g\n",
124                   ds_cstr (&s),
125                   vn->subscript, vn->cc);
126           ds_destroy (&s);
127         }
128     }
129 }
130
131
132
133 static struct value_node *
134 lookup_value (const struct hmap *map, const struct variable *var, const union value *val)
135 {
136   struct value_node *foo;
137   unsigned int width = var_get_width (var);
138   size_t hash = value_hash (val, width, 0);
139
140   HMAP_FOR_EACH_WITH_HASH (foo, struct value_node, node, hash, map)
141     {
142       if (value_equal (val, &foo->value, width))
143         break;
144
145       fprintf (stderr, "Warning: Hash table collision\n");
146     }
147
148   return foo;
149 }
150
151
152
153 struct categoricals *
154 categoricals_create (const struct variable **v, size_t n_vars, const struct variable *wv)
155 {
156   size_t i;
157   struct categoricals *cat = xmalloc (sizeof *cat);
158   
159   cat->vars = v;
160   cat->n_vars = n_vars;
161   cat->wv = wv;
162   cat->n_cats_total = 0;
163   cat->reverse_variable_map = NULL;
164   cat->pool = pool_create ();
165
166   cat->vp = pool_calloc (cat->pool, n_vars, sizeof *cat->vp);
167
168   for (i = 0 ; i < cat->n_vars; ++i)
169     hmap_init (&cat->vp[i].map);
170
171   return cat;
172 }
173
174
175
176 void
177 categoricals_update (struct categoricals *cat, const struct ccase *c)
178 {
179   size_t i;
180   
181   const double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0;
182
183   assert (NULL == cat->reverse_variable_map);
184
185   for (i = 0 ; i < cat->n_vars; ++i)
186     {
187       unsigned int width = var_get_width (cat->vars[i]);
188       const union value *val = case_data (c, cat->vars[i]);
189       size_t hash = value_hash (val, width, 0);
190
191       struct value_node  *node = lookup_value (&cat->vp[i].map, cat->vars[i], val);
192       if ( NULL == node)
193         {
194           node = pool_malloc (cat->pool, sizeof *node);
195
196           value_init (&node->value, width);
197           value_copy (&node->value, val, width);
198           node->cc = 0.0;
199
200           hmap_insert (&cat->vp[i].map, &node->node,  hash);
201           cat->n_cats_total ++;
202           node->subscript = cat->vp[i].n_cats++ ;
203         }
204
205       node->cc += weight;
206     }
207 }
208
209 /* Return the number of categories (distinct values) for variable N */
210 size_t
211 categoricals_n_count (const struct categoricals *cat, size_t n)
212 {
213   return hmap_count (&cat->vp[n].map);
214 }
215
216
217 /* Return the index for value VAL in the Nth variable */
218 int
219 categoricals_index (const struct categoricals *cat, size_t n, const union value *val)
220 {
221   struct value_node *vn = lookup_value (&cat->vp[n].map, cat->vars[n], val);
222
223   if ( vn == NULL)
224     return -1;
225
226   return vn->subscript;
227 }
228
229
230 /* Return the total number of categories */
231 size_t
232 categoricals_total (const struct categoricals *cat)
233 {
234   return cat->n_cats_total;
235 }
236
237
238 /* This function must be called *before* any call to categoricals_get_*_by subscript an
239  *after* all calls to categoricals_update */
240 void
241 categoricals_done (struct categoricals *cat)
242 {
243   /* Implementation Note: Whilst this function is O(n) in cat->n_cats_total, in most
244      uses it will be more efficient that using a tree based structure, since it
245      is called only once, and means that subsequent lookups will be O(1).
246
247      1 call of O(n) + 10^9 calls of O(1) is better than 10^9 calls of O(log n).
248   */
249   int v;
250   int idx = 0;
251   cat->reverse_variable_map = pool_calloc (cat->pool, cat->n_cats_total, sizeof *cat->reverse_variable_map);
252   
253   for (v = 0 ; v < cat->n_vars; ++v)
254     {
255       int i;
256       struct var_params *vp = &cat->vp[v];
257       int n_cats_total = categoricals_n_count (cat, v);
258       struct hmap_node *node ;
259
260       vp->reverse_value_map = pool_calloc (cat->pool, n_cats_total, sizeof *vp->reverse_value_map);
261
262       vp->base_subscript = idx;
263
264       for (node = hmap_first (&vp->map); node; node = hmap_next (&vp->map, node))
265         {
266           const struct value_node *vn = HMAP_DATA (node, struct value_node, node);
267           vp->reverse_value_map[vn->subscript] = vn;
268         }
269
270       for (i = 0; i < vp->n_cats; ++i)
271         cat->reverse_variable_map[idx++] = v;
272     }
273 }
274
275
276
277 /* Return the categorical variable corresponding to SUBSCRIPT */
278 const struct variable *
279 categoricals_get_variable_by_subscript (const struct categoricals *cat, int subscript)
280 {
281   int index;
282
283   assert (cat->reverse_variable_map);
284   
285   index = cat->reverse_variable_map[subscript];
286
287   return cat->vars[index];
288 }
289
290
291 /* Return the value corresponding to SUBSCRIPT */
292 const union value *
293 categoricals_get_value_by_subscript (const struct categoricals *cat, int subscript)
294 {
295   int vindex = cat->reverse_variable_map[subscript];
296   const struct var_params *vp = &cat->vp[vindex];
297   const struct value_node *vn = vp->reverse_value_map [subscript - vp->base_subscript];
298
299   return &vn->value;
300 }
301
302
303 /* Returns unity if the value in case C at SUBSCRIPT is equal to the category
304    for that subscript */
305 double
306 categoricals_get_binary_by_subscript (const struct categoricals *cat, int subscript,
307                                       const struct ccase *c)
308 {
309   const struct variable *var = categoricals_get_variable_by_subscript (cat, subscript);
310   int width = var_get_width (var);
311
312   const union value *val = case_data (c, var);
313
314   return value_equal (val, categoricals_get_value_by_subscript (cat, subscript), width);
315 }