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