6fad874d0c71482086c0387c1cf390e4f75708d0
[pspp-builds.git] / src / data / case.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include "case.h"
22 #include <limits.h>
23 #include <stdlib.h>
24 #include "value.h"
25 #include "alloc.h"
26 #include "str.h"
27 #include "variable.h"
28
29 #ifdef GLOBAL_DEBUGGING
30 #undef NDEBUG
31 #else
32 #ifndef NDEBUG
33 #define NDEBUG
34 #endif
35 #endif
36 #include <assert.h>
37
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
42    needed. */
43 void
44 case_unshare (struct ccase *c) 
45 {
46   struct case_data *cd;
47   
48   assert (c != NULL);
49   assert (c->this == c);
50   assert (c->case_data != NULL);
51   assert (c->case_data->ref_cnt > 1);
52
53   cd = c->case_data;
54   cd->ref_cnt--;
55   case_create (c, c->case_data->value_cnt);
56   memcpy (c->case_data->values, cd->values,
57           sizeof *cd->values * cd->value_cnt); 
58 }
59
60 /* Returns the number of bytes needed by a case with VALUE_CNT
61    values. */
62 static inline size_t
63 case_size (size_t value_cnt) 
64 {
65   return (offsetof (struct case_data, values)
66           + value_cnt * sizeof (union value));
67 }
68
69 #ifdef GLOBAL_DEBUGGING
70 /* Initializes C as a null case. */
71 void
72 case_nullify (struct ccase *c) 
73 {
74   c->case_data = NULL;
75   c->this = c;
76 }
77 #endif /* GLOBAL_DEBUGGING */
78
79 #ifdef GLOBAL_DEBUGGING
80 /* Returns true iff C is a null case. */
81 int
82 case_is_null (const struct ccase *c) 
83 {
84   return c->case_data == NULL;
85 }
86 #endif /* GLOBAL_DEBUGGING */
87
88 /* Initializes C as a new case that can store VALUE_CNT values.
89    The values have indeterminate contents until explicitly
90    written. */
91 void
92 case_create (struct ccase *c, size_t value_cnt) 
93 {
94   if (!case_try_create (c, value_cnt))
95     xalloc_die ();
96 }
97
98 #ifdef GLOBAL_DEBUGGING
99 /* Initializes CLONE as a copy of ORIG. */
100 void
101 case_clone (struct ccase *clone, const struct ccase *orig)
102 {
103   assert (orig != NULL);
104   assert (orig->this == orig);
105   assert (orig->case_data != NULL);
106   assert (orig->case_data->ref_cnt > 0);
107   assert (clone != NULL);
108
109   if (clone != orig) 
110     {
111       *clone = *orig;
112       clone->this = clone;
113     }
114   orig->case_data->ref_cnt++;
115 }
116 #endif /* GLOBAL_DEBUGGING */
117
118 #ifdef GLOBAL_DEBUGGING
119 /* Replaces DST by SRC and nullifies SRC.
120    DST and SRC must be initialized cases at entry. */
121 void
122 case_move (struct ccase *dst, struct ccase *src) 
123 {
124   assert (src != NULL);
125   assert (src->this == src);
126   assert (src->case_data != NULL);
127   assert (src->case_data->ref_cnt > 0);
128   assert (dst != NULL);
129
130   *dst = *src;
131   dst->this = dst;
132   case_nullify (src);
133 }
134 #endif /* GLOBAL_DEBUGGING */
135
136 #ifdef GLOBAL_DEBUGGING
137 /* Destroys case C. */
138 void
139 case_destroy (struct ccase *c) 
140 {
141   struct case_data *cd;
142   
143   assert (c != NULL);
144   assert (c->this == c);
145
146   cd = c->case_data;
147   if (cd != NULL && --cd->ref_cnt == 0) 
148     {
149       memset (cd->values, 0xcc, sizeof *cd->values * cd->value_cnt);
150       cd->value_cnt = 0xdeadbeef;
151       free (cd); 
152     }
153 }
154 #endif /* GLOBAL_DEBUGGING */
155
156 /* Resizes case C from OLD_CNT to NEW_CNT values. */
157 void
158 case_resize (struct ccase *c, size_t old_cnt, size_t new_cnt) 
159 {
160   struct ccase new;
161
162   case_create (&new, new_cnt);
163   case_copy (&new, 0, c, 0, old_cnt < new_cnt ? old_cnt : new_cnt);
164   case_swap (&new, c);
165   case_destroy (&new);
166 }
167
168 /* Swaps cases A and B. */
169 void
170 case_swap (struct ccase *a, struct ccase *b) 
171 {
172   struct case_data *t = a->case_data;
173   a->case_data = b->case_data;
174   b->case_data = t;
175 }
176
177 /* Attempts to create C as a new case that holds VALUE_CNT
178    values.  Returns nonzero if successful, zero if memory
179    allocation failed. */
180 int
181 case_try_create (struct ccase *c, size_t value_cnt) 
182 {
183   c->case_data = malloc (case_size (value_cnt));
184   if (c->case_data != NULL) 
185     {
186 #ifdef GLOBAL_DEBUGGING
187       c->this = c;
188 #endif
189       c->case_data->value_cnt = value_cnt;
190       c->case_data->ref_cnt = 1;
191       return 1;
192     }
193   else 
194     {
195 #ifdef GLOBAL_DEBUGGING
196       c->this = c;
197 #endif
198       return 0;
199     }
200 }
201
202 /* Tries to initialize CLONE as a copy of ORIG.
203    Returns nonzero if successful, zero if memory allocation
204    failed. */
205 int
206 case_try_clone (struct ccase *clone, const struct ccase *orig) 
207 {
208   case_clone (clone, orig);
209   return 1;
210 }
211
212 #ifdef GLOBAL_DEBUGGING
213 /* Copies VALUE_CNT values from SRC (starting at SRC_IDX) to DST
214    (starting at DST_IDX). */
215 void
216 case_copy (struct ccase *dst, size_t dst_idx,
217            const struct ccase *src, size_t src_idx,
218            size_t value_cnt)
219 {
220   assert (dst != NULL);
221   assert (dst->this == dst);
222   assert (dst->case_data != NULL);
223   assert (dst->case_data->ref_cnt > 0);
224   assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
225
226   assert (src != NULL);
227   assert (src->this == src);
228   assert (src->case_data != NULL);
229   assert (src->case_data->ref_cnt > 0);
230   assert (src_idx + value_cnt <= dst->case_data->value_cnt);
231
232   if (dst->case_data->ref_cnt > 1)
233     case_unshare (dst);
234   if (dst->case_data != src->case_data || dst_idx != src_idx) 
235     memmove (dst->case_data->values + dst_idx,
236              src->case_data->values + src_idx,
237              sizeof *dst->case_data->values * value_cnt); 
238 }
239 #endif /* GLOBAL_DEBUGGING */
240
241 #ifdef GLOBAL_DEBUGGING
242 /* Copies case C to OUTPUT.
243    OUTPUT_SIZE is the number of `union values' in OUTPUT,
244    which must match the number of `union values' in C. */
245 void
246 case_to_values (const struct ccase *c, union value *output,
247                 size_t output_size UNUSED) 
248 {
249   assert (c != NULL);
250   assert (c->this == c);
251   assert (c->case_data != NULL);
252   assert (c->case_data->ref_cnt > 0);
253   assert (output_size == c->case_data->value_cnt);
254   assert (output != NULL || output_size == 0);
255
256   memcpy (output, c->case_data->values,
257           c->case_data->value_cnt * sizeof *output);
258 }
259 #endif /* GLOBAL_DEBUGGING */
260
261 #ifdef GLOBAL_DEBUGGING
262 /* Copies INPUT into case C.
263    INPUT_SIZE is the number of `union values' in INPUT,
264    which must match the number of `union values' in C. */
265 void
266 case_from_values (struct ccase *c, const union value *input,
267                   size_t input_size UNUSED) 
268 {
269   assert (c != NULL);
270   assert (c->this == c);
271   assert (c->case_data != NULL);
272   assert (c->case_data->ref_cnt > 0);
273   assert (input_size == c->case_data->value_cnt);
274   assert (input != NULL || input_size == 0);
275
276   if (c->case_data->ref_cnt > 1)
277     case_unshare (c);
278   memcpy (c->case_data->values, input,
279           c->case_data->value_cnt * sizeof *input);
280 }
281 #endif /* GLOBAL_DEBUGGING */
282
283 #ifdef GLOBAL_DEBUGGING
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. */
287 const union value *
288 case_data (const struct ccase *c, size_t idx) 
289 {
290   assert (c != NULL);
291   assert (c->this == c);
292   assert (c->case_data != NULL);
293   assert (c->case_data->ref_cnt > 0);
294   assert (idx < c->case_data->value_cnt);
295
296   return &c->case_data->values[idx];
297 }
298 #endif /* GLOBAL_DEBUGGING */
299
300 #ifdef GLOBAL_DEBUGGING
301 /* Returns the numeric value of the `union value' in C numbered
302    IDX. */
303 double
304 case_num (const struct ccase *c, size_t idx) 
305 {
306   assert (c != NULL);
307   assert (c->this == c);
308   assert (c->case_data != NULL);
309   assert (c->case_data->ref_cnt > 0);
310   assert (idx < c->case_data->value_cnt);
311
312   return c->case_data->values[idx].f;
313 }
314 #endif /* GLOBAL_DEBUGGING */
315
316 #ifdef GLOBAL_DEBUGGING
317 /* Returns the string value of the `union value' in C numbered
318    IDX.
319    (Note that the value is not null-terminated.)
320    The caller must not modify the return value. */
321 const char *
322 case_str (const struct ccase *c, size_t idx) 
323 {
324   assert (c != NULL);
325   assert (c->this == c);
326   assert (c->case_data != NULL);
327   assert (c->case_data->ref_cnt > 0);
328   assert (idx < c->case_data->value_cnt);
329
330   return c->case_data->values[idx].s;
331 }
332 #endif /* GLOBAL_DEBUGGING */
333
334 #ifdef GLOBAL_DEBUGGING
335 /* Returns a pointer to the `union value' used for the
336    element of C numbered IDX.
337    The caller is allowed to modify the returned data. */
338 union value *
339 case_data_rw (struct ccase *c, size_t idx) 
340 {
341   assert (c != NULL);
342   assert (c->this == c);
343   assert (c->case_data != NULL);
344   assert (c->case_data->ref_cnt > 0);
345   assert (idx < c->case_data->value_cnt);
346
347   if (c->case_data->ref_cnt > 1)
348     case_unshare (c);
349   return &c->case_data->values[idx];
350 }
351 #endif /* GLOBAL_DEBUGGING */
352
353 /* Compares the values of the VAR_CNT variables in VP
354    in cases A and B and returns a strcmp()-type result. */
355 int
356 case_compare (const struct ccase *a, const struct ccase *b,
357               struct variable *const *vp, size_t var_cnt)
358 {
359   return case_compare_2dict (a, b, vp, vp, var_cnt);
360 }
361
362 /* Compares the values of the VAR_CNT variables in VAP in case CA
363    to the values of the VAR_CNT variables in VBP in CB
364    and returns a strcmp()-type result. */
365 int
366 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
367                     struct variable *const *vap, struct variable *const *vbp,
368                     size_t var_cnt) 
369 {
370   for (; var_cnt-- > 0; vap++, vbp++) 
371     {
372       const struct variable *va = *vap;
373       const struct variable *vb = *vbp;
374
375       assert (va->type == vb->type);
376       assert (va->width == vb->width);
377       
378       if (va->width == 0) 
379         {
380           double af = case_num (ca, va->fv);
381           double bf = case_num (cb, vb->fv);
382
383           if (af != bf) 
384             return af > bf ? 1 : -1;
385         }
386       else 
387         {
388           const char *as = case_str (ca, va->fv);
389           const char *bs = case_str (cb, vb->fv);
390           int cmp = memcmp (as, bs, va->width);
391
392           if (cmp != 0)
393             return cmp;
394         }
395     }
396   return 0;
397 }
398
399 /* Returns a pointer to the array of `union value's used for C.
400    The caller must *not* modify the returned data.
401
402    NOTE: This function breaks the case abstraction.  It should
403    *not* be used often.  Prefer the other case functions. */
404 const union value *
405 case_data_all (const struct ccase *c) 
406 {
407   assert (c != NULL);
408   assert (c->this == c);
409   assert (c->case_data != NULL);
410   assert (c->case_data->ref_cnt > 0);
411
412   return c->case_data->values;
413 }
414
415 /* Returns a pointer to the array of `union value's used for C.
416    The caller is allowed to modify the returned data.
417
418    NOTE: This function breaks the case abstraction.  It should
419    *not* be used often.  Prefer the other case functions. */
420 union value *
421 case_data_all_rw (struct ccase *c) 
422 {
423   assert (c != NULL);
424   assert (c->this == c);
425   assert (c->case_data != NULL);
426   assert (c->case_data->ref_cnt > 0);
427
428   if (c->case_data->ref_cnt > 1)
429     case_unshare (c);
430   return c->case_data->values;
431 }