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