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
37 /* Changes C not to share data with any other case.
38 C must be a case with a reference count greater than 1.
39 There should be no reason for external code to call this
40 function explicitly. It will be called automatically when
43 case_unshare (struct ccase *c)
48 assert (c->this == c);
49 assert (c->case_data != NULL);
50 assert (c->case_data->ref_cnt > 1);
54 case_create (c, c->case_data->value_cnt);
55 memcpy (c->case_data->values, cd->values,
56 sizeof *cd->values * cd->value_cnt);
59 /* Returns the number of bytes needed by a case with VALUE_CNT
62 case_size (size_t value_cnt)
64 return (offsetof (struct case_data, values)
65 + value_cnt * sizeof (union value));
68 #ifdef GLOBAL_DEBUGGING
69 /* Initializes C as a null case. */
71 case_nullify (struct ccase *c)
76 #endif /* GLOBAL_DEBUGGING */
78 #ifdef GLOBAL_DEBUGGING
79 /* Returns true iff C is a null case. */
81 case_is_null (const struct ccase *c)
83 return c->case_data == NULL;
85 #endif /* GLOBAL_DEBUGGING */
87 /* Initializes C as a new case that can store VALUE_CNT values.
88 The values have indeterminate contents until explicitly
91 case_create (struct ccase *c, size_t value_cnt)
93 if (!case_try_create (c, value_cnt))
97 #ifdef GLOBAL_DEBUGGING
98 /* Initializes CLONE as a copy of ORIG. */
100 case_clone (struct ccase *clone, const struct ccase *orig)
102 assert (orig != NULL);
103 assert (orig->this == orig);
104 assert (orig->case_data != NULL);
105 assert (orig->case_data->ref_cnt > 0);
106 assert (clone != NULL);
113 orig->case_data->ref_cnt++;
115 #endif /* GLOBAL_DEBUGGING */
117 #ifdef GLOBAL_DEBUGGING
118 /* Replaces DST by SRC and nullifies SRC.
119 DST and SRC must be initialized cases at entry. */
121 case_move (struct ccase *dst, struct ccase *src)
123 assert (src != NULL);
124 assert (src->this == src);
125 assert (src->case_data != NULL);
126 assert (src->case_data->ref_cnt > 0);
127 assert (dst != NULL);
133 #endif /* GLOBAL_DEBUGGING */
135 #ifdef GLOBAL_DEBUGGING
136 /* Destroys case C. */
138 case_destroy (struct ccase *c)
140 struct case_data *cd;
143 assert (c->this == c);
146 if (cd != NULL && --cd->ref_cnt == 0)
148 memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
149 cd->value_cnt = 0xdeadbeef;
153 #endif /* GLOBAL_DEBUGGING */
155 /* Attempts to create C as a new case that holds VALUE_CNT
156 values. Returns nonzero if successful, zero if memory
157 allocation failed. */
159 case_try_create (struct ccase *c, size_t value_cnt)
161 c->case_data = malloc (case_size (value_cnt));
162 if (c->case_data != NULL)
164 #ifdef GLOBAL_DEBUGGING
167 c->case_data->value_cnt = value_cnt;
168 c->case_data->ref_cnt = 1;
173 #ifdef GLOBAL_DEBUGGING
180 /* Tries to initialize CLONE as a copy of ORIG.
181 Returns nonzero if successful, zero if memory allocation
184 case_try_clone (struct ccase *clone, const struct ccase *orig)
186 case_clone (clone, orig);
190 #ifdef GLOBAL_DEBUGGING
191 /* Copies VALUE_CNT values from SRC (starting at SRC_IDX) to DST
192 (starting at DST_IDX). */
194 case_copy (struct ccase *dst, size_t dst_idx,
195 const struct ccase *src, size_t src_idx,
198 assert (dst != NULL);
199 assert (dst->this == dst);
200 assert (dst->case_data != NULL);
201 assert (dst->case_data->ref_cnt > 0);
202 assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
204 assert (src != NULL);
205 assert (src->this == src);
206 assert (src->case_data != NULL);
207 assert (src->case_data->ref_cnt > 0);
208 assert (src_idx + value_cnt <= dst->case_data->value_cnt);
210 if (dst->case_data->ref_cnt > 1)
212 if (dst->case_data != src->case_data || dst_idx != src_idx)
213 memmove (dst->case_data->values + dst_idx,
214 src->case_data->values + src_idx,
215 sizeof *dst->case_data->values * value_cnt);
217 #endif /* GLOBAL_DEBUGGING */
219 #ifdef GLOBAL_DEBUGGING
220 /* Copies case C to OUTPUT.
221 OUTPUT_SIZE is the number of `union values' in OUTPUT,
222 which must match the number of `union values' in C. */
224 case_to_values (const struct ccase *c, union value *output,
225 size_t output_size UNUSED)
228 assert (c->this == c);
229 assert (c->case_data != NULL);
230 assert (c->case_data->ref_cnt > 0);
231 assert (output_size == c->case_data->value_cnt);
232 assert (output != NULL || output_size == 0);
234 memcpy (output, c->case_data->values,
235 c->case_data->value_cnt * sizeof *output);
237 #endif /* GLOBAL_DEBUGGING */
239 #ifdef GLOBAL_DEBUGGING
240 /* Copies INPUT into case C.
241 INPUT_SIZE is the number of `union values' in INPUT,
242 which must match the number of `union values' in C. */
244 case_from_values (struct ccase *c, const union value *input,
245 size_t input_size UNUSED)
248 assert (c->this == c);
249 assert (c->case_data != NULL);
250 assert (c->case_data->ref_cnt > 0);
251 assert (input_size == c->case_data->value_cnt);
252 assert (input != NULL || input_size == 0);
254 if (c->case_data->ref_cnt > 1)
256 memcpy (c->case_data->values, input,
257 c->case_data->value_cnt * sizeof *input);
259 #endif /* GLOBAL_DEBUGGING */
261 #ifdef GLOBAL_DEBUGGING
262 /* Returns a pointer to the `union value' used for the
263 element of C numbered IDX.
264 The caller must not modify the returned data. */
266 case_data (const struct ccase *c, size_t idx)
269 assert (c->this == c);
270 assert (c->case_data != NULL);
271 assert (c->case_data->ref_cnt > 0);
272 assert (idx < c->case_data->value_cnt);
274 return &c->case_data->values[idx];
276 #endif /* GLOBAL_DEBUGGING */
278 #ifdef GLOBAL_DEBUGGING
279 /* Returns the numeric value of the `union value' in C numbered
282 case_num (const struct ccase *c, size_t idx)
285 assert (c->this == c);
286 assert (c->case_data != NULL);
287 assert (c->case_data->ref_cnt > 0);
288 assert (idx < c->case_data->value_cnt);
290 return c->case_data->values[idx].f;
292 #endif /* GLOBAL_DEBUGGING */
294 #ifdef GLOBAL_DEBUGGING
295 /* Returns the string value of the `union value' in C numbered
297 (Note that the value is not null-terminated.)
298 The caller must not modify the return value. */
300 case_str (const struct ccase *c, size_t idx)
303 assert (c->this == c);
304 assert (c->case_data != NULL);
305 assert (c->case_data->ref_cnt > 0);
306 assert (idx < c->case_data->value_cnt);
308 return c->case_data->values[idx].s;
310 #endif /* GLOBAL_DEBUGGING */
312 #ifdef GLOBAL_DEBUGGING
313 /* Returns a pointer to the `union value' used for the
314 element of C numbered IDX.
315 The caller is allowed to modify the returned data. */
317 case_data_rw (struct ccase *c, size_t idx)
320 assert (c->this == c);
321 assert (c->case_data != NULL);
322 assert (c->case_data->ref_cnt > 0);
323 assert (idx < c->case_data->value_cnt);
325 if (c->case_data->ref_cnt > 1)
327 return &c->case_data->values[idx];
329 #endif /* GLOBAL_DEBUGGING */
331 /* Returns a pointer to the array of `union value's used for C.
332 The caller must *not* modify the returned data.
334 NOTE: This function breaks the case abstraction. It should
335 *not* be used often. Prefer the other case functions. */
337 case_data_all (const struct ccase *c)
340 assert (c->this == c);
341 assert (c->case_data != NULL);
342 assert (c->case_data->ref_cnt > 0);
344 return c->case_data->values;
347 /* Returns a pointer to the array of `union value's used for C.
348 The caller is allowed to modify the returned data.
350 NOTE: This function breaks the case abstraction. It should
351 *not* be used often. Prefer the other case functions. */
353 case_data_all_rw (struct ccase *c)
356 assert (c->this == c);
357 assert (c->case_data != NULL);
358 assert (c->case_data->ref_cnt > 0);
360 if (c->case_data->ref_cnt > 1)
362 return c->case_data->values;