1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
22 #include "value-labels.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>
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;
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 *);
42 /* A set of value labels. */
45 int width; /* 0=numeric, otherwise string width. */
46 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
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. */
53 val_labs_create (int width)
59 vls = xmalloc (sizeof *vls);
65 /* Creates and returns a new set of value labels identical to
68 val_labs_copy (const struct val_labs *vls)
70 struct val_labs *copy;
71 struct val_labs_iterator *i;
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);
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. */
88 val_labs_can_set_width (const struct val_labs *vls, int new_width)
90 assert ((vls->width == 0) == (new_width == 0));
93 return new_width == 0;
94 else if (new_width < vls->width)
96 struct val_labs_iterator *i;
99 for (lab = val_labs_first (vls, &i); lab != NULL;
100 lab = val_labs_next (vls, &i))
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] != ' ')
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. */
123 val_labs_set_width (struct val_labs *vls, int new_width)
125 assert (val_labs_can_set_width (vls, new_width));
127 vls->width = new_width;
128 if (new_width > MAX_SHORT_STRING)
129 val_labs_clear (vls);
134 val_labs_destroy (struct val_labs *vls)
138 hsh_destroy (vls->labels);
143 /* Removes all the value labels from VLS. */
145 val_labs_clear (struct val_labs *vls)
147 assert (vls != NULL);
149 hsh_destroy (vls->labels);
153 /* Returns the number of value labels in VLS. */
155 val_labs_count (const struct val_labs *vls)
157 assert (vls != NULL);
159 if (vls->labels == NULL)
162 return hsh_count (vls->labels);
165 /* One value label in internal format. */
168 union value value; /* The value being labeled. */
169 struct atom *label; /* A ref-counted string. */
172 /* Creates and returns an int_val_lab based on VALUE and
174 static struct int_val_lab *
175 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
177 struct int_val_lab *ivl;
179 assert (label != NULL);
180 assert (vls->width <= MAX_SHORT_STRING);
182 ivl = xmalloc (sizeof *ivl);
185 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
186 ivl->label = atom_create (label);
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
196 val_labs_add (struct val_labs *vls, union value value, const char *label)
198 struct int_val_lab *ivl;
201 assert (vls != NULL);
202 assert (vls->width <= MAX_SHORT_STRING);
203 assert (label != NULL);
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);
209 ivl = create_int_val_lab (vls, value, label);
210 vlpp = hsh_probe (vls->labels, ivl);
216 free_int_val_lab (ivl, vls);
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. */
225 val_labs_replace (struct val_labs *vls, union value value, const char *label)
227 struct int_val_lab *ivl;
229 assert (vls != NULL);
230 assert (vls->width <= MAX_SHORT_STRING);
231 assert (label != NULL);
233 if (vls->labels == NULL)
235 val_labs_add (vls, value, label);
239 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
244 free_int_val_lab (ivl, vls);
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. */
253 val_labs_remove (struct val_labs *vls, union value value)
255 assert (vls != NULL);
256 assert (vls->width <= MAX_SHORT_STRING);
258 if (vls->labels != NULL)
260 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
261 int deleted = hsh_delete (vls->labels, ivl);
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
274 val_labs_find (const struct val_labs *vls, union value value)
276 assert (vls != NULL);
278 if (vls->width > MAX_SHORT_STRING)
281 if (vls->labels != NULL)
283 struct int_val_lab ivl, *vlp;
286 vlp = hsh_find (vls->labels, &ivl);
288 return atom_to_string (vlp->label);
293 /* A value labels iterator. */
294 struct val_labs_iterator
296 void **labels; /* The labels, in order. */
297 void **lp; /* Current label. */
298 struct val_lab vl; /* Structure presented to caller. */
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. */
308 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
310 struct val_labs_iterator *i;
312 assert (vls != NULL);
315 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
318 i = *ip = xmalloc (sizeof *i);
319 i->labels = hsh_data_copy (vls->labels);
321 return val_labs_next (vls, ip);
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. */
331 val_labs_first_sorted (const struct val_labs *vls,
332 struct val_labs_iterator **ip)
334 struct val_labs_iterator *i;
336 assert (vls != NULL);
339 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
342 i = *ip = xmalloc (sizeof *i);
343 i->lp = i->labels = hsh_sort_copy (vls->labels);
344 return val_labs_next (vls, ip);
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. */
353 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
355 struct val_labs_iterator *i;
356 struct int_val_lab *ivl;
358 assert (vls != NULL);
359 assert (vls->width <= MAX_SHORT_STRING);
361 assert (*ip != NULL);
367 i->vl.value = ivl->value;
368 i->vl.label = atom_to_string (ivl->label);
380 /* Discards the state for an incomplete iteration begun by
381 val_labs_first() or val_labs_first_sorted(). */
383 val_labs_done (struct val_labs_iterator **ip)
385 struct val_labs_iterator *i;
388 assert (*ip != NULL);
396 /* Compares two value labels and returns a strcmp()-type result. */
398 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
400 const struct int_val_lab *a = a_;
401 const struct int_val_lab *b = b_;
402 const struct val_labs *vls = vls_;
405 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
407 return memcmp (a->value.s, b->value.s, vls->width);
410 /* Hash a value label. */
412 hash_int_val_lab (const void *vl_, const void *vls_)
414 const struct int_val_lab *vl = vl_;
415 const struct val_labs *vls = vls_;
418 return hsh_hash_double (vl->value.f);
420 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
423 /* Free a value label. */
425 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
427 struct int_val_lab *vl = vl_;
429 atom_destroy (vl->label);
438 char *string; /* String value. */
439 unsigned ref_count; /* Number of references. */
442 static hsh_compare_func compare_atoms;
443 static hsh_hash_func hash_atom;
444 static hsh_free_func free_atom;
446 /* Hash table of atoms. */
447 static struct hsh_table *atoms;
449 /* Creates and returns an atom for STRING. */
451 atom_create (const char *string)
456 assert (string != NULL);
459 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
461 a.string = (char *) string;
462 app = hsh_probe (atoms, &a);
465 struct atom *ap = *app;
471 struct atom *ap = xmalloc (sizeof *ap);
472 ap->string = xstrdup (string);
481 atom_destroy (struct atom *atom)
485 assert (atom->ref_count > 0);
487 if (atom->ref_count == 0)
488 hsh_force_delete (atoms, atom);
492 /* Returns the string associated with ATOM. */
494 atom_to_string (const struct atom *atom)
496 assert (atom != NULL);
501 /* A hsh_compare_func that compares A and B. */
503 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
505 const struct atom *a = a_;
506 const struct atom *b = b_;
508 return strcmp (a->string, b->string);
511 /* A hsh_hash_func that hashes ATOM. */
513 hash_atom (const void *atom_, const void *aux UNUSED)
515 const struct atom *atom = atom_;
517 return hsh_hash_string (atom->string);
520 /* A hsh_free_func that destroys ATOM. */
522 free_atom (void *atom_, const void *aux UNUSED)
524 struct atom *atom = atom_;
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
537 value_to_string (const union value *val, const struct variable *var)
541 assert (val != NULL);
542 assert (var != NULL);
544 s = val_labs_find (var->val_labs, *val);
547 static char buf[MAX_STRING + 1];
548 data_out (buf, &var->print, val);
549 buf[var->print.w] = '\0';