578f8b6acbe66c8774ff667a46beed34f362f6f9
[pspp-builds.git] / src / data / case.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004, 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <data/case.h>
22
23 #include <assert.h>
24 #include <limits.h>
25 #include <stdlib.h>
26
27 #include <data/value.h>
28 #include <data/variable.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/str.h>
31
32 #include "minmax.h"
33
34 /* Reference-counted case implementation. */
35 struct case_data
36   {
37     size_t value_cnt;                   /* Number of values. */
38     unsigned ref_cnt;                   /* Reference count. */
39     union value values[1];              /* Values. */
40   };
41
42 /* Ensures that C does not share data with any other case. */
43 static void
44 case_unshare (struct ccase *c)
45 {
46   if (c->case_data->ref_cnt > 1)
47     {
48       struct case_data *cd = c->case_data;
49       cd->ref_cnt--;
50       case_create (c, cd->value_cnt);
51       memcpy (c->case_data->values, cd->values,
52               sizeof *cd->values * cd->value_cnt);
53     }
54 }
55
56 /* Returns the number of bytes needed by a case with VALUE_CNT
57    values. */
58 static size_t
59 case_size (size_t value_cnt)
60 {
61   return (offsetof (struct case_data, values)
62           + value_cnt * sizeof (union value));
63 }
64
65 /* Initializes C as a null case. */
66 void
67 case_nullify (struct ccase *c)
68 {
69   c->case_data = NULL;
70 }
71
72 /* Returns true iff C is a null case. */
73 bool
74 case_is_null (const struct ccase *c)
75 {
76   return c->case_data == NULL;
77 }
78
79 /* Initializes C as a new case that can store VALUE_CNT values.
80    The values have indeterminate contents until explicitly
81    written. */
82 void
83 case_create (struct ccase *c, size_t value_cnt)
84 {
85   if (!case_try_create (c, value_cnt))
86     xalloc_die ();
87 }
88
89 /* Initializes CLONE as a copy of ORIG. */
90 void
91 case_clone (struct ccase *clone, const struct ccase *orig)
92 {
93   assert (orig->case_data->ref_cnt > 0);
94
95   if (clone != orig)
96     *clone = *orig;
97   orig->case_data->ref_cnt++;
98 #ifdef DEBUGGING
99   case_unshare (clone);
100 #endif
101 }
102
103 /* Replaces DST by SRC and nullifies SRC.
104    DST and SRC must be initialized cases at entry. */
105 void
106 case_move (struct ccase *dst, struct ccase *src)
107 {
108   assert (src->case_data->ref_cnt > 0);
109
110   if (dst != src)
111     {
112       *dst = *src;
113       case_nullify (src);
114     }
115 }
116
117 /* Destroys case C. */
118 void
119 case_destroy (struct ccase *c)
120 {
121   struct case_data *cd;
122
123   cd = c->case_data;
124   if (cd != NULL && --cd->ref_cnt == 0)
125     {
126       memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
127       cd->value_cnt = 0xdeadbeef;
128       free (cd);
129     }
130 }
131
132 /* Returns the number of union values in C. */
133 size_t
134 case_get_value_cnt (const struct ccase *c)
135 {
136   return c->case_data->value_cnt;
137 }
138
139 /* Resizes case C to NEW_CNT union values. */
140 void
141 case_resize (struct ccase *c, size_t new_cnt)
142 {
143   size_t old_cnt = case_get_value_cnt (c);
144   if (old_cnt != new_cnt)
145     {
146       struct ccase new;
147
148       case_create (&new, new_cnt);
149       case_copy (&new, 0, c, 0, MIN (old_cnt, new_cnt));
150       case_swap (&new, c);
151       case_destroy (&new);
152     }
153 }
154
155 /* Swaps cases A and B. */
156 void
157 case_swap (struct ccase *a, struct ccase *b)
158 {
159   struct case_data *t = a->case_data;
160   a->case_data = b->case_data;
161   b->case_data = t;
162 }
163
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. */
167 bool
168 case_try_create (struct ccase *c, size_t value_cnt)
169 {
170   c->case_data = malloc (case_size (value_cnt));
171   if (c->case_data != NULL)
172     {
173       c->case_data->value_cnt = value_cnt;
174       c->case_data->ref_cnt = 1;
175       return true;
176     }
177
178   return false;
179 }
180
181 /* Tries to initialize CLONE as a copy of ORIG.
182    Returns true if successful, false if memory allocation
183    failed. */
184 bool
185 case_try_clone (struct ccase *clone, const struct ccase *orig)
186 {
187   case_clone (clone, orig);
188   return true;
189 }
190
191 /* Copies VALUE_CNT values from SRC (starting at SRC_IDX) to DST
192    (starting at DST_IDX). */
193 void
194 case_copy (struct ccase *dst, size_t dst_idx,
195            const struct ccase *src, size_t src_idx,
196            size_t value_cnt)
197 {
198   assert (dst->case_data->ref_cnt > 0);
199   assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
200
201   assert (src->case_data->ref_cnt > 0);
202   assert (src_idx + value_cnt <= src->case_data->value_cnt);
203
204   if (dst->case_data != src->case_data || dst_idx != src_idx)
205     {
206       case_unshare (dst);
207       memmove (dst->case_data->values + dst_idx,
208                src->case_data->values + src_idx,
209                sizeof *dst->case_data->values * value_cnt);
210     }
211 }
212
213 /* Copies VALUE_CNT values out of case C to VALUES, starting at
214    the given START_IDX. */
215 void
216 case_copy_out (const struct ccase *c,
217                size_t start_idx, union value *values, size_t value_cnt)
218 {
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);
222
223   memcpy (values, c->case_data->values + start_idx,
224           value_cnt * sizeof *values);
225 }
226
227 /* Copies VALUE_CNT values from VALUES into case C, staring at
228    the given START_IDX. */
229 void
230 case_copy_in (struct ccase *c,
231               size_t start_idx, const union value *values, size_t value_cnt)
232 {
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);
236
237   case_unshare (c);
238   memcpy (c->case_data->values + start_idx, values,
239           value_cnt * sizeof *values);
240 }
241
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. */
246 const union value *
247 case_data (const struct ccase *c, const struct variable *v)
248 {
249   return case_data_idx (c, var_get_case_index (v));
250 }
251
252 /* Returns the numeric value of the `union value' in C for
253    variable V.
254    Case C must be drawn from V's dictionary. */
255 double
256 case_num (const struct ccase *c, const struct variable *v)
257 {
258   return case_num_idx (c, var_get_case_index (v));
259 }
260
261 /* Returns the string value of the `union value' in C for
262    variable V.
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. */
266 const char *
267 case_str (const struct ccase *c, const struct variable *v)
268 {
269   return case_str_idx (c, var_get_case_index (v));
270 }
271
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. */
276 union value *
277 case_data_rw (struct ccase *c, const struct variable *v)
278 {
279   return case_data_rw_idx (c, var_get_case_index (v));
280 }
281
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. */
285 const union value *
286 case_data_idx (const struct ccase *c, size_t idx)
287 {
288   assert (c->case_data->ref_cnt > 0);
289   assert (idx < c->case_data->value_cnt);
290
291   return &c->case_data->values[idx];
292 }
293
294 /* Returns the numeric value of the `union value' in C numbered
295    IDX. */
296 double
297 case_num_idx (const struct ccase *c, size_t idx)
298 {
299   assert (c->case_data->ref_cnt > 0);
300   assert (idx < c->case_data->value_cnt);
301
302   return c->case_data->values[idx].f;
303 }
304
305 /* Returns the string value of the `union value' in C numbered
306    IDX.
307    (Note that the value is not null-terminated.)
308    The caller must not modify the return value. */
309 const char *
310 case_str_idx (const struct ccase *c, size_t idx)
311 {
312   assert (c->case_data->ref_cnt > 0);
313   assert (idx < c->case_data->value_cnt);
314
315   return c->case_data->values[idx].s;
316 }
317
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. */
321 union value *
322 case_data_rw_idx (struct ccase *c, size_t idx)
323 {
324   assert (c->case_data->ref_cnt > 0);
325   assert (idx < c->case_data->value_cnt);
326
327   case_unshare (c);
328   return &c->case_data->values[idx];
329 }
330
331 /* Compares the values of the VAR_CNT variables in VP
332    in cases A and B and returns a strcmp()-type result. */
333 int
334 case_compare (const struct ccase *a, const struct ccase *b,
335               const struct variable *const *vp, size_t var_cnt)
336 {
337   return case_compare_2dict (a, b, vp, vp, var_cnt);
338 }
339
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. */
343 int
344 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
345                     const struct variable *const *vap,
346                 const struct variable *const *vbp,
347                     size_t var_cnt)
348 {
349   for (; var_cnt-- > 0; vap++, vbp++)
350     {
351       const struct variable *va = *vap;
352       const struct variable *vb = *vbp;
353
354       assert (var_get_width (va) == var_get_width (vb));
355
356       if (var_get_width (va) == 0)
357         {
358           double af = case_num (ca, va);
359           double bf = case_num (cb, vb);
360
361           if (af != bf)
362             return af > bf ? 1 : -1;
363         }
364       else
365         {
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));
369
370           if (cmp != 0)
371             return cmp;
372         }
373     }
374   return 0;
375 }
376
377 /* Returns a pointer to the array of `union value's used for C.
378    The caller must *not* modify the returned data.
379
380    NOTE: This function breaks the case abstraction.  It should
381    *not* be used often.  Prefer the other case functions. */
382 const union value *
383 case_data_all (const struct ccase *c)
384 {
385   assert (c->case_data->ref_cnt > 0);
386
387   return c->case_data->values;
388 }
389
390 /* Returns a pointer to the array of `union value's used for C.
391    The caller is allowed to modify the returned data.
392
393    NOTE: This function breaks the case abstraction.  It should
394    *not* be used often.  Prefer the other case functions. */
395 union value *
396 case_data_all_rw (struct ccase *c)
397 {
398   assert (c->case_data->ref_cnt > 0);
399
400   case_unshare (c);
401   return c->case_data->values;
402 }