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 VALUE_CNT values out of case C to VALUES, starting at
214 the given START_IDX. */
216 case_copy_out (const struct ccase *c,
217 size_t start_idx, union value *values, size_t value_cnt)
219 assert (c->case_data->ref_cnt > 0);
220 assert (value_cnt <= c->case_data->value_cnt);
221 assert (start_idx + value_cnt <= c->case_data->value_cnt);
223 memcpy (values, c->case_data->values + start_idx,
224 value_cnt * sizeof *values);
227 /* Copies VALUE_CNT values from VALUES into case C, staring at
228 the given START_IDX. */
230 case_copy_in (struct ccase *c,
231 size_t start_idx, const union value *values, size_t value_cnt)
233 assert (c->case_data->ref_cnt > 0);
234 assert (value_cnt <= c->case_data->value_cnt);
235 assert (start_idx + value_cnt <= c->case_data->value_cnt);
238 memcpy (c->case_data->values + start_idx, values,
239 value_cnt * sizeof *values);
242 /* Returns a pointer to the `union value' used for the
243 element of C for variable V.
244 Case C must be drawn from V's dictionary.
245 The caller must not modify the returned data. */
247 case_data (const struct ccase *c, const struct variable *v)
249 return case_data_idx (c, var_get_case_index (v));
252 /* Returns the numeric value of the `union value' in C for
254 Case C must be drawn from V's dictionary. */
256 case_num (const struct ccase *c, const struct variable *v)
258 return case_num_idx (c, var_get_case_index (v));
261 /* Returns the string value of the `union value' in C for
263 Case C must be drawn from V's dictionary.
264 (Note that the value is not null-terminated.)
265 The caller must not modify the return value. */
267 case_str (const struct ccase *c, const struct variable *v)
269 return case_str_idx (c, var_get_case_index (v));
272 /* Returns a pointer to the `union value' used for the
273 element of C for variable V.
274 Case C must be drawn from V's dictionary.
275 The caller is allowed to modify the returned data. */
277 case_data_rw (struct ccase *c, const struct variable *v)
279 return case_data_rw_idx (c, var_get_case_index (v));
282 /* Returns a pointer to the `union value' used for the
283 element of C numbered IDX.
284 The caller must not modify the returned data. */
286 case_data_idx (const struct ccase *c, size_t idx)
288 assert (c->case_data->ref_cnt > 0);
289 assert (idx < c->case_data->value_cnt);
291 return &c->case_data->values[idx];
294 /* Returns the numeric value of the `union value' in C numbered
297 case_num_idx (const struct ccase *c, size_t idx)
299 assert (c->case_data->ref_cnt > 0);
300 assert (idx < c->case_data->value_cnt);
302 return c->case_data->values[idx].f;
305 /* Returns the string value of the `union value' in C numbered
307 (Note that the value is not null-terminated.)
308 The caller must not modify the return value. */
310 case_str_idx (const struct ccase *c, size_t idx)
312 assert (c->case_data->ref_cnt > 0);
313 assert (idx < c->case_data->value_cnt);
315 return c->case_data->values[idx].s;
318 /* Returns a pointer to the `union value' used for the
319 element of C numbered IDX.
320 The caller is allowed to modify the returned data. */
322 case_data_rw_idx (struct ccase *c, size_t idx)
324 assert (c->case_data->ref_cnt > 0);
325 assert (idx < c->case_data->value_cnt);
328 return &c->case_data->values[idx];
331 /* Compares the values of the VAR_CNT variables in VP
332 in cases A and B and returns a strcmp()-type result. */
334 case_compare (const struct ccase *a, const struct ccase *b,
335 const struct variable *const *vp, size_t var_cnt)
337 return case_compare_2dict (a, b, vp, vp, var_cnt);
340 /* Compares the values of the VAR_CNT variables in VAP in case CA
341 to the values of the VAR_CNT variables in VBP in CB
342 and returns a strcmp()-type result. */
344 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
345 const struct variable *const *vap,
346 const struct variable *const *vbp,
349 for (; var_cnt-- > 0; vap++, vbp++)
351 const struct variable *va = *vap;
352 const struct variable *vb = *vbp;
354 assert (var_get_width (va) == var_get_width (vb));
356 if (var_get_width (va) == 0)
358 double af = case_num (ca, va);
359 double bf = case_num (cb, vb);
362 return af > bf ? 1 : -1;
366 const char *as = case_str (ca, va);
367 const char *bs = case_str (cb, vb);
368 int cmp = memcmp (as, bs, var_get_width (va));
377 /* Returns a pointer to the array of `union value's used for C.
378 The caller must *not* modify the returned data.
380 NOTE: This function breaks the case abstraction. It should
381 *not* be used often. Prefer the other case functions. */
383 case_data_all (const struct ccase *c)
385 assert (c->case_data->ref_cnt > 0);
387 return c->case_data->values;
390 /* Returns a pointer to the array of `union value's used for C.
391 The caller is allowed to modify the returned data.
393 NOTE: This function breaks the case abstraction. It should
394 *not* be used often. Prefer the other case functions. */
396 case_data_all_rw (struct ccase *c)
398 assert (c->case_data->ref_cnt > 0);
401 return c->case_data->values;