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