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/format.h>
28 #include <data/value.h>
29 #include <data/variable.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/hash.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
36 static hsh_compare_func compare_int_val_lab;
37 static hsh_hash_func hash_int_val_lab;
38 static hsh_free_func free_int_val_lab;
41 static struct atom *atom_create (const char *string);
42 static void atom_destroy (struct atom *);
43 static char *atom_to_string (const struct atom *);
45 /* A set of value labels. */
48 int width; /* 0=numeric, otherwise string width. */
49 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
52 /* Creates and returns a new, empty set of value labels with the
53 given WIDTH. To actually add any value labels, WIDTH must be
54 a numeric or short string width. */
56 val_labs_create (int width)
62 vls = xmalloc (sizeof *vls);
68 /* Creates and returns a new set of value labels identical to
71 val_labs_copy (const struct val_labs *vls)
73 struct val_labs *copy;
74 struct val_labs_iterator *i;
80 copy = val_labs_create (vls->width);
81 for (vl = val_labs_first (vls, &i); vl != NULL;
82 vl = val_labs_next (vls, &i))
83 val_labs_add (copy, vl->value, vl->label);
87 /* Determines whether VLS's width can be changed to NEW_WIDTH.
88 Numeric widths cannot be changed at all.
89 Strings can be widened. They can be shortened only if the
90 characters that will be truncated are spaces. */
92 val_labs_can_set_width (const struct val_labs *vls, int new_width)
94 assert ((vls->width == 0) == (new_width == 0));
97 return new_width == 0;
98 else if (new_width < vls->width)
100 struct val_labs_iterator *i;
103 for (lab = val_labs_first (vls, &i); lab != NULL;
104 lab = val_labs_next (vls, &i))
108 /* We can shorten the value labels only if all the
109 truncated characters are blanks. */
110 for (j = vls->width; j < new_width; j++)
111 if (lab->value.s[j] != ' ')
123 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
124 NEW_WIDTH must be 0, otherwise it must be within the range
125 1...MAX_SHORT_STRING inclusive. */
127 val_labs_set_width (struct val_labs *vls, int new_width)
129 assert (val_labs_can_set_width (vls, new_width));
131 vls->width = new_width;
132 if (new_width > MAX_SHORT_STRING)
133 val_labs_clear (vls);
138 val_labs_destroy (struct val_labs *vls)
142 hsh_destroy (vls->labels);
147 /* Removes all the value labels from VLS. */
149 val_labs_clear (struct val_labs *vls)
151 assert (vls != NULL);
153 hsh_destroy (vls->labels);
157 /* Returns the number of value labels in VLS. */
159 val_labs_count (const struct val_labs *vls)
161 return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
164 /* One value label in internal format. */
167 union value value; /* The value being labeled. */
168 struct atom *label; /* A ref-counted string. */
171 /* Creates and returns an int_val_lab based on VALUE and
173 static struct int_val_lab *
174 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
176 struct int_val_lab *ivl;
178 assert (label != NULL);
179 assert (vls->width <= MAX_SHORT_STRING);
181 ivl = xmalloc (sizeof *ivl);
184 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
185 ivl->label = atom_create (label);
190 /* If VLS does not already contain a value label for VALUE, adds
191 LABEL for it and returns true. Otherwise, returns false.
192 Behavior is undefined if VLS's width is greater than
195 val_labs_add (struct val_labs *vls, union value value, const char *label)
197 struct int_val_lab *ivl;
200 assert (vls != NULL);
201 assert (vls->width <= MAX_SHORT_STRING);
202 assert (label != NULL);
204 if (vls->labels == NULL)
205 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
206 free_int_val_lab, vls);
208 ivl = create_int_val_lab (vls, value, label);
209 vlpp = hsh_probe (vls->labels, ivl);
215 free_int_val_lab (ivl, vls);
219 /* Sets LABEL as the value label for VALUE in VLS. Returns false
220 if there wasn't already a value label for VALUE, or true if
221 there was. Behavior is undefined if VLS's width is greater
222 than MAX_SHORT_STRING. */
224 val_labs_replace (struct val_labs *vls, union value value, const char *label)
226 assert (vls->width <= MAX_SHORT_STRING);
227 if (vls->labels != NULL)
229 struct int_val_lab *new = create_int_val_lab (vls, value, label);
230 struct int_val_lab *old = hsh_replace (vls->labels, new);
232 free_int_val_lab (old, vls);
235 val_labs_add (vls, value, label);
238 /* Removes any value label for VALUE within VLS. Returns true
239 if a value label was removed. Behavior is undefined if VLS's
240 width is greater than MAX_SHORT_STRING. */
242 val_labs_remove (struct val_labs *vls, union value value)
244 assert (vls != NULL);
245 assert (vls->width <= MAX_SHORT_STRING);
247 if (vls->labels != NULL)
249 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
250 int deleted = hsh_delete (vls->labels, ivl);
258 /* Searches VLS for a value label for VALUE. If successful,
259 returns the label; otherwise, returns a null pointer. If
260 VLS's width is greater than MAX_SHORT_STRING, always returns a
263 val_labs_find (const struct val_labs *vls, union value value)
266 && vls->width <= MAX_SHORT_STRING
267 && vls->labels != NULL)
269 struct int_val_lab ivl, *vlp;
272 vlp = hsh_find (vls->labels, &ivl);
274 return atom_to_string (vlp->label);
279 /* A value labels iterator. */
280 struct val_labs_iterator
282 void **labels; /* The labels, in order. */
283 void **lp; /* Current label. */
284 struct val_lab vl; /* Structure presented to caller. */
287 /* Sets up *IP for iterating through the value labels in VLS in
288 no particular order. Returns the first value label or a null
289 pointer if VLS is empty. If the return value is non-null,
290 then val_labs_next() may be used to continue iterating or
291 val_labs_done() to free up the iterator. Otherwise, neither
292 function may be called for *IP. */
294 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
296 struct val_labs_iterator *i;
298 assert (vls != NULL);
301 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
304 i = *ip = xmalloc (sizeof *i);
305 i->labels = hsh_data_copy (vls->labels);
307 return val_labs_next (vls, ip);
310 /* Sets up *IP for iterating through the value labels in VLS in
311 sorted order of values. Returns the first value label or a
312 null pointer if VLS is empty. If the return value is
313 non-null, then val_labs_next() may be used to continue
314 iterating or val_labs_done() to free up the iterator.
315 Otherwise, neither function may be called for *IP. */
317 val_labs_first_sorted (const struct val_labs *vls,
318 struct val_labs_iterator **ip)
320 struct val_labs_iterator *i;
322 assert (vls != NULL);
325 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
328 i = *ip = xmalloc (sizeof *i);
329 i->lp = i->labels = hsh_sort_copy (vls->labels);
330 return val_labs_next (vls, ip);
333 /* Returns the next value label in an iteration begun by
334 val_labs_first() or val_labs_first_sorted(). If the return
335 value is non-null, then val_labs_next() may be used to
336 continue iterating or val_labs_done() to free up the iterator.
337 Otherwise, neither function may be called for *IP. */
339 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
341 struct val_labs_iterator *i;
342 struct int_val_lab *ivl;
344 assert (vls != NULL);
345 assert (vls->width <= MAX_SHORT_STRING);
347 assert (*ip != NULL);
353 i->vl.value = ivl->value;
354 i->vl.label = atom_to_string (ivl->label);
366 /* Discards the state for an incomplete iteration begun by
367 val_labs_first() or val_labs_first_sorted(). */
369 val_labs_done (struct val_labs_iterator **ip)
371 struct val_labs_iterator *i;
374 assert (*ip != NULL);
382 /* Compares two value labels and returns a strcmp()-type result. */
384 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
386 const struct int_val_lab *a = a_;
387 const struct int_val_lab *b = b_;
388 const struct val_labs *vls = vls_;
391 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
393 return memcmp (a->value.s, b->value.s, vls->width);
396 /* Hash a value label. */
398 hash_int_val_lab (const void *vl_, const void *vls_)
400 const struct int_val_lab *vl = vl_;
401 const struct val_labs *vls = vls_;
404 return hsh_hash_double (vl->value.f);
406 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
409 /* Free a value label. */
411 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
413 struct int_val_lab *vl = vl_;
415 atom_destroy (vl->label);
424 char *string; /* String value. */
425 unsigned ref_count; /* Number of references. */
428 static hsh_compare_func compare_atoms;
429 static hsh_hash_func hash_atom;
430 static hsh_free_func free_atom;
432 /* Hash table of atoms. */
433 static struct hsh_table *atoms;
441 /* Creates and returns an atom for STRING. */
443 atom_create (const char *string)
448 assert (string != NULL);
452 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
453 atexit (destroy_atoms);
456 a.string = (char *) string;
457 app = hsh_probe (atoms, &a);
460 struct atom *ap = *app;
466 struct atom *ap = xmalloc (sizeof *ap);
467 ap->string = xstrdup (string);
476 atom_destroy (struct atom *atom)
480 assert (atom->ref_count > 0);
482 if (atom->ref_count == 0)
483 hsh_force_delete (atoms, atom);
487 /* Returns the string associated with ATOM. */
489 atom_to_string (const struct atom *atom)
491 assert (atom != NULL);
496 /* A hsh_compare_func that compares A and B. */
498 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
500 const struct atom *a = a_;
501 const struct atom *b = b_;
503 return strcmp (a->string, b->string);
506 /* A hsh_hash_func that hashes ATOM. */
508 hash_atom (const void *atom_, const void *aux UNUSED)
510 const struct atom *atom = atom_;
512 return hsh_hash_string (atom->string);
515 /* A hsh_free_func that destroys ATOM. */
517 free_atom (void *atom_, const void *aux UNUSED)
519 struct atom *atom = atom_;