1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
28 #ifdef GLOBAL_DEBUGGING
36 case_unshare (struct ccase *c)
41 assert (c->this == c);
42 assert (c->case_data != NULL);
43 assert (c->case_data->ref_cnt > 1);
47 case_create (c, c->case_data->value_cnt);
48 memcpy (c->case_data->values, cd->values,
49 sizeof *cd->values * cd->value_cnt);
53 case_size (size_t value_cnt)
55 return (offsetof (struct case_data, values)
56 + value_cnt * sizeof (union value));
59 #ifdef GLOBAL_DEBUGGING
61 case_nullify (struct ccase *c)
66 #endif /* GLOBAL_DEBUGGING */
68 #ifdef GLOBAL_DEBUGGING
70 case_is_null (const struct ccase *c)
72 return c->case_data == NULL;
74 #endif /* GLOBAL_DEBUGGING */
77 case_create (struct ccase *c, size_t value_cnt)
79 if (!case_try_create (c, value_cnt))
83 #ifdef GLOBAL_DEBUGGING
85 case_clone (struct ccase *clone, const struct ccase *orig)
87 assert (orig != NULL);
88 assert (orig->this == orig);
89 assert (orig->case_data != NULL);
90 assert (orig->case_data->ref_cnt > 0);
91 assert (clone != NULL);
98 orig->case_data->ref_cnt++;
100 #endif /* GLOBAL_DEBUGGING */
102 #ifdef GLOBAL_DEBUGGING
104 case_move (struct ccase *dst, struct ccase *src)
106 assert (src != NULL);
107 assert (src->this == src);
108 assert (src->case_data != NULL);
109 assert (src->case_data->ref_cnt > 0);
110 assert (dst != NULL);
116 #endif /* GLOBAL_DEBUGGING */
118 #ifdef GLOBAL_DEBUGGING
120 case_destroy (struct ccase *c)
122 struct case_data *cd;
125 assert (c->this == c);
128 if (cd != NULL && --cd->ref_cnt == 0)
130 memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
131 cd->value_cnt = 0xdeadbeef;
135 #endif /* GLOBAL_DEBUGGING */
138 case_try_create (struct ccase *c, size_t value_cnt)
140 c->case_data = malloc (case_size (value_cnt));
141 if (c->case_data != NULL)
143 #ifdef GLOBAL_DEBUGGING
146 c->case_data->value_cnt = value_cnt;
147 c->case_data->ref_cnt = 1;
152 #ifdef GLOBAL_DEBUGGING
160 case_try_clone (struct ccase *clone, const struct ccase *orig)
162 case_clone (clone, orig);
166 #ifdef GLOBAL_DEBUGGING
168 case_copy (struct ccase *dst, size_t dst_idx,
169 const struct ccase *src, size_t src_idx,
172 assert (dst != NULL);
173 assert (dst->this == dst);
174 assert (dst->case_data != NULL);
175 assert (dst->case_data->ref_cnt > 0);
176 assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
178 assert (src != NULL);
179 assert (src->this == src);
180 assert (src->case_data != NULL);
181 assert (src->case_data->ref_cnt > 0);
182 assert (src_idx + value_cnt <= dst->case_data->value_cnt);
184 if (dst->case_data->ref_cnt > 1)
186 if (dst->case_data != src->case_data || dst_idx != src_idx)
187 memmove (dst->case_data->values + dst_idx,
188 src->case_data->values + src_idx,
189 sizeof *dst->case_data->values * value_cnt);
191 #endif /* GLOBAL_DEBUGGING */
193 #ifdef GLOBAL_DEBUGGING
195 case_serial_size (size_t value_cnt)
197 return value_cnt * sizeof (union value);
199 #endif /* GLOBAL_DEBUGGING */
201 #ifdef GLOBAL_DEBUGGING
203 case_serialize (const struct ccase *c, void *output,
204 size_t output_size UNUSED)
207 assert (c->this == c);
208 assert (c->case_data != NULL);
209 assert (c->case_data->ref_cnt > 0);
210 assert (output_size == case_serial_size (c->case_data->value_cnt));
211 assert (output != NULL || output_size == 0);
213 memcpy (output, c->case_data->values,
214 case_serial_size (c->case_data->value_cnt));
216 #endif /* GLOBAL_DEBUGGING */
218 #ifdef GLOBAL_DEBUGGING
220 case_unserialize (struct ccase *c, const void *input,
221 size_t input_size UNUSED)
224 assert (c->this == c);
225 assert (c->case_data != NULL);
226 assert (c->case_data->ref_cnt > 0);
227 assert (input_size == case_serial_size (c->case_data->value_cnt));
228 assert (input != NULL || input_size == 0);
230 if (c->case_data->ref_cnt > 1)
232 memcpy (c->case_data->values, input,
233 case_serial_size (c->case_data->value_cnt));
235 #endif /* GLOBAL_DEBUGGING */
237 #ifdef GLOBAL_DEBUGGING
239 case_data (const struct ccase *c, size_t idx)
242 assert (c->this == c);
243 assert (c->case_data != NULL);
244 assert (c->case_data->ref_cnt > 0);
245 assert (idx < c->case_data->value_cnt);
247 return &c->case_data->values[idx];
249 #endif /* GLOBAL_DEBUGGING */
251 #ifdef GLOBAL_DEBUGGING
253 case_num (const struct ccase *c, size_t idx)
256 assert (c->this == c);
257 assert (c->case_data != NULL);
258 assert (c->case_data->ref_cnt > 0);
259 assert (idx < c->case_data->value_cnt);
261 return c->case_data->values[idx].f;
263 #endif /* GLOBAL_DEBUGGING */
265 #ifdef GLOBAL_DEBUGGING
267 case_str (const struct ccase *c, size_t idx)
270 assert (c->this == c);
271 assert (c->case_data != NULL);
272 assert (c->case_data->ref_cnt > 0);
273 assert (idx < c->case_data->value_cnt);
275 return c->case_data->values[idx].s;
277 #endif /* GLOBAL_DEBUGGING */
279 #ifdef GLOBAL_DEBUGGING
281 case_data_rw (struct ccase *c, size_t idx)
284 assert (c->this == c);
285 assert (c->case_data != NULL);
286 assert (c->case_data->ref_cnt > 0);
287 assert (idx < c->case_data->value_cnt);
289 if (c->case_data->ref_cnt > 1)
291 return &c->case_data->values[idx];
293 #endif /* GLOBAL_DEBUGGING */