Changed a lot of non-const pointers to const.
[pspp-builds.git] / src / data / category.c
1 /* PSPP - binary encodings for categorical variables.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Written by Jason H Stover <jason@sakla.net>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 /*
21   Functions and data structures to store values of a categorical
22   variable, and to recode those values into binary vectors.
23
24   For some statistical models, it is necessary to change each value
25   of a categorical variable to a vector with binary entries. These
26   vectors are then stored as sub-rows within a matrix during
27   model-fitting. For example, we need functions and data strucutres to map a
28   value, say 'a', of a variable named 'cat_var', to a vector, say (0
29   1 0 0 0), and vice versa.  We also need to be able to map the
30   vector back to the value 'a', and if the vector is a sub-row of a
31   matrix, we need to know which sub-row corresponds to the variable
32   'cat_var'.
33 */
34 #include <config.h>
35
36 #include "category.h"
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include <libpspp/alloc.h>
43 #include <libpspp/message.h>
44 #include "cat-routines.h"
45 #include "value.h"
46 #include "variable.h"
47
48 #define N_INITIAL_CATEGORIES 1
49
50 void
51 cat_stored_values_create (const struct variable *v)
52 {
53   if (!var_has_obs_vals (v))
54     {
55       struct cat_vals *obs_vals = xmalloc (sizeof *obs_vals);
56       obs_vals->n_categories = 0;
57       obs_vals->n_allocated_categories = N_INITIAL_CATEGORIES;
58       obs_vals->vals = xnmalloc (N_INITIAL_CATEGORIES, sizeof *obs_vals->vals);
59       var_set_obs_vals (v, obs_vals);
60     }
61 }
62
63 void
64 cat_stored_values_destroy (struct cat_vals *obs_vals)
65 {
66   if (obs_vals != NULL) 
67     {
68       if (obs_vals->n_allocated_categories > 0)
69         free (obs_vals->vals);
70       free (obs_vals);
71     }
72 }
73
74 /*
75   Which subscript corresponds to val?
76  */
77 size_t
78 cat_value_find (const struct variable *v, const union value *val)
79 {
80   struct cat_vals *obs_vals = var_get_obs_vals (v);
81   size_t i;
82   const union value *candidate;
83
84   for (i = 0; i < obs_vals->n_categories; i++)
85     {
86       candidate = obs_vals->vals + i;
87       assert (candidate != NULL);
88       if (!compare_values (candidate, val, var_get_width (v)))
89         {
90           return i;
91         }
92     }
93   return CAT_VALUE_NOT_FOUND;
94 }
95
96 /*
97    Add the new value unless it is already present.
98  */
99 void
100 cat_value_update (const struct variable *v, const union value *val)
101 {
102   if (var_is_alpha (v))
103     {
104       struct cat_vals *cv = var_get_obs_vals (v);
105       if (cat_value_find (v, val) == CAT_VALUE_NOT_FOUND)
106         {
107           if (cv->n_categories >= cv->n_allocated_categories)
108             {
109               cv->n_allocated_categories *= 2;
110               cv->vals = xnrealloc (cv->vals,
111                                     cv->n_allocated_categories,
112                                     sizeof *cv->vals);
113             }
114           cv->vals[cv->n_categories] = *val;
115           cv->n_categories++;
116         }
117     }
118 }
119
120 union value *
121 cat_subscript_to_value (const size_t s, struct variable *v)
122 {
123   struct cat_vals *obs_vals = var_get_obs_vals (v);
124   return s < obs_vals->n_categories ? obs_vals->vals + s : NULL;
125 }
126
127 /*
128   Return the number of categories of a categorical variable.
129  */
130 size_t 
131 cat_get_n_categories (const struct variable *v)
132 {
133   return var_get_obs_vals (v)->n_categories;
134 }
135