Changed a lot of non-const pointers to const.
[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 case C to OUTPUT.
214    OUTPUT_SIZE is the number of `union values' in OUTPUT,
215    which must match the number of `union values' in C. */
216 void
217 case_to_values (const struct ccase *c, union value *output,
218                 size_t output_size UNUSED) 
219 {
220   assert (c->case_data->ref_cnt > 0);
221   assert (output_size == c->case_data->value_cnt);
222   assert (output != NULL || output_size == 0);
223
224   memcpy (output, c->case_data->values,
225           c->case_data->value_cnt * sizeof *output);
226 }
227
228 /* Copies INPUT into case C.
229    INPUT_SIZE is the number of `union values' in INPUT,
230    which must match the number of `union values' in C. */
231 void
232 case_from_values (struct ccase *c, const union value *input,
233                   size_t input_size UNUSED) 
234 {
235   assert (c->case_data->ref_cnt > 0);
236   assert (input_size == c->case_data->value_cnt);
237   assert (input != NULL || input_size == 0);
238
239   case_unshare (c);
240   memcpy (c->case_data->values, input,
241           c->case_data->value_cnt * sizeof *input);
242 }
243
244 /* Returns a pointer to the `union value' used for the
245    element of C for variable V.
246    Case C must be drawn from V's dictionary.
247    The caller must not modify the returned data. */
248 const union value *
249 case_data (const struct ccase *c, const struct variable *v)
250 {
251   return case_data_idx (c, var_get_case_index (v));
252 }
253
254 /* Returns the numeric value of the `union value' in C for
255    variable V.
256    Case C must be drawn from V's dictionary. */
257 double
258 case_num (const struct ccase *c, const struct variable *v) 
259 {
260   return case_num_idx (c, var_get_case_index (v));
261 }
262
263 /* Returns the string value of the `union value' in C for
264    variable V.
265    Case C must be drawn from V's dictionary.
266    (Note that the value is not null-terminated.)
267    The caller must not modify the return value. */
268 const char *
269 case_str (const struct ccase *c, const struct variable *v) 
270 {
271   return case_str_idx (c, var_get_case_index (v));
272 }
273
274 /* Returns a pointer to the `union value' used for the
275    element of C for variable V.
276    Case C must be drawn from V's dictionary.   
277    The caller is allowed to modify the returned data. */
278 union value *
279 case_data_rw (struct ccase *c, const struct variable *v) 
280 {
281   return case_data_rw_idx (c, var_get_case_index (v));
282 }
283
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_idx (const struct ccase *c, size_t idx) 
289 {
290   assert (c->case_data->ref_cnt > 0);
291   assert (idx < c->case_data->value_cnt);
292
293   return &c->case_data->values[idx];
294 }
295
296 /* Returns the numeric value of the `union value' in C numbered
297    IDX. */
298 double
299 case_num_idx (const struct ccase *c, size_t idx) 
300 {
301   assert (c->case_data->ref_cnt > 0);
302   assert (idx < c->case_data->value_cnt);
303
304   return c->case_data->values[idx].f;
305 }
306
307 /* Returns the string value of the `union value' in C numbered
308    IDX.
309    (Note that the value is not null-terminated.)
310    The caller must not modify the return value. */
311 const char *
312 case_str_idx (const struct ccase *c, size_t idx) 
313 {
314   assert (c->case_data->ref_cnt > 0);
315   assert (idx < c->case_data->value_cnt);
316
317   return c->case_data->values[idx].s;
318 }
319
320 /* Returns a pointer to the `union value' used for the
321    element of C numbered IDX.
322    The caller is allowed to modify the returned data. */
323 union value *
324 case_data_rw_idx (struct ccase *c, size_t idx) 
325 {
326   assert (c->case_data->ref_cnt > 0);
327   assert (idx < c->case_data->value_cnt);
328
329   case_unshare (c);
330   return &c->case_data->values[idx];
331 }
332
333 /* Compares the values of the VAR_CNT variables in VP
334    in cases A and B and returns a strcmp()-type result. */
335 int
336 case_compare (const struct ccase *a, const struct ccase *b,
337               const struct variable *const *vp, size_t var_cnt)
338 {
339   return case_compare_2dict (a, b, vp, vp, var_cnt);
340 }
341
342 /* Compares the values of the VAR_CNT variables in VAP in case CA
343    to the values of the VAR_CNT variables in VBP in CB
344    and returns a strcmp()-type result. */
345 int
346 case_compare_2dict (const struct ccase *ca, const struct ccase *cb,
347                     const struct variable *const *vap, 
348                 const struct variable *const *vbp,
349                     size_t var_cnt) 
350 {
351   for (; var_cnt-- > 0; vap++, vbp++) 
352     {
353       const struct variable *va = *vap;
354       const struct variable *vb = *vbp;
355
356       assert (var_get_width (va) == var_get_width (vb));
357       
358       if (var_get_width (va) == 0) 
359         {
360           double af = case_num (ca, va);
361           double bf = case_num (cb, vb);
362
363           if (af != bf) 
364             return af > bf ? 1 : -1;
365         }
366       else 
367         {
368           const char *as = case_str (ca, va);
369           const char *bs = case_str (cb, vb);
370           int cmp = memcmp (as, bs, var_get_width (va));
371
372           if (cmp != 0)
373             return cmp;
374         }
375     }
376   return 0;
377 }
378
379 /* Returns a pointer to the array of `union value's used for C.
380    The caller must *not* modify the returned data.
381
382    NOTE: This function breaks the case abstraction.  It should
383    *not* be used often.  Prefer the other case functions. */
384 const union value *
385 case_data_all (const struct ccase *c) 
386 {
387   assert (c->case_data->ref_cnt > 0);
388
389   return c->case_data->values;
390 }
391
392 /* Returns a pointer to the array of `union value's used for C.
393    The caller is allowed to modify the returned data.
394
395    NOTE: This function breaks the case abstraction.  It should
396    *not* be used often.  Prefer the other case functions. */
397 union value *
398 case_data_all_rw (struct ccase *c) 
399 {
400   assert (c->case_data->ref_cnt > 0);
401
402   case_unshare (c);
403   return c->case_data->values;
404 }