Changed include paths to be explicitly specified in the #include directive.
[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 #include <stdlib.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/message.h>
38 #include "category.h"
39 #include "cat-routines.h"
40 #include <string.h>
41 #include "variable.h"
42
43 #define N_INITIAL_CATEGORIES 1
44
45 void
46 cat_stored_values_create (struct variable *v)
47 {
48   if (v->obs_vals == NULL)
49     {
50       v->obs_vals = xmalloc (sizeof (*v->obs_vals));
51       v->obs_vals->n_categories = 0;
52       v->obs_vals->n_allocated_categories = N_INITIAL_CATEGORIES;
53       v->obs_vals->vals =
54         xnmalloc (N_INITIAL_CATEGORIES, sizeof *v->obs_vals->vals);
55     }
56 }
57
58 void
59 cat_stored_values_destroy (struct variable *v)
60 {
61   assert (v != NULL);
62   if (v->obs_vals != NULL)
63     {
64       free (v->obs_vals);
65     }
66 }
67
68 /*
69   Which subscript corresponds to val?
70  */
71 size_t
72 cat_value_find (const struct variable *v, const union value *val)
73 {
74   size_t i;
75   const union value *candidate;
76
77   assert (val != NULL);
78   assert (v != NULL);
79   assert (v->obs_vals != NULL);
80   for (i = 0; i < v->obs_vals->n_categories; i++)
81     {
82       candidate = v->obs_vals->vals + i;
83       assert (candidate != NULL);
84       if (!compare_values (candidate, val, v->width))
85         {
86           return i;
87         }
88     }
89   return CAT_VALUE_NOT_FOUND;
90 }
91
92 /*
93    Add the new value unless it is already present.
94  */
95 void
96 cat_value_update (struct variable *v, const union value *val)
97 {
98   struct cat_vals *cv;
99
100   if (v->type == ALPHA)
101     {
102       assert (val != NULL);
103       assert (v != NULL);
104       cv = v->obs_vals;
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   assert (v->obs_vals != NULL);
124   if (s < v->obs_vals->n_categories)
125     {
126       return (v->obs_vals->vals + s);
127     }
128   else
129     {
130       return NULL;
131     }
132 }
133
134 /*
135   Return the number of categories of a categorical variable.
136  */
137 size_t 
138 cat_get_n_categories (const struct variable *v)
139 {
140   return v->obs_vals->n_categories;
141 }
142