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., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <libpspp/alloc.h>
26 #include <libpspp/str.h>
38 /* Changes C not to share data with any other case.
39 C must be a case with a reference count greater than 1.
40 There should be no reason for external code to call this
41 function explicitly. It will be called automatically when
44 case_unshare (struct ccase *c)
48 assert (c->case_data->ref_cnt > 1);
52 case_create (c, c->case_data->value_cnt);
53 memcpy (c->case_data->values, cd->values,
54 sizeof *cd->values * cd->value_cnt);
57 /* Returns the number of bytes needed by a case with VALUE_CNT
60 case_size (size_t value_cnt)
62 return (offsetof (struct case_data, values)
63 + value_cnt * sizeof (union value));
67 /* Initializes C as a null case. */
69 case_nullify (struct ccase *c)
73 #endif /* DEBUGGING */
76 /* Returns true iff C is a null case. */
78 case_is_null (const struct ccase *c)
80 return c->case_data == NULL;
82 #endif /* DEBUGGING */
84 /* Initializes C as a new case that can store VALUE_CNT values.
85 The values have indeterminate contents until explicitly
88 case_create (struct ccase *c, size_t value_cnt)
90 if (!case_try_create (c, value_cnt))
95 /* Initializes CLONE as a copy of ORIG. */
97 case_clone (struct ccase *clone, const struct ccase *orig)
99 assert (orig->case_data->ref_cnt > 0);
103 orig->case_data->ref_cnt++;
105 #endif /* DEBUGGING */
108 /* Replaces DST by SRC and nullifies SRC.
109 DST and SRC must be initialized cases at entry. */
111 case_move (struct ccase *dst, struct ccase *src)
113 assert (src->case_data->ref_cnt > 0);
121 #endif /* DEBUGGING */
124 /* Destroys case C. */
126 case_destroy (struct ccase *c)
128 struct case_data *cd;
133 if (cd != NULL && --cd->ref_cnt == 0)
135 memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
136 cd->value_cnt = 0xdeadbeef;
140 #endif /* DEBUGGING */
142 /* Resizes case C from OLD_CNT to NEW_CNT values. */
144 case_resize (struct ccase *c, size_t old_cnt, size_t new_cnt)
148 case_create (&new, new_cnt);
149 case_copy (&new, 0, c, 0, old_cnt < new_cnt ? old_cnt : new_cnt);
154 /* Swaps cases A and B. */
156 case_swap (struct ccase *a, struct ccase *b)
158 struct case_data *t = a->case_data;
159 a->case_data = b->case_data;
163 /* Attempts to create C as a new case that holds VALUE_CNT
164 values. Returns true if successful, false if memory
165 allocation failed. */
167 case_try_create (struct ccase *c, size_t value_cnt)
169 c->case_data = malloc (case_size (value_cnt));
170 if (c->case_data != NULL)
172 c->case_data->value_cnt = value_cnt;
173 c->case_data->ref_cnt = 1;
180 /* Tries to initialize CLONE as a copy of ORIG.
181 Returns true if successful, false if memory allocation
184 case_try_clone (struct ccase *clone, const struct ccase *orig)
186 case_clone (clone, orig);
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->case_data->ref_cnt > 0);
199 assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
201 assert (src->case_data->ref_cnt > 0);
202 assert (src_idx + value_cnt <= src->case_data->value_cnt);
204 if (dst->case_data != src->case_data || dst_idx != src_idx)
206 if (dst->case_data->ref_cnt > 1)
208 memmove (dst->case_data->values + dst_idx,
209 src->case_data->values + src_idx,
210 sizeof *dst->case_data->values * value_cnt);
213 #endif /* DEBUGGING */
216 /* Copies case C to OUTPUT.
217 OUTPUT_SIZE is the number of `union values' in OUTPUT,
218 which must match the number of `union values' in C. */
220 case_to_values (const struct ccase *c, union value *output,
221 size_t output_size UNUSED)
223 assert (c->case_data->ref_cnt > 0);
224 assert (output_size == c->case_data->value_cnt);
225 assert (output != NULL || output_size == 0);
227 memcpy (output, c->case_data->values,
228 c->case_data->value_cnt * sizeof *output);
230 #endif /* DEBUGGING */
233 /* Copies INPUT into case C.
234 INPUT_SIZE is the number of `union values' in INPUT,
235 which must match the number of `union values' in C. */
237 case_from_values (struct ccase *c, const union value *input,
238 size_t input_size UNUSED)
241 assert (c->case_data != NULL);
242 assert (c->case_data->ref_cnt > 0);
243 assert (input_size == c->case_data->value_cnt);
244 assert (input != NULL || input_size == 0);
246 if (c->case_data->ref_cnt > 1)
248 memcpy (c->case_data->values, input,
249 c->case_data->value_cnt * sizeof *input);
251 #endif /* DEBUGGING */
254 /* Returns a pointer to the `union value' used for the
255 element of C numbered IDX.
256 The caller must not modify the returned data. */
258 case_data_idx (const struct ccase *c, size_t idx)
261 assert (c->case_data != NULL);
262 assert (c->case_data->ref_cnt > 0);
263 assert (idx < c->case_data->value_cnt);
265 return &c->case_data->values[idx];
267 #endif /* DEBUGGING */
270 /* Returns the numeric value of the `union value' in C numbered
273 case_num_idx (const struct ccase *c, size_t idx)
276 assert (c->case_data != NULL);
277 assert (c->case_data->ref_cnt > 0);
278 assert (idx < c->case_data->value_cnt);
280 return c->case_data->values[idx].f;
282 #endif /* DEBUGGING */
285 /* Returns the string value of the `union value' in C numbered
287 (Note that the value is not null-terminated.)
288 The caller must not modify the return value. */
290 case_str_idx (const struct ccase *c, size_t idx)
293 assert (c->case_data != NULL);
294 assert (c->case_data->ref_cnt > 0);
295 assert (idx < c->case_data->value_cnt);
297 return c->case_data->values[idx].s;
299 #endif /* DEBUGGING */
302 /* Returns a pointer to the `union value' used for the
303 element of C numbered IDX.
304 The caller is allowed to modify the returned data. */
306 case_data_rw_idx (struct ccase *c, size_t idx)
309 assert (c->case_data != NULL);
310 assert (c->case_data->ref_cnt > 0);
311 assert (idx < c->case_data->value_cnt);
313 if (c->case_data->ref_cnt > 1)
315 return &c->case_data->values[idx];
317 #endif /* DEBUGGING */
319 /* Compares the values of the VAR_CNT variables in VP
320 in cases A and B and returns a strcmp()-type result. */
322 case_compare (const struct ccase *a, const struct ccase *b,
323 struct variable *const *vp, size_t var_cnt)
325 return case_compare_2dict (a, b, vp, vp, var_cnt);
328 /* Compares the values of the VAR_CNT variables in VAP in case CA
329 to the values of the VAR_CNT variables in VBP in CB
330 and returns a strcmp()-type result. */
332 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
333 struct variable *const *vap, struct variable *const *vbp,
336 for (; var_cnt-- > 0; vap++, vbp++)
338 const struct variable *va = *vap;
339 const struct variable *vb = *vbp;
341 assert (var_get_width (va) == var_get_width (vb));
343 if (var_get_width (va) == 0)
345 double af = case_num (ca, va);
346 double bf = case_num (cb, vb);
349 return af > bf ? 1 : -1;
353 const char *as = case_str (ca, va);
354 const char *bs = case_str (cb, vb);
355 int cmp = memcmp (as, bs, var_get_width (va));
364 /* Returns a pointer to the array of `union value's used for C.
365 The caller must *not* modify the returned data.
367 NOTE: This function breaks the case abstraction. It should
368 *not* be used often. Prefer the other case functions. */
370 case_data_all (const struct ccase *c)
373 assert (c->case_data != NULL);
374 assert (c->case_data->ref_cnt > 0);
376 return c->case_data->values;
379 /* Returns a pointer to the array of `union value's used for C.
380 The caller is allowed to modify the returned data.
382 NOTE: This function breaks the case abstraction. It should
383 *not* be used often. Prefer the other case functions. */
385 case_data_all_rw (struct ccase *c)
388 assert (c->case_data != NULL);
389 assert (c->case_data->ref_cnt > 0);
391 if (c->case_data->ref_cnt > 1)
393 return c->case_data->values;