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/value.h>
25 #include <data/variable.h>
26 #include <libpspp/compiler.h>
27 #include <libpspp/hash.h>
28 #include <libpspp/message.h>
29 #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_clone (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 using the rules checked by value_is_resizable. */
87 val_labs_can_set_width (const struct val_labs *vls, int new_width)
89 struct val_labs_iterator *i;
92 for (lab = val_labs_first (vls, &i); lab != NULL;
93 lab = val_labs_next (vls, &i))
94 if (!value_is_resizable (&lab->value, vls->width, new_width))
103 /* Changes the width of VLS to NEW_WIDTH. The original and new
104 width must be both numeric or both string. If the new width
105 is a long string width, then any value labels in VLS are
108 val_labs_set_width (struct val_labs *vls, int new_width)
110 assert (val_labs_can_set_width (vls, new_width));
112 vls->width = new_width;
113 if (new_width > MAX_SHORT_STRING)
114 val_labs_clear (vls);
119 val_labs_destroy (struct val_labs *vls)
123 hsh_destroy (vls->labels);
128 /* Removes all the value labels from VLS. */
130 val_labs_clear (struct val_labs *vls)
132 assert (vls != NULL);
134 hsh_destroy (vls->labels);
138 /* Returns the number of value labels in VLS. */
140 val_labs_count (const struct val_labs *vls)
142 return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
145 /* One value label in internal format. */
148 union value value; /* The value being labeled. */
149 struct atom *label; /* A ref-counted string. */
152 /* Creates and returns an int_val_lab based on VALUE and
154 static struct int_val_lab *
155 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
157 struct int_val_lab *ivl;
159 assert (label != NULL);
160 assert (vls->width <= MAX_SHORT_STRING);
162 ivl = xmalloc (sizeof *ivl);
165 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
166 ivl->label = atom_create (label);
171 /* If VLS does not already contain a value label for VALUE (and
172 VLS represents a numeric or short string set of value labels),
173 adds LABEL for it and returns true. Otherwise, returns
176 val_labs_add (struct val_labs *vls, union value value, const char *label)
178 assert (label != NULL);
179 if (vls->width < MIN_LONG_STRING)
181 struct int_val_lab *ivl;
184 if (vls->labels == NULL)
185 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
186 free_int_val_lab, vls);
188 ivl = create_int_val_lab (vls, value, label);
189 vlpp = hsh_probe (vls->labels, ivl);
195 free_int_val_lab (ivl, vls);
200 /* Sets LABEL as the value label for VALUE in VLS, replacing any
201 existing label for VALUE. Has no effect if VLS has a long
204 val_labs_replace (struct val_labs *vls, union value value, const char *label)
206 if (vls->width < MIN_LONG_STRING)
208 if (vls->labels != NULL)
210 struct int_val_lab *new = create_int_val_lab (vls, value, label);
211 struct int_val_lab *old = hsh_replace (vls->labels, new);
213 free_int_val_lab (old, vls);
216 val_labs_add (vls, value, label);
220 /* Removes any value label for VALUE within VLS. Returns true
221 if a value label was removed. */
223 val_labs_remove (struct val_labs *vls, union value value)
225 if (vls->width < MIN_LONG_STRING && 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)
244 && vls->width <= MAX_SHORT_STRING
245 && vls->labels != NULL)
247 struct int_val_lab ivl, *vlp;
250 vlp = hsh_find (vls->labels, &ivl);
252 return atom_to_string (vlp->label);
257 /* A value labels iterator. */
258 struct val_labs_iterator
260 void **labels; /* The labels, in order. */
261 void **lp; /* Current label. */
262 struct val_lab vl; /* Structure presented to caller. */
265 /* Sets up *IP for iterating through the value labels in VLS in
266 no particular order. Returns the first value label or a null
267 pointer if VLS is empty. If the return value is non-null,
268 then val_labs_next() may be used to continue iterating or
269 val_labs_done() to free up the iterator. Otherwise, neither
270 function may be called for *IP. */
272 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
274 struct val_labs_iterator *i;
276 assert (vls != NULL);
279 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)
312 i = *ip = xmalloc (sizeof *i);
313 i->lp = i->labels = hsh_sort_copy (vls->labels);
314 return val_labs_next (vls, ip);
317 /* Returns the next value label in an iteration begun by
318 val_labs_first() or val_labs_first_sorted(). If the return
319 value is non-null, then val_labs_next() may be used to
320 continue iterating or val_labs_done() to free up the iterator.
321 Otherwise, neither function may be called for *IP. */
323 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
325 struct val_labs_iterator *i;
326 struct int_val_lab *ivl;
328 assert (vls != NULL);
329 assert (vls->width <= MAX_SHORT_STRING);
331 assert (*ip != NULL);
337 i->vl.value = ivl->value;
338 i->vl.label = atom_to_string (ivl->label);
350 /* Discards the state for an incomplete iteration begun by
351 val_labs_first() or val_labs_first_sorted(). */
353 val_labs_done (struct val_labs_iterator **ip)
357 struct val_labs_iterator *i = *ip;
364 /* Compares two value labels and returns a strcmp()-type result. */
366 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
368 const struct int_val_lab *a = a_;
369 const struct int_val_lab *b = b_;
370 const struct val_labs *vls = vls_;
373 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
375 return memcmp (a->value.s, b->value.s, vls->width);
378 /* Hash a value label. */
380 hash_int_val_lab (const void *vl_, const void *vls_)
382 const struct int_val_lab *vl = vl_;
383 const struct val_labs *vls = vls_;
386 return hsh_hash_double (vl->value.f);
388 return hsh_hash_bytes (vl->value.s, vls->width);
391 /* Free a value label. */
393 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
395 struct int_val_lab *vl = vl_;
397 atom_destroy (vl->label);
406 char *string; /* String value. */
407 unsigned ref_count; /* Number of references. */
410 static hsh_compare_func compare_atoms;
411 static hsh_hash_func hash_atom;
412 static hsh_free_func free_atom;
414 /* Hash table of atoms. */
415 static struct hsh_table *atoms;
423 /* Creates and returns an atom for STRING. */
425 atom_create (const char *string)
430 assert (string != NULL);
434 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
435 atexit (destroy_atoms);
438 a.string = (char *) string;
439 app = hsh_probe (atoms, &a);
442 struct atom *ap = *app;
448 struct atom *ap = xmalloc (sizeof *ap);
449 ap->string = xstrdup (string);
458 atom_destroy (struct atom *atom)
462 assert (atom->ref_count > 0);
464 if (atom->ref_count == 0)
465 hsh_force_delete (atoms, atom);
469 /* Returns the string associated with ATOM. */
471 atom_to_string (const struct atom *atom)
473 assert (atom != NULL);
478 /* A hsh_compare_func that compares A and B. */
480 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
482 const struct atom *a = a_;
483 const struct atom *b = b_;
485 return strcmp (a->string, b->string);
488 /* A hsh_hash_func that hashes ATOM. */
490 hash_atom (const void *atom_, const void *aux UNUSED)
492 const struct atom *atom = atom_;
494 return hsh_hash_string (atom->string);
497 /* A hsh_free_func that destroys ATOM. */
499 free_atom (void *atom_, const void *aux UNUSED)
501 struct atom *atom = atom_;