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 nonzero. Otherwise, returns zero.
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);
218 free_int_val_lab (ivl, vls);
223 /* Sets LABEL as the value label for VALUE in VLS. Returns zero
224 if there wasn't already a value label for VALUE, or nonzero if
225 there was. Behavior is undefined if VLS's width is greater
226 than MAX_SHORT_STRING. */
228 val_labs_replace (struct val_labs *vls, union value value, const char *label)
230 struct int_val_lab *ivl;
232 assert (vls != NULL);
233 assert (vls->width <= MAX_SHORT_STRING);
234 assert (label != NULL);
236 if (vls->labels == NULL)
238 val_labs_add (vls, value, label);
242 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
247 free_int_val_lab (ivl, vls);
252 /* Removes any value label for VALUE within VLS. Returns nonzero
253 if a value label was removed. Behavior is undefined if VLS's
254 width is greater than MAX_SHORT_STRING. */
256 val_labs_remove (struct val_labs *vls, union value value)
258 assert (vls != NULL);
259 assert (vls->width <= MAX_SHORT_STRING);
261 if (vls->labels != NULL)
263 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
264 int deleted = hsh_delete (vls->labels, ivl);
272 /* Searches VLS for a value label for VALUE. If successful,
273 returns the label; otherwise, returns a null pointer. If
274 VLS's width is greater than MAX_SHORT_STRING, always returns a
277 val_labs_find (const struct val_labs *vls, union value value)
279 assert (vls != NULL);
281 if (vls->width > MAX_SHORT_STRING)
284 if (vls->labels != NULL)
286 struct int_val_lab ivl, *vlp;
289 vlp = hsh_find (vls->labels, &ivl);
291 return atom_to_string (vlp->label);
296 /* A value labels iterator. */
297 struct val_labs_iterator
299 void **labels; /* The labels, in order. */
300 void **lp; /* Current label. */
301 struct val_lab vl; /* Structure presented to caller. */
304 /* Sets up *IP for iterating through the value labels in VLS in
305 no particular order. Returns the first value label or a null
306 pointer if VLS is empty. If the return value is non-null,
307 then val_labs_next() may be used to continue iterating or
308 val_labs_done() to free up the iterator. Otherwise, neither
309 function may be called for *IP. */
311 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
313 struct val_labs_iterator *i;
315 assert (vls != NULL);
318 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
321 i = *ip = xmalloc (sizeof *i);
322 i->labels = hsh_data_copy (vls->labels);
324 return val_labs_next (vls, ip);
327 /* Sets up *IP for iterating through the value labels in VLS in
328 sorted order of values. Returns the first value label or a
329 null pointer if VLS is empty. If the return value is
330 non-null, then val_labs_next() may be used to continue
331 iterating or val_labs_done() to free up the iterator.
332 Otherwise, neither function may be called for *IP. */
334 val_labs_first_sorted (const struct val_labs *vls,
335 struct val_labs_iterator **ip)
337 struct val_labs_iterator *i;
339 assert (vls != NULL);
342 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
345 i = *ip = xmalloc (sizeof *i);
346 i->lp = i->labels = hsh_sort_copy (vls->labels);
347 return val_labs_next (vls, ip);
350 /* Returns the next value label in an iteration begun by
351 val_labs_first() or val_labs_first_sorted(). If the return
352 value is non-null, then val_labs_next() may be used to
353 continue iterating or val_labs_done() to free up the iterator.
354 Otherwise, neither function may be called for *IP. */
356 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
358 struct val_labs_iterator *i;
359 struct int_val_lab *ivl;
361 assert (vls != NULL);
362 assert (vls->width <= MAX_SHORT_STRING);
364 assert (*ip != NULL);
370 i->vl.value = ivl->value;
371 i->vl.label = atom_to_string (ivl->label);
383 /* Discards the state for an incomplete iteration begun by
384 val_labs_first() or val_labs_first_sorted(). */
386 val_labs_done (struct val_labs_iterator **ip)
388 struct val_labs_iterator *i;
391 assert (*ip != NULL);
399 /* Compares two value labels and returns a strcmp()-type result. */
401 compare_int_val_lab (const void *a_, const void *b_, void *vls_)
403 const struct int_val_lab *a = a_;
404 const struct int_val_lab *b = b_;
405 const struct val_labs *vls = vls_;
408 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
410 return memcmp (a->value.s, b->value.s, vls->width);
413 /* Hash a value label. */
415 hash_int_val_lab (const void *vl_, void *vls_)
417 const struct int_val_lab *vl = vl_;
418 const struct val_labs *vls = vls_;
421 return hsh_hash_double (vl->value.f);
423 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
426 /* Free a value label. */
428 free_int_val_lab (void *vl_, void *vls_ UNUSED)
430 struct int_val_lab *vl = vl_;
432 atom_destroy (vl->label);
441 char *string; /* String value. */
442 unsigned ref_count; /* Number of references. */
445 static hsh_compare_func compare_atoms;
446 static hsh_hash_func hash_atom;
447 static hsh_free_func free_atom;
449 /* Hash table of atoms. */
450 static struct hsh_table *atoms;
452 /* Creates and returns an atom for STRING. */
454 atom_create (const char *string)
459 assert (string != NULL);
462 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
464 a.string = (char *) string;
465 app = hsh_probe (atoms, &a);
468 struct atom *ap = *app;
474 struct atom *ap = xmalloc (sizeof *ap);
475 ap->string = xstrdup (string);
484 atom_destroy (struct atom *atom)
488 assert (atom->ref_count > 0);
490 if (atom->ref_count == 0)
491 hsh_force_delete (atoms, atom);
495 /* Returns the string associated with ATOM. */
497 atom_to_string (const struct atom *atom)
499 assert (atom != NULL);
504 /* A hsh_compare_func that compares A and B. */
506 compare_atoms (const void *a_, const void *b_, void *aux UNUSED)
508 const struct atom *a = a_;
509 const struct atom *b = b_;
511 return strcmp (a->string, b->string);
514 /* A hsh_hash_func that hashes ATOM. */
516 hash_atom (const void *atom_, void *aux UNUSED)
518 const struct atom *atom = atom_;
520 return hsh_hash_string (atom->string);
523 /* A hsh_free_func that destroys ATOM. */
525 free_atom (void *atom_, void *aux UNUSED)
527 struct atom *atom = atom_;
534 /* Get a string representing the value.
535 That is, if it has a label, then return that label,
536 otherwise, if the value is alpha, then return the string for it,
537 else format it and return the formatted string
540 value_to_string (const union value *val, const struct variable *var)
544 assert (val != NULL);
545 assert (var != NULL);
547 s = val_labs_find (var->val_labs, *val);
550 static char buf[256];
552 str_copy_buf_trunc (buf, sizeof buf, val->s, var->width);
554 snprintf(buf, 100, "%g", val->f);