1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2007 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
19 #include <data/case.h>
25 #include <data/value.h>
26 #include <data/variable.h>
27 #include <libpspp/alloc.h>
28 #include <libpspp/str.h>
32 /* Reference-counted case implementation. */
35 size_t value_cnt; /* Number of values. */
36 unsigned ref_cnt; /* Reference count. */
37 union value values[1]; /* Values. */
40 /* Ensures that C does not share data with any other case. */
42 case_unshare (struct ccase *c)
44 if (c->case_data->ref_cnt > 1)
46 struct case_data *cd = c->case_data;
48 case_create (c, cd->value_cnt);
49 memcpy (c->case_data->values, cd->values,
50 sizeof *cd->values * cd->value_cnt);
54 /* Returns the number of bytes needed by a case with VALUE_CNT
57 case_size (size_t value_cnt)
59 return (offsetof (struct case_data, values)
60 + value_cnt * sizeof (union value));
63 /* Initializes C as a null case. */
65 case_nullify (struct ccase *c)
70 /* Returns true iff C is a null case. */
72 case_is_null (const struct ccase *c)
74 return c->case_data == NULL;
77 /* Initializes C as a new case that can store VALUE_CNT values.
78 The values have indeterminate contents until explicitly
81 case_create (struct ccase *c, size_t value_cnt)
83 if (!case_try_create (c, value_cnt))
87 /* Initializes CLONE as a copy of ORIG. */
89 case_clone (struct ccase *clone, const struct ccase *orig)
91 assert (orig->case_data->ref_cnt > 0);
95 orig->case_data->ref_cnt++;
101 /* Replaces DST by SRC and nullifies SRC.
102 DST and SRC must be initialized cases at entry. */
104 case_move (struct ccase *dst, struct ccase *src)
106 assert (src->case_data->ref_cnt > 0);
115 /* Destroys case C. */
117 case_destroy (struct ccase *c)
119 struct case_data *cd;
122 if (cd != NULL && --cd->ref_cnt == 0)
124 memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
125 cd->value_cnt = 0xdeadbeef;
130 /* Returns the number of union values in C. */
132 case_get_value_cnt (const struct ccase *c)
134 return c->case_data->value_cnt;
137 /* Resizes case C to NEW_CNT union values. */
139 case_resize (struct ccase *c, size_t new_cnt)
141 size_t old_cnt = case_get_value_cnt (c);
142 if (old_cnt != new_cnt)
146 case_create (&new, new_cnt);
147 case_copy (&new, 0, c, 0, MIN (old_cnt, new_cnt));
153 /* Swaps cases A and B. */
155 case_swap (struct ccase *a, struct ccase *b)
157 struct case_data *t = a->case_data;
158 a->case_data = b->case_data;
162 /* Attempts to create C as a new case that holds VALUE_CNT
163 values. Returns true if successful, false if memory
164 allocation failed. */
166 case_try_create (struct ccase *c, size_t value_cnt)
168 c->case_data = malloc (case_size (value_cnt));
169 if (c->case_data != NULL)
171 c->case_data->value_cnt = value_cnt;
172 c->case_data->ref_cnt = 1;
179 /* Tries to initialize CLONE as a copy of ORIG.
180 Returns true if successful, false if memory allocation
183 case_try_clone (struct ccase *clone, const struct ccase *orig)
185 case_clone (clone, orig);
189 /* Copies VALUE_CNT values from SRC (starting at SRC_IDX) to DST
190 (starting at DST_IDX). */
192 case_copy (struct ccase *dst, size_t dst_idx,
193 const struct ccase *src, size_t src_idx,
196 assert (dst->case_data->ref_cnt > 0);
197 assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
199 assert (src->case_data->ref_cnt > 0);
200 assert (src_idx + value_cnt <= src->case_data->value_cnt);
202 if (dst->case_data != src->case_data || dst_idx != src_idx)
205 memmove (dst->case_data->values + dst_idx,
206 src->case_data->values + src_idx,
207 sizeof *dst->case_data->values * value_cnt);
211 /* Copies VALUE_CNT values out of case C to VALUES, starting at
212 the given START_IDX. */
214 case_copy_out (const struct ccase *c,
215 size_t start_idx, union value *values, size_t value_cnt)
217 assert (c->case_data->ref_cnt > 0);
218 assert (value_cnt <= c->case_data->value_cnt);
219 assert (start_idx + value_cnt <= c->case_data->value_cnt);
221 memcpy (values, c->case_data->values + start_idx,
222 value_cnt * sizeof *values);
225 /* Copies VALUE_CNT values from VALUES into case C, staring at
226 the given START_IDX. */
228 case_copy_in (struct ccase *c,
229 size_t start_idx, const union value *values, size_t value_cnt)
231 assert (c->case_data->ref_cnt > 0);
232 assert (value_cnt <= c->case_data->value_cnt);
233 assert (start_idx + value_cnt <= c->case_data->value_cnt);
236 memcpy (c->case_data->values + start_idx, values,
237 value_cnt * sizeof *values);
240 /* Returns a pointer to the `union value' used for the
241 element of C for variable V.
242 Case C must be drawn from V's dictionary.
243 The caller must not modify the returned data. */
245 case_data (const struct ccase *c, const struct variable *v)
247 return case_data_idx (c, var_get_case_index (v));
250 /* Returns the numeric value of the `union value' in C for
252 Case C must be drawn from V's dictionary. */
254 case_num (const struct ccase *c, const struct variable *v)
256 return case_num_idx (c, var_get_case_index (v));
259 /* Returns the string value of the `union value' in C for
261 Case C must be drawn from V's dictionary.
262 (Note that the value is not null-terminated.)
263 The caller must not modify the return value. */
265 case_str (const struct ccase *c, const struct variable *v)
267 return case_str_idx (c, var_get_case_index (v));
270 /* Returns a pointer to the `union value' used for the
271 element of C for variable V.
272 Case C must be drawn from V's dictionary.
273 The caller is allowed to modify the returned data. */
275 case_data_rw (struct ccase *c, const struct variable *v)
277 return case_data_rw_idx (c, var_get_case_index (v));
280 /* Returns a pointer to the `union value' used for the
281 element of C numbered IDX.
282 The caller must not modify the returned data. */
284 case_data_idx (const struct ccase *c, size_t idx)
286 assert (c->case_data->ref_cnt > 0);
287 assert (idx < c->case_data->value_cnt);
289 return &c->case_data->values[idx];
292 /* Returns the numeric value of the `union value' in C numbered
295 case_num_idx (const struct ccase *c, size_t idx)
297 assert (c->case_data->ref_cnt > 0);
298 assert (idx < c->case_data->value_cnt);
300 return c->case_data->values[idx].f;
303 /* Returns the string value of the `union value' in C numbered
305 (Note that the value is not null-terminated.)
306 The caller must not modify the return value. */
308 case_str_idx (const struct ccase *c, size_t idx)
310 assert (c->case_data->ref_cnt > 0);
311 assert (idx < c->case_data->value_cnt);
313 return c->case_data->values[idx].s;
316 /* Returns a pointer to the `union value' used for the
317 element of C numbered IDX.
318 The caller is allowed to modify the returned data. */
320 case_data_rw_idx (struct ccase *c, size_t idx)
322 assert (c->case_data->ref_cnt > 0);
323 assert (idx < c->case_data->value_cnt);
326 return &c->case_data->values[idx];
329 /* Compares the values of the VAR_CNT variables in VP
330 in cases A and B and returns a strcmp()-type result. */
332 case_compare (const struct ccase *a, const struct ccase *b,
333 const struct variable *const *vp, size_t var_cnt)
335 return case_compare_2dict (a, b, vp, vp, var_cnt);
338 /* Compares the values of the VAR_CNT variables in VAP in case CA
339 to the values of the VAR_CNT variables in VBP in CB
340 and returns a strcmp()-type result. */
342 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
343 const struct variable *const *vap,
344 const struct variable *const *vbp,
347 for (; var_cnt-- > 0; vap++, vbp++)
349 const struct variable *va = *vap;
350 const struct variable *vb = *vbp;
352 assert (var_get_width (va) == var_get_width (vb));
354 if (var_get_width (va) == 0)
356 double af = case_num (ca, va);
357 double bf = case_num (cb, vb);
360 return af > bf ? 1 : -1;
364 const char *as = case_str (ca, va);
365 const char *bs = case_str (cb, vb);
366 int cmp = memcmp (as, bs, var_get_width (va));
375 /* Returns a pointer to the array of `union value's used for C.
376 The caller must *not* modify the returned data.
378 NOTE: This function breaks the case abstraction. It should
379 *not* be used often. Prefer the other case functions. */
381 case_data_all (const struct ccase *c)
383 assert (c->case_data->ref_cnt > 0);
385 return c->case_data->values;
388 /* Returns a pointer to the array of `union value's used for C.
389 The caller is allowed to modify the returned data.
391 NOTE: This function breaks the case abstraction. It should
392 *not* be used often. Prefer the other case functions. */
394 case_data_all_rw (struct ccase *c)
396 assert (c->case_data->ref_cnt > 0);
399 return c->case_data->values;