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