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., 59 Temple Place - Suite 330, Boston, MA
26 #include "value-labels.h"
28 static hsh_compare_func compare_int_val_lab;
29 static hsh_hash_func hash_int_val_lab;
30 static hsh_free_func free_int_val_lab;
33 static struct atom *atom_create (const char *string);
34 static void atom_destroy (struct atom *);
35 static const char *atom_to_string (const struct atom *);
37 /* A set of value labels. */
40 int width; /* 0=numeric, otherwise string width. */
41 struct hsh_table *labels; /* Hash table of `struct int_val_lab's. */
44 /* Creates and returns a new, empty set of value labels with the
45 given WIDTH, which must designate a numeric (0) or short
46 string (1...MAX_SHORT_STRING inclusive) width. */
48 val_labs_create (int width)
54 vls = xmalloc (sizeof *vls);
60 /* Creates and returns a new set of value labels identical to
63 val_labs_copy (const struct val_labs *vls)
65 struct val_labs *copy;
66 struct val_labs_iterator *i;
71 copy = val_labs_create (vls->width);
72 for (vl = val_labs_first (vls, &i); vl != NULL;
73 vl = val_labs_next (vls, &i))
74 val_labs_add (copy, vl->value, vl->label);
78 /* Changes the width of VLS to NEW_WIDTH. If VLS is numeric,
79 NEW_WIDTH must be 0, otherwise it must be within the range
80 1...MAX_SHORT_STRING inclusive. */
82 val_labs_set_width (struct val_labs *vls, int new_width)
85 assert ((vls->width == 0) == (new_width == 0));
87 vls->width = new_width;
92 val_labs_destroy (struct val_labs *vls)
94 if (vls != NULL && vls->labels != NULL)
95 hsh_destroy (vls->labels);
98 /* Removes all the value labels from VLS. */
100 val_labs_clear (struct val_labs *vls)
102 assert (vls != NULL);
104 hsh_destroy (vls->labels);
108 /* Returns the number of value labels in VLS. */
110 val_labs_count (struct val_labs *vls)
112 assert (vls != NULL);
114 if (vls->labels == NULL)
117 return hsh_count (vls->labels);
120 /* One value label in internal format. */
123 union value value; /* The value being labeled. */
124 struct atom *label; /* A ref-counted string. */
127 /* Creates and returns an int_val_lab based on VALUE and
129 static struct int_val_lab *
130 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
132 struct int_val_lab *ivl;
134 assert (label != NULL);
135 assert (vls->width <= MAX_SHORT_STRING);
137 ivl = xmalloc (sizeof *ivl);
140 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
141 ivl->label = atom_create (label);
146 /* If VLS does not already contain a value label for VALUE, adds
147 LABEL for it and returns nonzero. Otherwise, returns zero.
148 Behavior is undefined if VLS's width is greater than
151 val_labs_add (struct val_labs *vls, union value value, const char *label)
153 struct int_val_lab *ivl;
156 assert (vls != NULL);
157 assert (vls->width <= MAX_SHORT_STRING);
158 assert (label != NULL);
160 if (vls->labels == NULL)
161 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
162 free_int_val_lab, vls);
164 ivl = create_int_val_lab (vls, value, label);
165 vlpp = hsh_probe (vls->labels, ivl);
173 free_int_val_lab (ivl, vls);
178 /* Sets LABEL as the value label for VALUE in VLS. Returns zero
179 if there wasn't already a value label for VALUE, or nonzero if
180 there was. Behavior is undefined if VLS's width is greater
181 than MAX_SHORT_STRING. */
183 val_labs_replace (struct val_labs *vls, union value value, const char *label)
185 struct int_val_lab *ivl;
187 assert (vls != NULL);
188 assert (vls->width <= MAX_SHORT_STRING);
189 assert (label != NULL);
191 if (vls->labels == NULL)
193 val_labs_add (vls, value, label);
197 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
202 free_int_val_lab (ivl, vls);
207 /* Removes any value label for VALUE within VLS. Returns nonzero
208 if a value label was removed. Behavior is undefined if VLS's
209 width is greater than MAX_SHORT_STRING. */
211 val_labs_remove (struct val_labs *vls, union value value)
213 assert (vls != NULL);
214 assert (vls->width <= MAX_SHORT_STRING);
216 if (vls->labels != NULL)
218 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
219 int deleted = hsh_delete (vls->labels, &ivl);
227 /* Searches VLS for a value label for VALUE. If successful,
228 returns the label; otherwise, returns a null pointer. If
229 VLS's width is greater than MAX_SHORT_STRING, always returns a
232 val_labs_find (const struct val_labs *vls, union value value)
234 assert (vls != NULL);
236 if (vls->width > MAX_SHORT_STRING)
239 if (vls->labels != NULL)
241 struct int_val_lab ivl, *vlp;
244 vlp = hsh_find (vls->labels, &ivl);
246 return atom_to_string (vlp->label);
251 /* A value labels iterator. */
252 struct val_labs_iterator
254 void **labels; /* The labels, in order. */
255 void **lp; /* Current label. */
256 struct val_lab vl; /* Structure presented to caller. */
259 /* Sets up *IP for iterating through the value labels in VLS in
260 no particular order. Returns the first value label or a null
261 pointer if VLS is empty. If the return value is non-null,
262 then val_labs_next() may be used to continue iterating or
263 val_labs_done() to free up the iterator. Otherwise, neither
264 function may be called for *IP. */
266 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
268 struct val_labs_iterator *i;
270 assert (vls != NULL);
273 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
276 i = *ip = xmalloc (sizeof *i);
277 i->labels = hsh_data_copy (vls->labels);
279 return val_labs_next (vls, ip);
282 /* Sets up *IP for iterating through the value labels in VLS in
283 sorted order of values. Returns the first value label or a
284 null pointer if VLS is empty. If the return value is
285 non-null, then val_labs_next() may be used to continue
286 iterating or val_labs_done() to free up the iterator.
287 Otherwise, neither function may be called for *IP. */
289 val_labs_first_sorted (const struct val_labs *vls,
290 struct val_labs_iterator **ip)
292 struct val_labs_iterator *i;
294 assert (vls != NULL);
297 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
300 i = *ip = xmalloc (sizeof *i);
301 i->lp = i->labels = hsh_sort_copy (vls->labels);
302 return val_labs_next (vls, ip);
305 /* Returns the next value label in an iteration begun by
306 val_labs_first() or val_labs_first_sorted(). If the return
307 value is non-null, then val_labs_next() may be used to
308 continue iterating or val_labs_done() to free up the iterator.
309 Otherwise, neither function may be called for *IP. */
311 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
313 struct val_labs_iterator *i;
314 struct int_val_lab *ivl;
316 assert (vls != NULL);
317 assert (vls->width <= MAX_SHORT_STRING);
319 assert (*ip != NULL);
325 i->vl.value = ivl->value;
326 i->vl.label = atom_to_string (ivl->label);
338 /* Discards the state for an incomplete iteration begun by
339 val_labs_first() or val_labs_first_sorted(). */
341 val_labs_done (struct val_labs_iterator **ip)
343 struct val_labs_iterator *i;
346 assert (*ip != NULL);
354 /* Compares two value labels and returns a strcmp()-type result. */
356 compare_int_val_lab (const void *a_, const void *b_, void *vls_)
358 const struct int_val_lab *a = a_;
359 const struct int_val_lab *b = b_;
360 const struct val_labs *vls = vls_;
363 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
365 return memcmp (a->value.s, b->value.s, vls->width);
368 /* Hash a value label. */
370 hash_int_val_lab (const void *vl_, void *vls_)
372 const struct int_val_lab *vl = vl_;
373 const struct val_labs *vls = vls_;
376 return hsh_hash_double (vl->value.f);
378 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
381 /* Free a value label. */
383 free_int_val_lab (void *vl_, void *vls_ unused)
385 struct int_val_lab *vl = vl_;
387 atom_destroy (vl->label);
396 char *string; /* String value. */
397 unsigned ref_count; /* Number of references. */
400 static hsh_compare_func compare_atoms;
401 static hsh_hash_func hash_atom;
402 static hsh_free_func free_atom;
404 /* Hash table of atoms. */
405 static struct hsh_table *atoms;
407 /* Creates and returns an atom for STRING. */
409 atom_create (const char *string)
414 assert (string != NULL);
417 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
419 a.string = (char *) string;
420 app = hsh_probe (atoms, &a);
423 struct atom *ap = *app;
429 struct atom *ap = xmalloc (sizeof *ap);
430 ap->string = xstrdup (string);
439 atom_destroy (struct atom *atom)
443 assert (atom->ref_count > 0);
445 if (atom->ref_count == 0)
446 hsh_force_delete (atoms, atom);
450 /* Returns the string associated with ATOM. */
452 atom_to_string (const struct atom *atom)
454 assert (atom != NULL);
459 /* A hsh_compare_func that compares A and B. */
461 compare_atoms (const void *a_, const void *b_, void *aux unused)
463 const struct atom *a = a_;
464 const struct atom *b = b_;
466 return strcmp (a->string, b->string);
469 /* A hsh_hash_func that hashes ATOM. */
471 hash_atom (const void *atom_, void *aux unused)
473 const struct atom *atom = atom_;
475 return hsh_hash_string (atom->string);
478 /* A hsh_free_func that destroys ATOM. */
480 free_atom (void *atom_, void *aux unused)
482 struct atom *atom = atom_;