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/compiler.h>
28 #include <libpspp/hash.h>
29 #include <libpspp/message.h>
30 #include <libpspp/str.h>
34 static hsh_compare_func compare_int_val_lab;
35 static hsh_hash_func hash_int_val_lab;
36 static hsh_free_func free_int_val_lab;
39 static struct atom *atom_create (const char *string);
40 static void atom_destroy (struct atom *);
41 static char *atom_to_string (const struct atom *);
43 /* A set of value labels. */
46 int width; /* 0=numeric, otherwise string width. */
47 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
50 /* Creates and returns a new, empty set of value labels with the
51 given WIDTH. To actually add any value labels, WIDTH must be
52 a numeric or short string width. */
54 val_labs_create (int width)
60 vls = xmalloc (sizeof *vls);
66 /* Creates and returns a new set of value labels identical to
69 val_labs_clone (const struct val_labs *vls)
71 struct val_labs *copy;
72 struct val_labs_iterator *i;
78 copy = val_labs_create (vls->width);
79 for (vl = val_labs_first (vls, &i); vl != NULL;
80 vl = val_labs_next (vls, &i))
81 val_labs_add (copy, vl->value, vl->label);
85 /* Determines whether VLS's width can be changed to NEW_WIDTH,
86 using the rules checked by value_is_resizable. */
88 val_labs_can_set_width (const struct val_labs *vls, int new_width)
90 struct val_labs_iterator *i;
93 for (lab = val_labs_first (vls, &i); lab != NULL;
94 lab = val_labs_next (vls, &i))
95 if (!value_is_resizable (&lab->value, vls->width, new_width))
104 /* Changes the width of VLS to NEW_WIDTH. The original and new
105 width must be both numeric or both string. If the new width
106 is a long string width, then any value labels in VLS are
109 val_labs_set_width (struct val_labs *vls, int new_width)
111 assert (val_labs_can_set_width (vls, new_width));
113 vls->width = new_width;
114 if (new_width > MAX_SHORT_STRING)
115 val_labs_clear (vls);
120 val_labs_destroy (struct val_labs *vls)
124 hsh_destroy (vls->labels);
129 /* Removes all the value labels from VLS. */
131 val_labs_clear (struct val_labs *vls)
133 assert (vls != NULL);
135 hsh_destroy (vls->labels);
139 /* Returns the number of value labels in VLS. */
141 val_labs_count (const struct val_labs *vls)
143 return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
146 /* One value label in internal format. */
149 union value value; /* The value being labeled. */
150 struct atom *label; /* A ref-counted string. */
153 /* Creates and returns an int_val_lab based on VALUE and
155 static struct int_val_lab *
156 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
158 struct int_val_lab *ivl;
160 assert (label != NULL);
161 assert (vls->width <= MAX_SHORT_STRING);
163 ivl = xmalloc (sizeof *ivl);
166 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
167 ivl->label = atom_create (label);
172 /* If VLS does not already contain a value label for VALUE (and
173 VLS represents a numeric or short string set of value labels),
174 adds LABEL for it and returns true. Otherwise, returns
177 val_labs_add (struct val_labs *vls, union value value, const char *label)
179 assert (label != NULL);
180 if (vls->width < MIN_LONG_STRING)
182 struct int_val_lab *ivl;
185 if (vls->labels == NULL)
186 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
187 free_int_val_lab, vls);
189 ivl = create_int_val_lab (vls, value, label);
190 vlpp = hsh_probe (vls->labels, ivl);
196 free_int_val_lab (ivl, vls);
201 /* Sets LABEL as the value label for VALUE in VLS, replacing any
202 existing label for VALUE. Has no effect if VLS has a long
205 val_labs_replace (struct val_labs *vls, union value value, const char *label)
207 if (vls->width < MIN_LONG_STRING)
209 if (vls->labels != NULL)
211 struct int_val_lab *new = create_int_val_lab (vls, value, label);
212 struct int_val_lab *old = hsh_replace (vls->labels, new);
214 free_int_val_lab (old, vls);
217 val_labs_add (vls, value, label);
221 /* Removes any value label for VALUE within VLS. Returns true
222 if a value label was removed. */
224 val_labs_remove (struct val_labs *vls, union value value)
226 if (vls->width < MIN_LONG_STRING && vls->labels != NULL)
228 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
229 int deleted = hsh_delete (vls->labels, ivl);
237 /* Searches VLS for a value label for VALUE. If successful,
238 returns the label; otherwise, returns a null pointer. If
239 VLS's width is greater than MAX_SHORT_STRING, always returns a
242 val_labs_find (const struct val_labs *vls, union value value)
245 && vls->width <= MAX_SHORT_STRING
246 && vls->labels != NULL)
248 struct int_val_lab ivl, *vlp;
251 vlp = hsh_find (vls->labels, &ivl);
253 return atom_to_string (vlp->label);
258 /* A value labels iterator. */
259 struct val_labs_iterator
261 void **labels; /* The labels, in order. */
262 void **lp; /* Current label. */
263 struct val_lab vl; /* Structure presented to caller. */
266 /* Sets up *IP for iterating through the value labels in VLS in
267 no particular order. Returns the first value label or a null
268 pointer if VLS is empty. If the return value is non-null,
269 then val_labs_next() may be used to continue iterating or
270 val_labs_done() to free up the iterator. Otherwise, neither
271 function may be called for *IP. */
273 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
275 struct val_labs_iterator *i;
277 assert (vls != NULL);
280 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
286 i = *ip = xmalloc (sizeof *i);
287 i->labels = hsh_data_copy (vls->labels);
289 return val_labs_next (vls, ip);
292 /* Sets up *IP for iterating through the value labels in VLS in
293 sorted order of values. Returns the first value label or a
294 null pointer if VLS is empty. If the return value is
295 non-null, then val_labs_next() may be used to continue
296 iterating or val_labs_done() to free up the iterator.
297 Otherwise, neither function may be called for *IP. */
299 val_labs_first_sorted (const struct val_labs *vls,
300 struct val_labs_iterator **ip)
302 struct val_labs_iterator *i;
304 assert (vls != NULL);
307 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
313 i = *ip = xmalloc (sizeof *i);
314 i->lp = i->labels = hsh_sort_copy (vls->labels);
315 return val_labs_next (vls, ip);
318 /* Returns the next value label in an iteration begun by
319 val_labs_first() or val_labs_first_sorted(). If the return
320 value is non-null, then val_labs_next() may be used to
321 continue iterating or val_labs_done() to free up the iterator.
322 Otherwise, neither function may be called for *IP. */
324 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
326 struct val_labs_iterator *i;
327 struct int_val_lab *ivl;
329 assert (vls != NULL);
330 assert (vls->width <= MAX_SHORT_STRING);
332 assert (*ip != NULL);
338 i->vl.value = ivl->value;
339 i->vl.label = atom_to_string (ivl->label);
351 /* Discards the state for an incomplete iteration begun by
352 val_labs_first() or val_labs_first_sorted(). */
354 val_labs_done (struct val_labs_iterator **ip)
358 struct val_labs_iterator *i = *ip;
365 /* Compares two value labels and returns a strcmp()-type result. */
367 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
369 const struct int_val_lab *a = a_;
370 const struct int_val_lab *b = b_;
371 const struct val_labs *vls = vls_;
374 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
376 return memcmp (a->value.s, b->value.s, vls->width);
379 /* Hash a value label. */
381 hash_int_val_lab (const void *vl_, const void *vls_)
383 const struct int_val_lab *vl = vl_;
384 const struct val_labs *vls = vls_;
387 return hsh_hash_double (vl->value.f);
389 return hsh_hash_bytes (vl->value.s, vls->width);
392 /* Free a value label. */
394 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
396 struct int_val_lab *vl = vl_;
398 atom_destroy (vl->label);
407 char *string; /* String value. */
408 unsigned ref_count; /* Number of references. */
411 static hsh_compare_func compare_atoms;
412 static hsh_hash_func hash_atom;
413 static hsh_free_func free_atom;
415 /* Hash table of atoms. */
416 static struct hsh_table *atoms;
424 /* Creates and returns an atom for STRING. */
426 atom_create (const char *string)
431 assert (string != NULL);
435 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
436 atexit (destroy_atoms);
439 a.string = (char *) string;
440 app = hsh_probe (atoms, &a);
443 struct atom *ap = *app;
449 struct atom *ap = xmalloc (sizeof *ap);
450 ap->string = xstrdup (string);
459 atom_destroy (struct atom *atom)
463 assert (atom->ref_count > 0);
465 if (atom->ref_count == 0)
466 hsh_force_delete (atoms, atom);
470 /* Returns the string associated with ATOM. */
472 atom_to_string (const struct atom *atom)
474 assert (atom != NULL);
479 /* A hsh_compare_func that compares A and B. */
481 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
483 const struct atom *a = a_;
484 const struct atom *b = b_;
486 return strcmp (a->string, b->string);
489 /* A hsh_hash_func that hashes ATOM. */
491 hash_atom (const void *atom_, const void *aux UNUSED)
493 const struct atom *atom = atom_;
495 return hsh_hash_string (atom->string);
498 /* A hsh_free_func that destroys ATOM. */
500 free_atom (void *atom_, const void *aux UNUSED)
502 struct atom *atom = atom_;