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