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