1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "value-labels.h"
23 #include <data/data-out.h>
24 #include <data/format.h>
25 #include <data/value.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;
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 if ( var_type_from_width (new_width) != var_type_from_width (vls->width ))
95 return new_width == 0;
96 else if (new_width < vls->width)
98 struct val_labs_iterator *i;
101 for (lab = val_labs_first (vls, &i); lab != NULL;
102 lab = val_labs_next (vls, &i))
106 /* We can shorten the value labels only if all the
107 truncated characters are blanks. */
108 for (j = vls->width; j < new_width; j++)
109 if (lab->value.s[j] != ' ')
121 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
122 NEW_WIDTH must be 0, otherwise it must be within the range
123 1...MAX_SHORT_STRING inclusive. */
125 val_labs_set_width (struct val_labs *vls, int new_width)
127 assert (val_labs_can_set_width (vls, new_width));
129 vls->width = new_width;
130 if (new_width > MAX_SHORT_STRING)
131 val_labs_clear (vls);
136 val_labs_destroy (struct val_labs *vls)
140 hsh_destroy (vls->labels);
145 /* Removes all the value labels from VLS. */
147 val_labs_clear (struct val_labs *vls)
149 assert (vls != NULL);
151 hsh_destroy (vls->labels);
155 /* Returns the number of value labels in VLS. */
157 val_labs_count (const struct val_labs *vls)
159 return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
162 /* One value label in internal format. */
165 union value value; /* The value being labeled. */
166 struct atom *label; /* A ref-counted string. */
169 /* Creates and returns an int_val_lab based on VALUE and
171 static struct int_val_lab *
172 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
174 struct int_val_lab *ivl;
176 assert (label != NULL);
177 assert (vls->width <= MAX_SHORT_STRING);
179 ivl = xmalloc (sizeof *ivl);
182 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
183 ivl->label = atom_create (label);
188 /* If VLS does not already contain a value label for VALUE, adds
189 LABEL for it and returns true. Otherwise, returns false.
190 Behavior is undefined if VLS's width is greater than
193 val_labs_add (struct val_labs *vls, union value value, const char *label)
195 struct int_val_lab *ivl;
198 assert (vls != NULL);
199 assert (vls->width <= MAX_SHORT_STRING);
200 assert (label != NULL);
202 if (vls->labels == NULL)
203 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
204 free_int_val_lab, vls);
206 ivl = create_int_val_lab (vls, value, label);
207 vlpp = hsh_probe (vls->labels, ivl);
213 free_int_val_lab (ivl, vls);
217 /* Sets LABEL as the value label for VALUE in VLS. Returns false
218 if there wasn't already a value label for VALUE, or true if
219 there was. Behavior is undefined if VLS's width is greater
220 than MAX_SHORT_STRING. */
222 val_labs_replace (struct val_labs *vls, union value value, const char *label)
224 assert (vls->width <= MAX_SHORT_STRING);
225 if (vls->labels != NULL)
227 struct int_val_lab *new = create_int_val_lab (vls, value, label);
228 struct int_val_lab *old = hsh_replace (vls->labels, new);
230 free_int_val_lab (old, vls);
233 val_labs_add (vls, value, label);
236 /* Removes any value label for VALUE within VLS. Returns true
237 if a value label was removed. Behavior is undefined if VLS's
238 width is greater than MAX_SHORT_STRING. */
240 val_labs_remove (struct val_labs *vls, union value value)
242 assert (vls != NULL);
243 assert (vls->width <= MAX_SHORT_STRING);
245 if (vls->labels != NULL)
247 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
248 int deleted = hsh_delete (vls->labels, ivl);
256 /* Searches VLS for a value label for VALUE. If successful,
257 returns the label; otherwise, returns a null pointer. If
258 VLS's width is greater than MAX_SHORT_STRING, always returns a
261 val_labs_find (const struct val_labs *vls, union value value)
264 && vls->width <= MAX_SHORT_STRING
265 && vls->labels != NULL)
267 struct int_val_lab ivl, *vlp;
270 vlp = hsh_find (vls->labels, &ivl);
272 return atom_to_string (vlp->label);
277 /* A value labels iterator. */
278 struct val_labs_iterator
280 void **labels; /* The labels, in order. */
281 void **lp; /* Current label. */
282 struct val_lab vl; /* Structure presented to caller. */
285 /* Sets up *IP for iterating through the value labels in VLS in
286 no particular order. Returns the first value label or a null
287 pointer if VLS is empty. If the return value is non-null,
288 then val_labs_next() may be used to continue iterating or
289 val_labs_done() to free up the iterator. Otherwise, neither
290 function may be called for *IP. */
292 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
294 struct val_labs_iterator *i;
296 assert (vls != NULL);
299 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
302 i = *ip = xmalloc (sizeof *i);
303 i->labels = hsh_data_copy (vls->labels);
305 return val_labs_next (vls, ip);
308 /* Sets up *IP for iterating through the value labels in VLS in
309 sorted order of values. Returns the first value label or a
310 null pointer if VLS is empty. If the return value is
311 non-null, then val_labs_next() may be used to continue
312 iterating or val_labs_done() to free up the iterator.
313 Otherwise, neither function may be called for *IP. */
315 val_labs_first_sorted (const struct val_labs *vls,
316 struct val_labs_iterator **ip)
318 struct val_labs_iterator *i;
320 assert (vls != NULL);
323 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
326 i = *ip = xmalloc (sizeof *i);
327 i->lp = i->labels = hsh_sort_copy (vls->labels);
328 return val_labs_next (vls, ip);
331 /* Returns the next value label in an iteration begun by
332 val_labs_first() or val_labs_first_sorted(). If the return
333 value is non-null, then val_labs_next() may be used to
334 continue iterating or val_labs_done() to free up the iterator.
335 Otherwise, neither function may be called for *IP. */
337 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
339 struct val_labs_iterator *i;
340 struct int_val_lab *ivl;
342 assert (vls != NULL);
343 assert (vls->width <= MAX_SHORT_STRING);
345 assert (*ip != NULL);
351 i->vl.value = ivl->value;
352 i->vl.label = atom_to_string (ivl->label);
364 /* Discards the state for an incomplete iteration begun by
365 val_labs_first() or val_labs_first_sorted(). */
367 val_labs_done (struct val_labs_iterator **ip)
369 struct val_labs_iterator *i;
372 assert (*ip != NULL);
380 /* Compares two value labels and returns a strcmp()-type result. */
382 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
384 const struct int_val_lab *a = a_;
385 const struct int_val_lab *b = b_;
386 const struct val_labs *vls = vls_;
389 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
391 return memcmp (a->value.s, b->value.s, vls->width);
394 /* Hash a value label. */
396 hash_int_val_lab (const void *vl_, const void *vls_)
398 const struct int_val_lab *vl = vl_;
399 const struct val_labs *vls = vls_;
402 return hsh_hash_double (vl->value.f);
404 return hsh_hash_bytes (vl->value.s, vls->width);
407 /* Free a value label. */
409 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
411 struct int_val_lab *vl = vl_;
413 atom_destroy (vl->label);
422 char *string; /* String value. */
423 unsigned ref_count; /* Number of references. */
426 static hsh_compare_func compare_atoms;
427 static hsh_hash_func hash_atom;
428 static hsh_free_func free_atom;
430 /* Hash table of atoms. */
431 static struct hsh_table *atoms;
439 /* Creates and returns an atom for STRING. */
441 atom_create (const char *string)
446 assert (string != NULL);
450 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
451 atexit (destroy_atoms);
454 a.string = (char *) string;
455 app = hsh_probe (atoms, &a);
458 struct atom *ap = *app;
464 struct atom *ap = xmalloc (sizeof *ap);
465 ap->string = xstrdup (string);
474 atom_destroy (struct atom *atom)
478 assert (atom->ref_count > 0);
480 if (atom->ref_count == 0)
481 hsh_force_delete (atoms, atom);
485 /* Returns the string associated with ATOM. */
487 atom_to_string (const struct atom *atom)
489 assert (atom != NULL);
494 /* A hsh_compare_func that compares A and B. */
496 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
498 const struct atom *a = a_;
499 const struct atom *b = b_;
501 return strcmp (a->string, b->string);
504 /* A hsh_hash_func that hashes ATOM. */
506 hash_atom (const void *atom_, const void *aux UNUSED)
508 const struct atom *atom = atom_;
510 return hsh_hash_string (atom->string);
513 /* A hsh_free_func that destroys ATOM. */
515 free_atom (void *atom_, const void *aux UNUSED)
517 struct atom *atom = atom_;