1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "data/caseinit.h"
25 #include "data/case.h"
26 #include "data/dictionary.h"
27 #include "data/value.h"
28 #include "data/variable.h"
29 #include "libpspp/array.h"
30 #include "libpspp/assertion.h"
31 #include "libpspp/compiler.h"
33 #include "gl/xalloc.h"
35 /* Initializer list: a set of values to write to locations within
38 /* Binds a value with a place to put it. */
46 /* A set of values to initialize in a case. */
49 struct init_value *values;
53 /* A bitmap of the "left" status of variables. */
56 LEAVE_REINIT = 0x001, /* Reinitalize for every case. */
57 LEAVE_LEFT = 0x002 /* Keep the value from one case to the next. */
60 /* Initializes LIST as an empty initializer list. */
62 init_list_create (struct init_list *list)
68 /* Frees the storage associated with LIST. */
70 init_list_destroy (struct init_list *list)
72 struct init_value *iv;
74 for (iv = &list->values[0]; iv < &list->values[list->cnt]; iv++)
75 value_destroy (&iv->value, iv->width);
79 /* Clears LIST, making it an empty list. */
81 init_list_clear (struct init_list *list)
83 init_list_destroy (list);
84 init_list_create (list);
87 /* Compares `struct init_value's A and B by case_index and
88 returns a strcmp()-type result. */
90 compare_init_values (const void *a_, const void *b_, const void *aux UNUSED)
92 const struct init_value *a = a_;
93 const struct init_value *b = b_;
95 return a->case_index < b->case_index ? -1 : a->case_index > b->case_index;
98 /* Returns true if LIST includes CASE_INDEX, false otherwise. */
100 init_list_includes (const struct init_list *list, size_t case_index)
102 struct init_value value;
103 value.case_index = case_index;
104 return binary_search (list->values, list->cnt, sizeof *list->values,
105 &value, compare_init_values, NULL) != NULL;
108 /* Marks LIST to initialize the `union value's for the variables
109 in dictionary D that both (1) fall in the leave class or
110 classes designated by INCLUDE and (2) are not in EXCLUDE. */
112 init_list_mark (struct init_list *list, const struct init_list *exclude,
113 enum leave_class include, const struct dictionary *d)
115 size_t var_cnt = dict_get_var_cnt (d);
118 assert (list != exclude);
119 list->values = xnrealloc (list->values, list->cnt + dict_get_var_cnt (d),
120 sizeof *list->values);
121 for (i = 0; i < var_cnt; i++)
123 struct variable *v = dict_get_var (d, i);
124 size_t case_index = var_get_case_index (v);
125 struct init_value *iv;
127 /* Only include the correct class. */
128 if (!(include & (var_get_leave (v) ? LEAVE_LEFT : LEAVE_REINIT)))
131 /* Don't include those to be excluded. */
132 if (exclude != NULL && init_list_includes (exclude, case_index))
135 iv = &list->values[list->cnt++];
136 iv->case_index = case_index;
137 iv->width = var_get_width (v);
138 value_init (&iv->value, iv->width);
139 if (var_is_numeric (v) && var_get_leave (v))
142 value_set_missing (&iv->value, iv->width);
145 /* Drop duplicates. */
146 list->cnt = sort_unique (list->values, list->cnt, sizeof *list->values,
147 compare_init_values, NULL);
150 /* Initializes data in case C to the values in the initializer
153 init_list_init (const struct init_list *list, struct ccase *c)
155 const struct init_value *iv;
157 for (iv = &list->values[0]; iv < &list->values[list->cnt]; iv++)
158 value_copy (case_data_rw_idx (c, iv->case_index), &iv->value, iv->width);
161 /* Updates the values in the initializer LIST from the data in
164 init_list_update (const struct init_list *list, const struct ccase *c)
166 struct init_value *iv;
168 for (iv = &list->values[0]; iv < &list->values[list->cnt]; iv++)
169 value_copy (&iv->value, case_data_idx (c, iv->case_index), iv->width);
172 /* A case initializer. */
175 /* Values that do not need to be initialized by the
176 procedure, because they are initialized by the data
178 struct init_list preinited_values;
180 /* Values that need to be initialized to SYSMIS or spaces in
182 struct init_list reinit_values;
184 /* Values that need to be initialized to 0 or spaces in the
185 first case and thereafter retain their values from case to
187 struct init_list left_values;
190 /* Creates and returns a new case initializer. */
192 caseinit_create (void)
194 struct caseinit *ci = xmalloc (sizeof *ci);
195 init_list_create (&ci->preinited_values);
196 init_list_create (&ci->reinit_values);
197 init_list_create (&ci->left_values);
201 /* Clears the contents of case initializer CI. */
203 caseinit_clear (struct caseinit *ci)
205 init_list_clear (&ci->preinited_values);
206 init_list_clear (&ci->reinit_values);
207 init_list_clear (&ci->left_values);
210 /* Destroys case initializer CI. */
212 caseinit_destroy (struct caseinit *ci)
216 init_list_destroy (&ci->preinited_values);
217 init_list_destroy (&ci->reinit_values);
218 init_list_destroy (&ci->left_values);
223 /* Marks the variables from dictionary D in CI as being
224 initialized by the data source, so that the case initializer
225 need not initialize them itself. */
227 caseinit_mark_as_preinited (struct caseinit *ci, const struct dictionary *d)
229 init_list_mark (&ci->preinited_values, NULL, LEAVE_REINIT | LEAVE_LEFT, d);
232 /* Marks in CI the variables from dictionary D, except for any
233 variables that were already marked with
234 caseinit_mark_as_preinited, as needing initialization
235 according to their leave status. */
237 caseinit_mark_for_init (struct caseinit *ci, const struct dictionary *d)
239 init_list_mark (&ci->reinit_values, &ci->preinited_values, LEAVE_REINIT, d);
240 init_list_mark (&ci->left_values, &ci->preinited_values, LEAVE_LEFT, d);
243 /* Initializes variables in *C as described by CI.
244 C must not be shared. */
246 caseinit_init_vars (const struct caseinit *ci, struct ccase *c)
248 init_list_init (&ci->reinit_values, c);
249 init_list_init (&ci->left_values, c);
252 /* Updates the left vars in CI from the data in C, so that the
253 next call to caseinit_init_vars will store those values in the
256 caseinit_update_left_vars (struct caseinit *ci, const struct ccase *c)
258 init_list_update (&ci->left_values, c);