1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <data/case.h>
27 #include <data/value.h>
28 #include <data/variable.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/str.h>
34 /* Reference-counted case implementation. */
37 size_t value_cnt; /* Number of values. */
38 unsigned ref_cnt; /* Reference count. */
39 union value values[1]; /* Values. */
42 /* Ensures that C does not share data with any other case. */
44 case_unshare (struct ccase *c)
46 if (c->case_data->ref_cnt > 1)
48 struct case_data *cd = c->case_data;
50 case_create (c, cd->value_cnt);
51 memcpy (c->case_data->values, cd->values,
52 sizeof *cd->values * cd->value_cnt);
56 /* Returns the number of bytes needed by a case with VALUE_CNT
59 case_size (size_t value_cnt)
61 return (offsetof (struct case_data, values)
62 + value_cnt * sizeof (union value));
65 /* Initializes C as a null case. */
67 case_nullify (struct ccase *c)
72 /* Returns true iff C is a null case. */
74 case_is_null (const struct ccase *c)
76 return c->case_data == NULL;
79 /* Initializes C as a new case that can store VALUE_CNT values.
80 The values have indeterminate contents until explicitly
83 case_create (struct ccase *c, size_t value_cnt)
85 if (!case_try_create (c, value_cnt))
89 /* Initializes CLONE as a copy of ORIG. */
91 case_clone (struct ccase *clone, const struct ccase *orig)
93 assert (orig->case_data->ref_cnt > 0);
97 orig->case_data->ref_cnt++;
103 /* Replaces DST by SRC and nullifies SRC.
104 DST and SRC must be initialized cases at entry. */
106 case_move (struct ccase *dst, struct ccase *src)
108 assert (src->case_data->ref_cnt > 0);
117 /* Destroys case C. */
119 case_destroy (struct ccase *c)
121 struct case_data *cd;
124 if (cd != NULL && --cd->ref_cnt == 0)
126 memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
127 cd->value_cnt = 0xdeadbeef;
132 /* Returns the number of union values in C. */
134 case_get_value_cnt (const struct ccase *c)
136 return c->case_data->value_cnt;
139 /* Resizes case C to NEW_CNT union values. */
141 case_resize (struct ccase *c, size_t new_cnt)
143 size_t old_cnt = case_get_value_cnt (c);
144 if (old_cnt != new_cnt)
148 case_create (&new, new_cnt);
149 case_copy (&new, 0, c, 0, MIN (old_cnt, new_cnt));
155 /* Swaps cases A and B. */
157 case_swap (struct ccase *a, struct ccase *b)
159 struct case_data *t = a->case_data;
160 a->case_data = b->case_data;
164 /* Attempts to create C as a new case that holds VALUE_CNT
165 values. Returns true if successful, false if memory
166 allocation failed. */
168 case_try_create (struct ccase *c, size_t value_cnt)
170 c->case_data = malloc (case_size (value_cnt));
171 if (c->case_data != NULL)
173 c->case_data->value_cnt = value_cnt;
174 c->case_data->ref_cnt = 1;
181 /* Tries to initialize CLONE as a copy of ORIG.
182 Returns true if successful, false if memory allocation
185 case_try_clone (struct ccase *clone, const struct ccase *orig)
187 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)
207 memmove (dst->case_data->values + dst_idx,
208 src->case_data->values + src_idx,
209 sizeof *dst->case_data->values * value_cnt);
213 /* Copies case C to OUTPUT.
214 OUTPUT_SIZE is the number of `union values' in OUTPUT,
215 which must match the number of `union values' in C. */
217 case_to_values (const struct ccase *c, union value *output,
218 size_t output_size UNUSED)
220 assert (c->case_data->ref_cnt > 0);
221 assert (output_size == c->case_data->value_cnt);
222 assert (output != NULL || output_size == 0);
224 memcpy (output, c->case_data->values,
225 c->case_data->value_cnt * sizeof *output);
228 /* Copies INPUT into case C.
229 INPUT_SIZE is the number of `union values' in INPUT,
230 which must match the number of `union values' in C. */
232 case_from_values (struct ccase *c, const union value *input,
233 size_t input_size UNUSED)
235 assert (c->case_data->ref_cnt > 0);
236 assert (input_size == c->case_data->value_cnt);
237 assert (input != NULL || input_size == 0);
240 memcpy (c->case_data->values, input,
241 c->case_data->value_cnt * sizeof *input);
244 /* Returns a pointer to the `union value' used for the
245 element of C for variable V.
246 Case C must be drawn from V's dictionary.
247 The caller must not modify the returned data. */
249 case_data (const struct ccase *c, const struct variable *v)
251 return case_data_idx (c, var_get_case_index (v));
254 /* Returns the numeric value of the `union value' in C for
256 Case C must be drawn from V's dictionary. */
258 case_num (const struct ccase *c, const struct variable *v)
260 return case_num_idx (c, var_get_case_index (v));
263 /* Returns the string value of the `union value' in C for
265 Case C must be drawn from V's dictionary.
266 (Note that the value is not null-terminated.)
267 The caller must not modify the return value. */
269 case_str (const struct ccase *c, const struct variable *v)
271 return case_str_idx (c, var_get_case_index (v));
274 /* Returns a pointer to the `union value' used for the
275 element of C for variable V.
276 Case C must be drawn from V's dictionary.
277 The caller is allowed to modify the returned data. */
279 case_data_rw (struct ccase *c, const struct variable *v)
281 return case_data_rw_idx (c, var_get_case_index (v));
284 /* Returns a pointer to the `union value' used for the
285 element of C numbered IDX.
286 The caller must not modify the returned data. */
288 case_data_idx (const struct ccase *c, size_t idx)
290 assert (c->case_data->ref_cnt > 0);
291 assert (idx < c->case_data->value_cnt);
293 return &c->case_data->values[idx];
296 /* Returns the numeric value of the `union value' in C numbered
299 case_num_idx (const struct ccase *c, size_t idx)
301 assert (c->case_data->ref_cnt > 0);
302 assert (idx < c->case_data->value_cnt);
304 return c->case_data->values[idx].f;
307 /* Returns the string value of the `union value' in C numbered
309 (Note that the value is not null-terminated.)
310 The caller must not modify the return value. */
312 case_str_idx (const struct ccase *c, size_t idx)
314 assert (c->case_data->ref_cnt > 0);
315 assert (idx < c->case_data->value_cnt);
317 return c->case_data->values[idx].s;
320 /* Returns a pointer to the `union value' used for the
321 element of C numbered IDX.
322 The caller is allowed to modify the returned data. */
324 case_data_rw_idx (struct ccase *c, size_t idx)
326 assert (c->case_data->ref_cnt > 0);
327 assert (idx < c->case_data->value_cnt);
330 return &c->case_data->values[idx];
333 /* Compares the values of the VAR_CNT variables in VP
334 in cases A and B and returns a strcmp()-type result. */
336 case_compare (const struct ccase *a, const struct ccase *b,
337 struct variable *const *vp, size_t var_cnt)
339 return case_compare_2dict (a, b, vp, vp, var_cnt);
342 /* Compares the values of the VAR_CNT variables in VAP in case CA
343 to the values of the VAR_CNT variables in VBP in CB
344 and returns a strcmp()-type result. */
346 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
347 struct variable *const *vap, struct variable *const *vbp,
350 for (; var_cnt-- > 0; vap++, vbp++)
352 const struct variable *va = *vap;
353 const struct variable *vb = *vbp;
355 assert (var_get_width (va) == var_get_width (vb));
357 if (var_get_width (va) == 0)
359 double af = case_num (ca, va);
360 double bf = case_num (cb, vb);
363 return af > bf ? 1 : -1;
367 const char *as = case_str (ca, va);
368 const char *bs = case_str (cb, vb);
369 int cmp = memcmp (as, bs, var_get_width (va));
378 /* Returns a pointer to the array of `union value's used for C.
379 The caller must *not* modify the returned data.
381 NOTE: This function breaks the case abstraction. It should
382 *not* be used often. Prefer the other case functions. */
384 case_data_all (const struct ccase *c)
386 assert (c->case_data->ref_cnt > 0);
388 return c->case_data->values;
391 /* Returns a pointer to the array of `union value's used for C.
392 The caller is allowed to modify the returned data.
394 NOTE: This function breaks the case abstraction. It should
395 *not* be used often. Prefer the other case functions. */
397 case_data_all_rw (struct ccase *c)
399 assert (c->case_data->ref_cnt > 0);
402 return c->case_data->values;