Fixed memory leak
[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
63   if (v->obs_vals != NULL)
64     {
65       if (v->obs_vals->n_allocated_categories > 0)
66         {
67           free (v->obs_vals->vals);
68           v->obs_vals->vals = NULL;
69         }
70       free (v->obs_vals);
71       v->obs_vals = NULL;
72     }
73 }
74
75 /*
76   Which subscript corresponds to val?
77  */
78 size_t
79 cat_value_find (const struct variable *v, const union value *val)
80 {
81   size_t i;
82   const union value *candidate;
83
84   assert (val != NULL);
85   assert (v != NULL);
86   assert (v->obs_vals != NULL);
87   for (i = 0; i < v->obs_vals->n_categories; i++)
88     {
89       candidate = v->obs_vals->vals + i;
90       assert (candidate != NULL);
91       if (!compare_values (candidate, val, v->width))
92         {
93           return i;
94         }
95     }
96   return CAT_VALUE_NOT_FOUND;
97 }
98
99 /*
100    Add the new value unless it is already present.
101  */
102 void
103 cat_value_update (struct variable *v, const union value *val)
104 {
105   struct cat_vals *cv;
106
107   if (v->type == ALPHA)
108     {
109       assert (val != NULL);
110       assert (v != NULL);
111       cv = v->obs_vals;
112       if (cat_value_find (v, val) == CAT_VALUE_NOT_FOUND)
113         {
114           if (cv->n_categories >= cv->n_allocated_categories)
115             {
116               cv->n_allocated_categories *= 2;
117               cv->vals = xnrealloc (cv->vals,
118                                     cv->n_allocated_categories,
119                                     sizeof *cv->vals);
120             }
121           cv->vals[cv->n_categories] = *val;
122           cv->n_categories++;
123         }
124     }
125 }
126
127 union value *
128 cat_subscript_to_value (const size_t s, struct variable *v)
129 {
130   assert (v->obs_vals != NULL);
131   if (s < v->obs_vals->n_categories)
132     {
133       return (v->obs_vals->vals + s);
134     }
135   else
136     {
137       return NULL;
138     }
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 v->obs_vals->n_categories;
148 }
149