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_copy (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 Numeric widths cannot be changed at all.
87 Strings can be widened. They can be shortened only if the
88 characters that will be truncated are spaces. */
90 val_labs_can_set_width (const struct val_labs *vls, int new_width)
92 if ( var_type_from_width (new_width) != var_type_from_width (vls->width ))
96 return new_width == 0;
97 else if (new_width < vls->width)
99 struct val_labs_iterator *i;
102 for (lab = val_labs_first (vls, &i); lab != NULL;
103 lab = val_labs_next (vls, &i))
107 /* We can shorten the value labels only if all the
108 truncated characters are blanks. */
109 for (j = vls->width; j < new_width; j++)
110 if (lab->value.s[j] != ' ')
122 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
123 NEW_WIDTH must be 0, otherwise it must be within the range
124 1...MAX_SHORT_STRING inclusive. */
126 val_labs_set_width (struct val_labs *vls, int new_width)
128 assert (val_labs_can_set_width (vls, new_width));
130 vls->width = new_width;
131 if (new_width > MAX_SHORT_STRING)
132 val_labs_clear (vls);
137 val_labs_destroy (struct val_labs *vls)
141 hsh_destroy (vls->labels);
146 /* Removes all the value labels from VLS. */
148 val_labs_clear (struct val_labs *vls)
150 assert (vls != NULL);
152 hsh_destroy (vls->labels);
156 /* Returns the number of value labels in VLS. */
158 val_labs_count (const struct val_labs *vls)
160 return vls == NULL || vls->labels == NULL ? 0 : hsh_count (vls->labels);
163 /* One value label in internal format. */
166 union value value; /* The value being labeled. */
167 struct atom *label; /* A ref-counted string. */
170 /* Creates and returns an int_val_lab based on VALUE and
172 static struct int_val_lab *
173 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
175 struct int_val_lab *ivl;
177 assert (label != NULL);
178 assert (vls->width <= MAX_SHORT_STRING);
180 ivl = xmalloc (sizeof *ivl);
183 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
184 ivl->label = atom_create (label);
189 /* If VLS does not already contain a value label for VALUE, adds
190 LABEL for it and returns true. Otherwise, returns false.
191 Behavior is undefined if VLS's width is greater than
194 val_labs_add (struct val_labs *vls, union value value, const char *label)
196 struct int_val_lab *ivl;
199 assert (vls != NULL);
200 assert (vls->width <= MAX_SHORT_STRING);
201 assert (label != NULL);
203 if (vls->labels == NULL)
204 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
205 free_int_val_lab, vls);
207 ivl = create_int_val_lab (vls, value, label);
208 vlpp = hsh_probe (vls->labels, ivl);
214 free_int_val_lab (ivl, vls);
218 /* Sets LABEL as the value label for VALUE in VLS. Returns false
219 if there wasn't already a value label for VALUE, or true if
220 there was. Behavior is undefined if VLS's width is greater
221 than MAX_SHORT_STRING. */
223 val_labs_replace (struct val_labs *vls, union value value, const char *label)
225 assert (vls->width <= MAX_SHORT_STRING);
226 if (vls->labels != NULL)
228 struct int_val_lab *new = create_int_val_lab (vls, value, label);
229 struct int_val_lab *old = hsh_replace (vls->labels, new);
231 free_int_val_lab (old, vls);
234 val_labs_add (vls, value, label);
237 /* Removes any value label for VALUE within VLS. Returns true
238 if a value label was removed. Behavior is undefined if VLS's
239 width is greater than MAX_SHORT_STRING. */
241 val_labs_remove (struct val_labs *vls, union value value)
243 assert (vls != NULL);
244 assert (vls->width <= MAX_SHORT_STRING);
246 if (vls->labels != NULL)
248 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
249 int deleted = hsh_delete (vls->labels, ivl);
257 /* Searches VLS for a value label for VALUE. If successful,
258 returns the label; otherwise, returns a null pointer. If
259 VLS's width is greater than MAX_SHORT_STRING, always returns a
262 val_labs_find (const struct val_labs *vls, union value value)
265 && vls->width <= MAX_SHORT_STRING
266 && vls->labels != NULL)
268 struct int_val_lab ivl, *vlp;
271 vlp = hsh_find (vls->labels, &ivl);
273 return atom_to_string (vlp->label);
278 /* A value labels iterator. */
279 struct val_labs_iterator
281 void **labels; /* The labels, in order. */
282 void **lp; /* Current label. */
283 struct val_lab vl; /* Structure presented to caller. */
286 /* Sets up *IP for iterating through the value labels in VLS in
287 no particular order. Returns the first value label or a null
288 pointer if VLS is empty. If the return value is non-null,
289 then val_labs_next() may be used to continue iterating or
290 val_labs_done() to free up the iterator. Otherwise, neither
291 function may be called for *IP. */
293 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
295 struct val_labs_iterator *i;
297 assert (vls != NULL);
300 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
303 i = *ip = xmalloc (sizeof *i);
304 i->labels = hsh_data_copy (vls->labels);
306 return val_labs_next (vls, ip);
309 /* Sets up *IP for iterating through the value labels in VLS in
310 sorted order of values. Returns the first value label or a
311 null pointer if VLS is empty. If the return value is
312 non-null, then val_labs_next() may be used to continue
313 iterating or val_labs_done() to free up the iterator.
314 Otherwise, neither function may be called for *IP. */
316 val_labs_first_sorted (const struct val_labs *vls,
317 struct val_labs_iterator **ip)
319 struct val_labs_iterator *i;
321 assert (vls != NULL);
324 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
327 i = *ip = xmalloc (sizeof *i);
328 i->lp = i->labels = hsh_sort_copy (vls->labels);
329 return val_labs_next (vls, ip);
332 /* Returns the next value label in an iteration begun by
333 val_labs_first() or val_labs_first_sorted(). If the return
334 value is non-null, then val_labs_next() may be used to
335 continue iterating or val_labs_done() to free up the iterator.
336 Otherwise, neither function may be called for *IP. */
338 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
340 struct val_labs_iterator *i;
341 struct int_val_lab *ivl;
343 assert (vls != NULL);
344 assert (vls->width <= MAX_SHORT_STRING);
346 assert (*ip != NULL);
352 i->vl.value = ivl->value;
353 i->vl.label = atom_to_string (ivl->label);
365 /* Discards the state for an incomplete iteration begun by
366 val_labs_first() or val_labs_first_sorted(). */
368 val_labs_done (struct val_labs_iterator **ip)
370 struct val_labs_iterator *i;
373 assert (*ip != NULL);
381 /* Compares two value labels and returns a strcmp()-type result. */
383 compare_int_val_lab (const void *a_, const void *b_, const void *vls_)
385 const struct int_val_lab *a = a_;
386 const struct int_val_lab *b = b_;
387 const struct val_labs *vls = vls_;
390 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
392 return memcmp (a->value.s, b->value.s, vls->width);
395 /* Hash a value label. */
397 hash_int_val_lab (const void *vl_, const void *vls_)
399 const struct int_val_lab *vl = vl_;
400 const struct val_labs *vls = vls_;
403 return hsh_hash_double (vl->value.f);
405 return hsh_hash_bytes (vl->value.s, vls->width);
408 /* Free a value label. */
410 free_int_val_lab (void *vl_, const void *vls_ UNUSED)
412 struct int_val_lab *vl = vl_;
414 atom_destroy (vl->label);
423 char *string; /* String value. */
424 unsigned ref_count; /* Number of references. */
427 static hsh_compare_func compare_atoms;
428 static hsh_hash_func hash_atom;
429 static hsh_free_func free_atom;
431 /* Hash table of atoms. */
432 static struct hsh_table *atoms;
440 /* Creates and returns an atom for STRING. */
442 atom_create (const char *string)
447 assert (string != NULL);
451 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
452 atexit (destroy_atoms);
455 a.string = (char *) string;
456 app = hsh_probe (atoms, &a);
459 struct atom *ap = *app;
465 struct atom *ap = xmalloc (sizeof *ap);
466 ap->string = xstrdup (string);
475 atom_destroy (struct atom *atom)
479 assert (atom->ref_count > 0);
481 if (atom->ref_count == 0)
482 hsh_force_delete (atoms, atom);
486 /* Returns the string associated with ATOM. */
488 atom_to_string (const struct atom *atom)
490 assert (atom != NULL);
495 /* A hsh_compare_func that compares A and B. */
497 compare_atoms (const void *a_, const void *b_, const void *aux UNUSED)
499 const struct atom *a = a_;
500 const struct atom *b = b_;
502 return strcmp (a->string, b->string);
505 /* A hsh_hash_func that hashes ATOM. */
507 hash_atom (const void *atom_, const void *aux UNUSED)
509 const struct atom *atom = atom_;
511 return hsh_hash_string (atom->string);
514 /* A hsh_free_func that destroys ATOM. */
516 free_atom (void *atom_, const void *aux UNUSED)
518 struct atom *atom = atom_;