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