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
21 #include "value-labels.h"
22 #include <libpspp/message.h>
24 #include <libpspp/alloc.h>
25 #include <libpspp/compiler.h>
26 #include <libpspp/hash.h>
27 #include <libpspp/str.h>
29 static hsh_compare_func compare_int_val_lab;
30 static hsh_hash_func hash_int_val_lab;
31 static hsh_free_func free_int_val_lab;
34 static struct atom *atom_create (const char *string);
35 static void atom_destroy (struct atom *);
36 static char *atom_to_string (const struct atom *);
38 /* A set of value labels. */
41 int width; /* 0=numeric, otherwise string width. */
42 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
45 /* Creates and returns a new, empty set of value labels with the
46 given WIDTH, which must designate a numeric (0) or short
47 string (1...MAX_SHORT_STRING inclusive) width. */
49 val_labs_create (int width)
55 vls = xmalloc (sizeof *vls);
61 /* Creates and returns a new set of value labels identical to
64 val_labs_copy (const struct val_labs *vls)
66 struct val_labs *copy;
67 struct val_labs_iterator *i;
72 copy = val_labs_create (vls->width);
73 for (vl = val_labs_first (vls, &i); vl != NULL;
74 vl = val_labs_next (vls, &i))
75 val_labs_add (copy, vl->value, vl->label);
79 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
80 NEW_WIDTH must be 0, otherwise it must be within the range
81 1...MAX_SHORT_STRING inclusive. */
83 val_labs_set_width (struct val_labs *vls, int new_width)
86 assert ((vls->width == 0) == (new_width == 0));
88 vls->width = new_width;
93 val_labs_destroy (struct val_labs *vls)
97 if (vls->labels != NULL)
98 hsh_destroy (vls->labels);
103 /* Removes all the value labels from VLS. */
105 val_labs_clear (struct val_labs *vls)
107 assert (vls != NULL);
109 hsh_destroy (vls->labels);
113 /* Returns the number of value labels in VLS. */
115 val_labs_count (const struct val_labs *vls)
117 assert (vls != NULL);
119 if (vls->labels == NULL)
122 return hsh_count (vls->labels);
125 /* One value label in internal format. */
128 union value value; /* The value being labeled. */
129 struct atom *label; /* A ref-counted string. */
132 /* Creates and returns an int_val_lab based on VALUE and
134 static struct int_val_lab *
135 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
137 struct int_val_lab *ivl;
139 assert (label != NULL);
140 assert (vls->width <= MAX_SHORT_STRING);
142 ivl = xmalloc (sizeof *ivl);
145 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
146 ivl->label = atom_create (label);
151 /* If VLS does not already contain a value label for VALUE, adds
152 LABEL for it and returns nonzero. Otherwise, returns zero.
153 Behavior is undefined if VLS's width is greater than
156 val_labs_add (struct val_labs *vls, union value value, const char *label)
158 struct int_val_lab *ivl;
161 assert (vls != NULL);
162 assert (vls->width <= MAX_SHORT_STRING);
163 assert (label != NULL);
165 if (vls->labels == NULL)
166 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
167 free_int_val_lab, vls);
169 ivl = create_int_val_lab (vls, value, label);
170 vlpp = hsh_probe (vls->labels, ivl);
178 free_int_val_lab (ivl, vls);
183 /* Sets LABEL as the value label for VALUE in VLS. Returns zero
184 if there wasn't already a value label for VALUE, or nonzero if
185 there was. Behavior is undefined if VLS's width is greater
186 than MAX_SHORT_STRING. */
188 val_labs_replace (struct val_labs *vls, union value value, const char *label)
190 struct int_val_lab *ivl;
192 assert (vls != NULL);
193 assert (vls->width <= MAX_SHORT_STRING);
194 assert (label != NULL);
196 if (vls->labels == NULL)
198 val_labs_add (vls, value, label);
202 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
207 free_int_val_lab (ivl, vls);
212 /* Removes any value label for VALUE within VLS. Returns nonzero
213 if a value label was removed. Behavior is undefined if VLS's
214 width is greater than MAX_SHORT_STRING. */
216 val_labs_remove (struct val_labs *vls, union value value)
218 assert (vls != NULL);
219 assert (vls->width <= MAX_SHORT_STRING);
221 if (vls->labels != NULL)
223 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
224 int deleted = hsh_delete (vls->labels, ivl);
232 /* Searches VLS for a value label for VALUE. If successful,
233 returns the label; otherwise, returns a null pointer. If
234 VLS's width is greater than MAX_SHORT_STRING, always returns a
237 val_labs_find (const struct val_labs *vls, union value value)
239 assert (vls != NULL);
241 if (vls->width > MAX_SHORT_STRING)
244 if (vls->labels != NULL)
246 struct int_val_lab ivl, *vlp;
249 vlp = hsh_find (vls->labels, &ivl);
251 return atom_to_string (vlp->label);
256 /* A value labels iterator. */
257 struct val_labs_iterator
259 void **labels; /* The labels, in order. */
260 void **lp; /* Current label. */
261 struct val_lab vl; /* Structure presented to caller. */
264 /* Sets up *IP for iterating through the value labels in VLS in
265 no particular order. Returns the first value label or a null
266 pointer if VLS is empty. If the return value is non-null,
267 then val_labs_next() may be used to continue iterating or
268 val_labs_done() to free up the iterator. Otherwise, neither
269 function may be called for *IP. */
271 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
273 struct val_labs_iterator *i;
275 assert (vls != NULL);
278 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
281 i = *ip = xmalloc (sizeof *i);
282 i->labels = hsh_data_copy (vls->labels);
284 return val_labs_next (vls, ip);
287 /* Sets up *IP for iterating through the value labels in VLS in
288 sorted order of values. Returns the first value label or a
289 null pointer if VLS is empty. If the return value is
290 non-null, then val_labs_next() may be used to continue
291 iterating or val_labs_done() to free up the iterator.
292 Otherwise, neither function may be called for *IP. */
294 val_labs_first_sorted (const struct val_labs *vls,
295 struct val_labs_iterator **ip)
297 struct val_labs_iterator *i;
299 assert (vls != NULL);
302 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
305 i = *ip = xmalloc (sizeof *i);
306 i->lp = i->labels = hsh_sort_copy (vls->labels);
307 return val_labs_next (vls, ip);
310 /* Returns the next value label in an iteration begun by
311 val_labs_first() or val_labs_first_sorted(). If the return
312 value is non-null, then val_labs_next() may be used to
313 continue iterating or val_labs_done() to free up the iterator.
314 Otherwise, neither function may be called for *IP. */
316 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
318 struct val_labs_iterator *i;
319 struct int_val_lab *ivl;
321 assert (vls != NULL);
322 assert (vls->width <= MAX_SHORT_STRING);
324 assert (*ip != NULL);
330 i->vl.value = ivl->value;
331 i->vl.label = atom_to_string (ivl->label);
343 /* Discards the state for an incomplete iteration begun by
344 val_labs_first() or val_labs_first_sorted(). */
346 val_labs_done (struct val_labs_iterator **ip)
348 struct val_labs_iterator *i;
351 assert (*ip != NULL);
359 /* Compares two value labels and returns a strcmp()-type result. */
361 compare_int_val_lab (const void *a_, const void *b_, void *vls_)
363 const struct int_val_lab *a = a_;
364 const struct int_val_lab *b = b_;
365 const struct val_labs *vls = vls_;
368 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
370 return memcmp (a->value.s, b->value.s, vls->width);
373 /* Hash a value label. */
375 hash_int_val_lab (const void *vl_, void *vls_)
377 const struct int_val_lab *vl = vl_;
378 const struct val_labs *vls = vls_;
381 return hsh_hash_double (vl->value.f);
383 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
386 /* Free a value label. */
388 free_int_val_lab (void *vl_, void *vls_ UNUSED)
390 struct int_val_lab *vl = vl_;
392 atom_destroy (vl->label);
401 char *string; /* String value. */
402 unsigned ref_count; /* Number of references. */
405 static hsh_compare_func compare_atoms;
406 static hsh_hash_func hash_atom;
407 static hsh_free_func free_atom;
409 /* Hash table of atoms. */
410 static struct hsh_table *atoms;
412 /* Creates and returns an atom for STRING. */
414 atom_create (const char *string)
419 assert (string != NULL);
422 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
424 a.string = (char *) string;
425 app = hsh_probe (atoms, &a);
428 struct atom *ap = *app;
434 struct atom *ap = xmalloc (sizeof *ap);
435 ap->string = xstrdup (string);
444 atom_destroy (struct atom *atom)
448 assert (atom->ref_count > 0);
450 if (atom->ref_count == 0)
451 hsh_force_delete (atoms, atom);
455 /* Returns the string associated with ATOM. */
457 atom_to_string (const struct atom *atom)
459 assert (atom != NULL);
464 /* A hsh_compare_func that compares A and B. */
466 compare_atoms (const void *a_, const void *b_, void *aux UNUSED)
468 const struct atom *a = a_;
469 const struct atom *b = b_;
471 return strcmp (a->string, b->string);
474 /* A hsh_hash_func that hashes ATOM. */
476 hash_atom (const void *atom_, void *aux UNUSED)
478 const struct atom *atom = atom_;
480 return hsh_hash_string (atom->string);
483 /* A hsh_free_func that destroys ATOM. */
485 free_atom (void *atom_, void *aux UNUSED)
487 struct atom *atom = atom_;
494 /* Get a string representing the value.
495 That is, if it has a label, then return that label,
496 otherwise, if the value is alpha, then return the string for it,
497 else format it and return the formatted string
500 value_to_string (const union value *val, const struct variable *var)
504 assert (val != NULL);
505 assert (var != NULL);
507 s = val_labs_find (var->val_labs, *val);
510 static char buf[256];
512 str_copy_buf_trunc (buf, sizeof buf, val->s, var->width);
514 snprintf(buf, 100, "%g", val->f);