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/data-out.h>
27 #include <data/variable.h>
28 #include <libpspp/alloc.h>
29 #include <libpspp/compiler.h>
30 #include <libpspp/hash.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
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;
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 *);
43 /* A set of value labels. */
46 int width; /* 0=numeric, otherwise string width. */
47 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
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. */
54 val_labs_create (int width)
60 vls = xmalloc (sizeof *vls);
66 /* Creates and returns a new set of value labels identical to
69 val_labs_copy (const struct val_labs *vls)
71 struct val_labs *copy;
72 struct val_labs_iterator *i;
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);
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. */
89 val_labs_can_set_width (const struct val_labs *vls, int new_width)
91 assert ((vls->width == 0) == (new_width == 0));
94 return new_width == 0;
95 else if (new_width < vls->width)
97 struct val_labs_iterator *i;
100 for (lab = val_labs_first (vls, &i); lab != NULL;
101 lab = val_labs_next (vls, &i))
105 /* We can shorten the value labels only if all the
106 truncated characters are blanks. */
107 for (j = vls->width; j < new_width; j++)
108 if (lab->value.s[j] != ' ')
120 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
121 NEW_WIDTH must be 0, otherwise it must be within the range
122 1...MAX_SHORT_STRING inclusive. */
124 val_labs_set_width (struct val_labs *vls, int new_width)
126 assert (val_labs_can_set_width (vls, new_width));
128 vls->width = new_width;
129 if (new_width > MAX_SHORT_STRING)
130 val_labs_clear (vls);
135 val_labs_destroy (struct val_labs *vls)
139 hsh_destroy (vls->labels);
144 /* Removes all the value labels from VLS. */
146 val_labs_clear (struct val_labs *vls)
148 assert (vls != NULL);
150 hsh_destroy (vls->labels);
154 /* Returns the number of value labels in VLS. */
156 val_labs_count (const struct val_labs *vls)
158 assert (vls != NULL);
160 if (vls->labels == NULL)
163 return hsh_count (vls->labels);
166 /* One value label in internal format. */
169 union value value; /* The value being labeled. */
170 struct atom *label; /* A ref-counted string. */
173 /* Creates and returns an int_val_lab based on VALUE and
175 static struct int_val_lab *
176 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
178 struct int_val_lab *ivl;
180 assert (label != NULL);
181 assert (vls->width <= MAX_SHORT_STRING);
183 ivl = xmalloc (sizeof *ivl);
186 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
187 ivl->label = atom_create (label);
192 /* If VLS does not already contain a value label for VALUE, adds
193 LABEL for it and returns true. Otherwise, returns false.
194 Behavior is undefined if VLS's width is greater than
197 val_labs_add (struct val_labs *vls, union value value, const char *label)
199 struct int_val_lab *ivl;
202 assert (vls != NULL);
203 assert (vls->width <= MAX_SHORT_STRING);
204 assert (label != NULL);
206 if (vls->labels == NULL)
207 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
208 free_int_val_lab, vls);
210 ivl = create_int_val_lab (vls, value, label);
211 vlpp = hsh_probe (vls->labels, ivl);
217 free_int_val_lab (ivl, vls);
221 /* Sets LABEL as the value label for VALUE in VLS. Returns false
222 if there wasn't already a value label for VALUE, or true if
223 there was. Behavior is undefined if VLS's width is greater
224 than MAX_SHORT_STRING. */
226 val_labs_replace (struct val_labs *vls, union value value, const char *label)
228 struct int_val_lab *ivl;
230 assert (vls != NULL);
231 assert (vls->width <= MAX_SHORT_STRING);
232 assert (label != NULL);
234 if (vls->labels == NULL)
236 val_labs_add (vls, value, label);
240 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
245 free_int_val_lab (ivl, vls);
250 /* Removes any value label for VALUE within VLS. Returns true
251 if a value label was removed. Behavior is undefined if VLS's
252 width is greater than MAX_SHORT_STRING. */
254 val_labs_remove (struct val_labs *vls, union value value)
256 assert (vls != NULL);
257 assert (vls->width <= MAX_SHORT_STRING);
259 if (vls->labels != NULL)
261 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
262 int deleted = hsh_delete (vls->labels, ivl);
270 /* Searches VLS for a value label for VALUE. If successful,
271 returns the label; otherwise, returns a null pointer. If
272 VLS's width is greater than MAX_SHORT_STRING, always returns a
275 val_labs_find (const struct val_labs *vls, union value value)
277 assert (vls != NULL);
279 if (vls->width > MAX_SHORT_STRING)
282 if (vls->labels != NULL)
284 struct int_val_lab ivl, *vlp;
287 vlp = hsh_find (vls->labels, &ivl);
289 return atom_to_string (vlp->label);
294 /* A value labels iterator. */
295 struct val_labs_iterator
297 void **labels; /* The labels, in order. */
298 void **lp; /* Current label. */
299 struct val_lab vl; /* Structure presented to caller. */
302 /* Sets up *IP for iterating through the value labels in VLS in
303 no particular order. Returns the first value label or a null
304 pointer if VLS is empty. If the return value is non-null,
305 then val_labs_next() may be used to continue iterating or
306 val_labs_done() to free up the iterator. Otherwise, neither
307 function may be called for *IP. */
309 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
311 struct val_labs_iterator *i;
313 assert (vls != NULL);
316 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
319 i = *ip = xmalloc (sizeof *i);
320 i->labels = hsh_data_copy (vls->labels);
322 return val_labs_next (vls, ip);
325 /* Sets up *IP for iterating through the value labels in VLS in
326 sorted order of values. Returns the first value label or a
327 null pointer if VLS is empty. If the return value is
328 non-null, then val_labs_next() may be used to continue
329 iterating or val_labs_done() to free up the iterator.
330 Otherwise, neither function may be called for *IP. */
332 val_labs_first_sorted (const struct val_labs *vls,
333 struct val_labs_iterator **ip)
335 struct val_labs_iterator *i;
337 assert (vls != NULL);
340 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
343 i = *ip = xmalloc (sizeof *i);
344 i->lp = i->labels = hsh_sort_copy (vls->labels);
345 return val_labs_next (vls, ip);
348 /* Returns the next value label in an iteration begun by
349 val_labs_first() or val_labs_first_sorted(). If the return
350 value is non-null, then val_labs_next() may be used to
351 continue iterating or val_labs_done() to free up the iterator.
352 Otherwise, neither function may be called for *IP. */
354 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
356 struct val_labs_iterator *i;
357 struct int_val_lab *ivl;
359 assert (vls != NULL);
360 assert (vls->width <= MAX_SHORT_STRING);
362 assert (*ip != NULL);
368 i->vl.value = ivl->value;
369 i->vl.label = atom_to_string (ivl->label);
381 /* Discards the state for an incomplete iteration begun by
382 val_labs_first() or val_labs_first_sorted(). */
384 val_labs_done (struct val_labs_iterator **ip)
386 struct val_labs_iterator *i;
389 assert (*ip != NULL);
397 /* Compares two value labels and returns a strcmp()-type result. */
399 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
401 const struct int_val_lab *a = a_;
402 const struct int_val_lab *b = b_;
403 const struct val_labs *vls = vls_;
406 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
408 return memcmp (a->value.s, b->value.s, vls->width);
411 /* Hash a value label. */
413 hash_int_val_lab (const void *vl_, const void *vls_)
415 const struct int_val_lab *vl = vl_;
416 const struct val_labs *vls = vls_;
419 return hsh_hash_double (vl->value.f);
421 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
424 /* Free a value label. */
426 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
428 struct int_val_lab *vl = vl_;
430 atom_destroy (vl->label);
439 char *string; /* String value. */
440 unsigned ref_count; /* Number of references. */
443 static hsh_compare_func compare_atoms;
444 static hsh_hash_func hash_atom;
445 static hsh_free_func free_atom;
447 /* Hash table of atoms. */
448 static struct hsh_table *atoms;
450 /* Creates and returns an atom for STRING. */
452 atom_create (const char *string)
457 assert (string != NULL);
460 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
462 a.string = (char *) string;
463 app = hsh_probe (atoms, &a);
466 struct atom *ap = *app;
472 struct atom *ap = xmalloc (sizeof *ap);
473 ap->string = xstrdup (string);
482 atom_destroy (struct atom *atom)
486 assert (atom->ref_count > 0);
488 if (atom->ref_count == 0)
489 hsh_force_delete (atoms, atom);
493 /* Returns the string associated with ATOM. */
495 atom_to_string (const struct atom *atom)
497 assert (atom != NULL);
502 /* A hsh_compare_func that compares A and B. */
504 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
506 const struct atom *a = a_;
507 const struct atom *b = b_;
509 return strcmp (a->string, b->string);
512 /* A hsh_hash_func that hashes ATOM. */
514 hash_atom (const void *atom_, const void *aux UNUSED)
516 const struct atom *atom = atom_;
518 return hsh_hash_string (atom->string);
521 /* A hsh_free_func that destroys ATOM. */
523 free_atom (void *atom_, const void *aux UNUSED)
525 struct atom *atom = atom_;
532 /* Get a string representing the value.
533 That is, if it has a label, then return that label,
534 otherwise, if the value is alpha, then return the string for it,
535 else format it and return the formatted string
538 value_to_string (const union value *val, const struct variable *var)
542 assert (val != NULL);
543 assert (var != NULL);
545 s = val_labs_find (var->val_labs, *val);
548 static char buf[MAX_STRING + 1];
549 data_out (val, &var->print, buf);
550 buf[var->print.w] = '\0';