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, which must designate a numeric (0) or short
51 string (1...MAX_SHORT_STRING inclusive) 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 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
84 NEW_WIDTH must be 0, otherwise it must be within the range
85 1...MAX_SHORT_STRING inclusive. */
87 val_labs_set_width (struct val_labs *vls, int new_width)
90 assert ((vls->width == 0) == (new_width == 0));
92 vls->width = new_width;
97 val_labs_destroy (struct val_labs *vls)
101 if (vls->labels != NULL)
102 hsh_destroy (vls->labels);
107 /* Removes all the value labels from VLS. */
109 val_labs_clear (struct val_labs *vls)
111 assert (vls != NULL);
113 hsh_destroy (vls->labels);
117 /* Returns the number of value labels in VLS. */
119 val_labs_count (const struct val_labs *vls)
121 assert (vls != NULL);
123 if (vls->labels == NULL)
126 return hsh_count (vls->labels);
129 /* One value label in internal format. */
132 union value value; /* The value being labeled. */
133 struct atom *label; /* A ref-counted string. */
136 /* Creates and returns an int_val_lab based on VALUE and
138 static struct int_val_lab *
139 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
141 struct int_val_lab *ivl;
143 assert (label != NULL);
144 assert (vls->width <= MAX_SHORT_STRING);
146 ivl = xmalloc (sizeof *ivl);
149 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
150 ivl->label = atom_create (label);
155 /* If VLS does not already contain a value label for VALUE, adds
156 LABEL for it and returns nonzero. Otherwise, returns zero.
157 Behavior is undefined if VLS's width is greater than
160 val_labs_add (struct val_labs *vls, union value value, const char *label)
162 struct int_val_lab *ivl;
165 assert (vls != NULL);
166 assert (vls->width <= MAX_SHORT_STRING);
167 assert (label != NULL);
169 if (vls->labels == NULL)
170 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
171 free_int_val_lab, vls);
173 ivl = create_int_val_lab (vls, value, label);
174 vlpp = hsh_probe (vls->labels, ivl);
182 free_int_val_lab (ivl, vls);
187 /* Sets LABEL as the value label for VALUE in VLS. Returns zero
188 if there wasn't already a value label for VALUE, or nonzero if
189 there was. Behavior is undefined if VLS's width is greater
190 than MAX_SHORT_STRING. */
192 val_labs_replace (struct val_labs *vls, union value value, const char *label)
194 struct int_val_lab *ivl;
196 assert (vls != NULL);
197 assert (vls->width <= MAX_SHORT_STRING);
198 assert (label != NULL);
200 if (vls->labels == NULL)
202 val_labs_add (vls, value, label);
206 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
211 free_int_val_lab (ivl, vls);
216 /* Removes any value label for VALUE within VLS. Returns nonzero
217 if a value label was removed. Behavior is undefined if VLS's
218 width is greater than MAX_SHORT_STRING. */
220 val_labs_remove (struct val_labs *vls, union value value)
222 assert (vls != NULL);
223 assert (vls->width <= MAX_SHORT_STRING);
225 if (vls->labels != NULL)
227 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
228 int deleted = hsh_delete (vls->labels, ivl);
236 /* Searches VLS for a value label for VALUE. If successful,
237 returns the label; otherwise, returns a null pointer. If
238 VLS's width is greater than MAX_SHORT_STRING, always returns a
241 val_labs_find (const struct val_labs *vls, union value value)
243 assert (vls != NULL);
245 if (vls->width > MAX_SHORT_STRING)
248 if (vls->labels != NULL)
250 struct int_val_lab ivl, *vlp;
253 vlp = hsh_find (vls->labels, &ivl);
255 return atom_to_string (vlp->label);
260 /* A value labels iterator. */
261 struct val_labs_iterator
263 void **labels; /* The labels, in order. */
264 void **lp; /* Current label. */
265 struct val_lab vl; /* Structure presented to caller. */
268 /* Sets up *IP for iterating through the value labels in VLS in
269 no particular order. Returns the first value label or a null
270 pointer if VLS is empty. If the return value is non-null,
271 then val_labs_next() may be used to continue iterating or
272 val_labs_done() to free up the iterator. Otherwise, neither
273 function may be called for *IP. */
275 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
277 struct val_labs_iterator *i;
279 assert (vls != NULL);
282 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
285 i = *ip = xmalloc (sizeof *i);
286 i->labels = hsh_data_copy (vls->labels);
288 return val_labs_next (vls, ip);
291 /* Sets up *IP for iterating through the value labels in VLS in
292 sorted order of values. Returns the first value label or a
293 null pointer if VLS is empty. If the return value is
294 non-null, then val_labs_next() may be used to continue
295 iterating or val_labs_done() to free up the iterator.
296 Otherwise, neither function may be called for *IP. */
298 val_labs_first_sorted (const struct val_labs *vls,
299 struct val_labs_iterator **ip)
301 struct val_labs_iterator *i;
303 assert (vls != NULL);
306 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
309 i = *ip = xmalloc (sizeof *i);
310 i->lp = i->labels = hsh_sort_copy (vls->labels);
311 return val_labs_next (vls, ip);
314 /* Returns the next value label in an iteration begun by
315 val_labs_first() or val_labs_first_sorted(). If the return
316 value is non-null, then val_labs_next() may be used to
317 continue iterating or val_labs_done() to free up the iterator.
318 Otherwise, neither function may be called for *IP. */
320 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
322 struct val_labs_iterator *i;
323 struct int_val_lab *ivl;
325 assert (vls != NULL);
326 assert (vls->width <= MAX_SHORT_STRING);
328 assert (*ip != NULL);
334 i->vl.value = ivl->value;
335 i->vl.label = atom_to_string (ivl->label);
347 /* Discards the state for an incomplete iteration begun by
348 val_labs_first() or val_labs_first_sorted(). */
350 val_labs_done (struct val_labs_iterator **ip)
352 struct val_labs_iterator *i;
355 assert (*ip != NULL);
363 /* Compares two value labels and returns a strcmp()-type result. */
365 compare_int_val_lab (const void *a_, const void *b_, void *vls_)
367 const struct int_val_lab *a = a_;
368 const struct int_val_lab *b = b_;
369 const struct val_labs *vls = vls_;
372 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
374 return memcmp (a->value.s, b->value.s, vls->width);
377 /* Hash a value label. */
379 hash_int_val_lab (const void *vl_, void *vls_)
381 const struct int_val_lab *vl = vl_;
382 const struct val_labs *vls = vls_;
385 return hsh_hash_double (vl->value.f);
387 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
390 /* Free a value label. */
392 free_int_val_lab (void *vl_, void *vls_ UNUSED)
394 struct int_val_lab *vl = vl_;
396 atom_destroy (vl->label);
405 char *string; /* String value. */
406 unsigned ref_count; /* Number of references. */
409 static hsh_compare_func compare_atoms;
410 static hsh_hash_func hash_atom;
411 static hsh_free_func free_atom;
413 /* Hash table of atoms. */
414 static struct hsh_table *atoms;
416 /* Creates and returns an atom for STRING. */
418 atom_create (const char *string)
423 assert (string != NULL);
426 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
428 a.string = (char *) string;
429 app = hsh_probe (atoms, &a);
432 struct atom *ap = *app;
438 struct atom *ap = xmalloc (sizeof *ap);
439 ap->string = xstrdup (string);
448 atom_destroy (struct atom *atom)
452 assert (atom->ref_count > 0);
454 if (atom->ref_count == 0)
455 hsh_force_delete (atoms, atom);
459 /* Returns the string associated with ATOM. */
461 atom_to_string (const struct atom *atom)
463 assert (atom != NULL);
468 /* A hsh_compare_func that compares A and B. */
470 compare_atoms (const void *a_, const void *b_, void *aux UNUSED)
472 const struct atom *a = a_;
473 const struct atom *b = b_;
475 return strcmp (a->string, b->string);
478 /* A hsh_hash_func that hashes ATOM. */
480 hash_atom (const void *atom_, void *aux UNUSED)
482 const struct atom *atom = atom_;
484 return hsh_hash_string (atom->string);
487 /* A hsh_free_func that destroys ATOM. */
489 free_atom (void *atom_, void *aux UNUSED)
491 struct atom *atom = atom_;
498 /* Get a string representing the value.
499 That is, if it has a label, then return that label,
500 otherwise, if the value is alpha, then return the string for it,
501 else format it and return the formatted string
504 value_to_string (const union value *val, const struct variable *var)
508 assert (val != NULL);
509 assert (var != NULL);
511 s = val_labs_find (var->val_labs, *val);
514 static char buf[256];
516 str_copy_buf_trunc (buf, sizeof buf, val->s, var->width);
518 snprintf(buf, 100, "%g", val->f);