Patch #6262. New developers guide and resulting fixes and cleanups.
[pspp-builds.git] / src / data / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "value-labels.h"
20
21 #include <stdlib.h>
22
23 #include <data/data-out.h>
24 #include <data/format.h>
25 #include <data/value.h>
26 #include <data/variable.h>
27 #include <libpspp/compiler.h>
28 #include <libpspp/hash.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
31
32 #include "xalloc.h"
33
34 static hsh_compare_func compare_int_val_lab;
35 static hsh_hash_func hash_int_val_lab;
36 static hsh_free_func free_int_val_lab;
37
38 struct atom;
39 static struct atom *atom_create (const char *string);
40 static void atom_destroy (struct atom *);
41 static char *atom_to_string (const struct atom *);
42
43 /* A set of value labels. */
44 struct val_labs
45   {
46     int width;                  /* 0=numeric, otherwise string width. */
47     struct hsh_table *labels;   /* Hash table of `struct int_val_lab's. */
48   };
49
50 /* Creates and returns a new, empty set of value labels with the
51    given WIDTH.  To actually add any value labels, WIDTH must be
52    a numeric or short string width. */
53 struct val_labs *
54 val_labs_create (int width)
55 {
56   struct val_labs *vls;
57
58   assert (width >= 0);
59
60   vls = xmalloc (sizeof *vls);
61   vls->width = width;
62   vls->labels = NULL;
63   return vls;
64 }
65
66 /* Creates and returns a new set of value labels identical to
67    VLS. */
68 struct val_labs *
69 val_labs_clone (const struct val_labs *vls)
70 {
71   struct val_labs *copy;
72   struct val_labs_iterator *i;
73   struct val_lab *vl;
74
75   if (vls == NULL)
76     return NULL;
77
78   copy = val_labs_create (vls->width);
79   for (vl = val_labs_first (vls, &i); vl != NULL;
80        vl = val_labs_next (vls, &i))
81     val_labs_add (copy, vl->value, vl->label);
82   return copy;
83 }
84
85 /* Determines whether VLS's width can be changed to NEW_WIDTH,
86    using the rules checked by value_is_resizable. */
87 bool
88 val_labs_can_set_width (const struct val_labs *vls, int new_width)
89 {
90   struct val_labs_iterator *i;
91   struct val_lab *lab;
92
93   for (lab = val_labs_first (vls, &i); lab != NULL;
94        lab = val_labs_next (vls, &i))
95     if (!value_is_resizable (&lab->value, vls->width, new_width))
96       {
97         val_labs_done (&i);
98         return false;
99       }
100
101   return true;
102 }
103
104 /* Changes the width of VLS to NEW_WIDTH.  The original and new
105    width must be both numeric or both string.  If the new width
106    is a long string width, then any value labels in VLS are
107    deleted. */
108 void
109 val_labs_set_width (struct val_labs *vls, int new_width)
110 {
111   assert (val_labs_can_set_width (vls, new_width));
112
113   vls->width = new_width;
114   if (new_width > MAX_SHORT_STRING)
115     val_labs_clear (vls);
116 }
117
118 /* Destroys VLS. */
119 void
120 val_labs_destroy (struct val_labs *vls)
121 {
122   if (vls != NULL)
123     {
124       hsh_destroy (vls->labels);
125       free (vls);
126     }
127 }
128
129 /* Removes all the value labels from VLS. */
130 void
131 val_labs_clear (struct val_labs *vls)
132 {
133   assert (vls != NULL);
134
135   hsh_destroy (vls->labels);
136   vls->labels = NULL;
137 }
138
139 /* Returns the number of value labels in VLS. */
140 size_t
141 val_labs_count (const struct val_labs *vls)
142 {
143   return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
144 }
145 \f
146 /* One value label in internal format. */
147 struct int_val_lab
148   {
149     union value value;          /* The value being labeled. */
150     struct atom *label;         /* A ref-counted string. */
151   };
152
153 /* Creates and returns an int_val_lab based on VALUE and
154    LABEL. */
155 static struct int_val_lab *
156 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
157 {
158   struct int_val_lab *ivl;
159
160   assert (label != NULL);
161   assert (vls->width <= MAX_SHORT_STRING);
162
163   ivl = xmalloc (sizeof *ivl);
164   ivl->value = value;
165   if (vls->width > 0)
166     memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
167   ivl->label = atom_create (label);
168
169   return ivl;
170 }
171
172 /* If VLS does not already contain a value label for VALUE (and
173    VLS represents a numeric or short string set of value labels),
174    adds LABEL for it and returns true.  Otherwise, returns
175    false. */
176 bool
177 val_labs_add (struct val_labs *vls, union value value, const char *label)
178 {
179   assert (label != NULL);
180   if (vls->width < MIN_LONG_STRING)
181     {
182       struct int_val_lab *ivl;
183       void **vlpp;
184
185       if (vls->labels == NULL)
186         vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
187                                   free_int_val_lab, vls);
188
189       ivl = create_int_val_lab (vls, value, label);
190       vlpp = hsh_probe (vls->labels, ivl);
191       if (*vlpp == NULL)
192         {
193           *vlpp = ivl;
194           return true;
195         }
196       free_int_val_lab (ivl, vls);
197     }
198   return false;
199 }
200
201 /* Sets LABEL as the value label for VALUE in VLS, replacing any
202    existing label for VALUE.  Has no effect if VLS has a long
203    string width. */
204 void
205 val_labs_replace (struct val_labs *vls, union value value, const char *label)
206 {
207   if (vls->width < MIN_LONG_STRING)
208     {
209       if (vls->labels != NULL)
210         {
211           struct int_val_lab *new = create_int_val_lab (vls, value, label);
212           struct int_val_lab *old = hsh_replace (vls->labels, new);
213           if (old != NULL)
214             free_int_val_lab (old, vls);
215         }
216       else
217         val_labs_add (vls, value, label);
218     }
219 }
220
221 /* Removes any value label for VALUE within VLS.  Returns true
222    if a value label was removed. */
223 bool
224 val_labs_remove (struct val_labs *vls, union value value)
225 {
226   if (vls->width < MIN_LONG_STRING && vls->labels != NULL)
227     {
228       struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
229       int deleted = hsh_delete (vls->labels, ivl);
230       free (ivl);
231       return deleted;
232     }
233   else
234     return false;
235 }
236
237 /* Searches VLS for a value label for VALUE.  If successful,
238    returns the label; otherwise, returns a null pointer.  If
239    VLS's width is greater than MAX_SHORT_STRING, always returns a
240    null pointer. */
241 char *
242 val_labs_find (const struct val_labs *vls, union value value)
243 {
244   if (vls != NULL
245       && vls->width <= MAX_SHORT_STRING
246       && vls->labels != NULL)
247     {
248       struct int_val_lab ivl, *vlp;
249
250       ivl.value = value;
251       vlp = hsh_find (vls->labels, &ivl);
252       if (vlp != NULL)
253         return atom_to_string (vlp->label);
254     }
255   return NULL;
256 }
257 \f
258 /* A value labels iterator. */
259 struct val_labs_iterator
260   {
261     void **labels;              /* The labels, in order. */
262     void **lp;                  /* Current label. */
263     struct val_lab vl;          /* Structure presented to caller. */
264   };
265
266 /* Sets up *IP for iterating through the value labels in VLS in
267    no particular order.  Returns the first value label or a null
268    pointer if VLS is empty.  If the return value is non-null,
269    then val_labs_next() may be used to continue iterating or
270    val_labs_done() to free up the iterator.  Otherwise, neither
271    function may be called for *IP. */
272 struct val_lab *
273 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
274 {
275   struct val_labs_iterator *i;
276
277   assert (vls != NULL);
278   assert (ip != NULL);
279
280   if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
281     {
282       *ip = NULL;
283       return NULL;
284     }
285
286   i = *ip = xmalloc (sizeof *i);
287   i->labels = hsh_data_copy (vls->labels);
288   i->lp = i->labels;
289   return val_labs_next (vls, ip);
290 }
291
292 /* Sets up *IP for iterating through the value labels in VLS in
293    sorted order of values.  Returns the first value label or a
294    null pointer if VLS is empty.  If the return value is
295    non-null, then val_labs_next() may be used to continue
296    iterating or val_labs_done() to free up the iterator.
297    Otherwise, neither function may be called for *IP. */
298 struct val_lab *
299 val_labs_first_sorted (const struct val_labs *vls,
300                        struct val_labs_iterator **ip)
301 {
302   struct val_labs_iterator *i;
303
304   assert (vls != NULL);
305   assert (ip != NULL);
306
307   if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
308     {
309       *ip = NULL;
310       return NULL;
311     }
312
313   i = *ip = xmalloc (sizeof *i);
314   i->lp = i->labels = hsh_sort_copy (vls->labels);
315   return val_labs_next (vls, ip);
316 }
317
318 /* Returns the next value label in an iteration begun by
319    val_labs_first() or val_labs_first_sorted().  If the return
320    value is non-null, then val_labs_next() may be used to
321    continue iterating or val_labs_done() to free up the iterator.
322    Otherwise, neither function may be called for *IP. */
323 struct val_lab *
324 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
325 {
326   struct val_labs_iterator *i;
327   struct int_val_lab *ivl;
328
329   assert (vls != NULL);
330   assert (vls->width <= MAX_SHORT_STRING);
331   assert (ip != NULL);
332   assert (*ip != NULL);
333
334   i = *ip;
335   ivl = *i->lp++;
336   if (ivl != NULL)
337     {
338       i->vl.value = ivl->value;
339       i->vl.label = atom_to_string (ivl->label);
340       return &i->vl;
341     }
342   else
343     {
344       free (i->labels);
345       free (i);
346       *ip = NULL;
347       return NULL;
348     }
349 }
350
351 /* Discards the state for an incomplete iteration begun by
352    val_labs_first() or val_labs_first_sorted(). */
353 void
354 val_labs_done (struct val_labs_iterator **ip)
355 {
356   if (*ip != NULL)
357     {
358       struct val_labs_iterator *i = *ip;
359       free (i->labels);
360       free (i);
361       *ip = NULL;
362     }
363 }
364 \f
365 /* Compares two value labels and returns a strcmp()-type result. */
366 int
367 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
368 {
369   const struct int_val_lab *a = a_;
370   const struct int_val_lab *b = b_;
371   const struct val_labs *vls = vls_;
372
373   if (vls->width == 0)
374     return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
375   else
376     return memcmp (a->value.s, b->value.s, vls->width);
377 }
378
379 /* Hash a value label. */
380 unsigned
381 hash_int_val_lab (const void *vl_, const void *vls_)
382 {
383   const struct int_val_lab *vl = vl_;
384   const struct val_labs *vls = vls_;
385
386   if (vls->width == 0)
387     return hsh_hash_double (vl->value.f);
388   else
389     return hsh_hash_bytes (vl->value.s, vls->width);
390 }
391
392 /* Free a value label. */
393 void
394 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
395 {
396   struct int_val_lab *vl = vl_;
397
398   atom_destroy (vl->label);
399   free (vl);
400 }
401 \f
402 /* Atoms. */
403
404 /* An atom. */
405 struct atom
406   {
407     char *string;               /* String value. */
408     unsigned ref_count;         /* Number of references. */
409   };
410
411 static hsh_compare_func compare_atoms;
412 static hsh_hash_func hash_atom;
413 static hsh_free_func free_atom;
414
415 /* Hash table of atoms. */
416 static struct hsh_table *atoms;
417
418 static void
419 destroy_atoms (void)
420 {
421   hsh_destroy (atoms);
422 }
423
424 /* Creates and returns an atom for STRING. */
425 static struct atom *
426 atom_create (const char *string)
427 {
428   struct atom a;
429   void **app;
430
431   assert (string != NULL);
432
433   if (atoms == NULL)
434     {
435       atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
436       atexit (destroy_atoms);
437     }
438
439   a.string = (char *) string;
440   app = hsh_probe (atoms, &a);
441   if (*app != NULL)
442     {
443       struct atom *ap = *app;
444       ap->ref_count++;
445       return ap;
446     }
447   else
448     {
449       struct atom *ap = xmalloc (sizeof *ap);
450       ap->string = xstrdup (string);
451       ap->ref_count = 1;
452       *app = ap;
453       return ap;
454     }
455 }
456
457 /* Destroys ATOM. */
458 static void
459 atom_destroy (struct atom *atom)
460 {
461   if (atom != NULL)
462     {
463       assert (atom->ref_count > 0);
464       atom->ref_count--;
465       if (atom->ref_count == 0)
466         hsh_force_delete (atoms, atom);
467     }
468 }
469
470 /* Returns the string associated with ATOM. */
471 static  char *
472 atom_to_string (const struct atom *atom)
473 {
474   assert (atom != NULL);
475
476   return atom->string;
477 }
478
479 /* A hsh_compare_func that compares A and B. */
480 static int
481 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
482 {
483   const struct atom *a = a_;
484   const struct atom *b = b_;
485
486   return strcmp (a->string, b->string);
487 }
488
489 /* A hsh_hash_func that hashes ATOM. */
490 static unsigned
491 hash_atom (const void *atom_, const void *aux UNUSED)
492 {
493   const struct atom *atom = atom_;
494
495   return hsh_hash_string (atom->string);
496 }
497
498 /* A hsh_free_func that destroys ATOM. */
499 static void
500 free_atom (void *atom_, const void *aux UNUSED)
501 {
502   struct atom *atom = atom_;
503
504   free (atom->string);
505   free (atom);
506 }