b0424d9f65c054df4631897702d33ab4eae1c57a
[pspp-builds.git] / src / data / category.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2005 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 /*
18   Functions and data structures to store values of a categorical
19   variable, and to recode those values into binary vectors.
20
21   For some statistical models, it is necessary to change each value
22   of a categorical variable to a vector with binary entries. These
23   vectors are then stored as sub-rows within a matrix during
24   model-fitting. For example, we need functions and data strucutres to map a
25   value, say 'a', of a variable named 'cat_var', to a vector, say (0
26   1 0 0 0), and vice versa.  We also need to be able to map the
27   vector back to the value 'a', and if the vector is a sub-row of a
28   matrix, we need to know which sub-row corresponds to the variable
29   'cat_var'.
30 */
31 #include <config.h>
32
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <libpspp/message.h>
38 #include "category.h"
39 #include "value.h"
40 #include "variable.h"
41
42 #include "xalloc.h"
43
44 #define CAT_VALUE_NOT_FOUND -2
45
46 #define N_INITIAL_CATEGORIES 1
47
48 /*
49   This structure contains the observed values of a
50   categorical variable.
51  */
52 struct cat_vals
53 {
54   union value *vals;
55   size_t n_categories;
56   size_t n_allocated_categories;        /* This is used only during
57                                            initialization to keep
58                                            track of the number of
59                                            values stored.
60                                          */
61 };
62
63 void
64 cat_stored_values_create (const struct variable *v)
65 {
66   if (!var_has_obs_vals (v))
67     {
68       struct cat_vals *obs_vals = xmalloc (sizeof *obs_vals);
69
70       obs_vals->n_categories = 0;
71       obs_vals->n_allocated_categories = N_INITIAL_CATEGORIES;
72       obs_vals->vals = xnmalloc (N_INITIAL_CATEGORIES, sizeof *obs_vals->vals);
73       var_set_obs_vals (v, obs_vals);
74     }
75 }
76
77 void
78 cat_stored_values_destroy (struct cat_vals *obs_vals)
79 {
80   if (obs_vals != NULL)
81     {
82       if (obs_vals->n_allocated_categories > 0)
83         free (obs_vals->vals);
84       free (obs_vals);
85     }
86 }
87
88 /*
89   Which subscript corresponds to val?
90  */
91 size_t
92 cat_value_find (const struct variable *v, const union value *val)
93 {
94   struct cat_vals *obs_vals = var_get_obs_vals (v);
95   size_t i;
96   const union value *candidate;
97
98   for (i = 0; i < obs_vals->n_categories; i++)
99     {
100       candidate = obs_vals->vals + i;
101       assert (candidate != NULL);
102       if (!compare_values (candidate, val, var_get_width (v)))
103         {
104           return i;
105         }
106     }
107   return CAT_VALUE_NOT_FOUND;
108 }
109
110 /*
111    Add the new value unless it is already present.
112  */
113 void
114 cat_value_update (const struct variable *v, const union value *val)
115 {
116   if (var_is_alpha (v))
117     {
118       struct cat_vals *cv = var_get_obs_vals (v);
119       if (cat_value_find (v, val) == CAT_VALUE_NOT_FOUND)
120         {
121           if (cv->n_categories >= cv->n_allocated_categories)
122             {
123               cv->n_allocated_categories *= 2;
124               cv->vals = xnrealloc (cv->vals,
125                                     cv->n_allocated_categories,
126                                     sizeof *cv->vals);
127             }
128           cv->vals[cv->n_categories] = *val;
129           cv->n_categories++;
130         }
131     }
132 }
133
134 const union value *
135 cat_subscript_to_value (const size_t s, const struct variable *v)
136 {
137   struct cat_vals *obs_vals = var_get_obs_vals (v);
138   return s < obs_vals->n_categories ? obs_vals->vals + s : NULL;
139 }
140
141 /*
142   Return the number of categories of a categorical variable.
143  */
144 size_t
145 cat_get_n_categories (const struct variable *v)
146 {
147   return var_get_obs_vals (v)->n_categories;
148 }
149