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., 51 Franklin Street, Fifth Floor, Boston, MA
21 #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 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)
96 if (vls->labels != NULL)
97 hsh_destroy (vls->labels);
102 /* Removes all the value labels from VLS. */
104 val_labs_clear (struct val_labs *vls)
106 assert (vls != NULL);
108 hsh_destroy (vls->labels);
112 /* Returns the number of value labels in VLS. */
114 val_labs_count (struct val_labs *vls)
116 assert (vls != NULL);
118 if (vls->labels == NULL)
121 return hsh_count (vls->labels);
124 /* One value label in internal format. */
127 union value value; /* The value being labeled. */
128 struct atom *label; /* A ref-counted string. */
131 /* Creates and returns an int_val_lab based on VALUE and
133 static struct int_val_lab *
134 create_int_val_lab (struct val_labs *vls, union value value, const char *label)
136 struct int_val_lab *ivl;
138 assert (label != NULL);
139 assert (vls->width <= MAX_SHORT_STRING);
141 ivl = xmalloc (sizeof *ivl);
144 memset (ivl->value.s + vls->width, ' ', MAX_SHORT_STRING - vls->width);
145 ivl->label = atom_create (label);
150 /* If VLS does not already contain a value label for VALUE, adds
151 LABEL for it and returns nonzero. Otherwise, returns zero.
152 Behavior is undefined if VLS's width is greater than
155 val_labs_add (struct val_labs *vls, union value value, const char *label)
157 struct int_val_lab *ivl;
160 assert (vls != NULL);
161 assert (vls->width <= MAX_SHORT_STRING);
162 assert (label != NULL);
164 if (vls->labels == NULL)
165 vls->labels = hsh_create (8, compare_int_val_lab, hash_int_val_lab,
166 free_int_val_lab, vls);
168 ivl = create_int_val_lab (vls, value, label);
169 vlpp = hsh_probe (vls->labels, ivl);
177 free_int_val_lab (ivl, vls);
182 /* Sets LABEL as the value label for VALUE in VLS. Returns zero
183 if there wasn't already a value label for VALUE, or nonzero if
184 there was. Behavior is undefined if VLS's width is greater
185 than MAX_SHORT_STRING. */
187 val_labs_replace (struct val_labs *vls, union value value, const char *label)
189 struct int_val_lab *ivl;
191 assert (vls != NULL);
192 assert (vls->width <= MAX_SHORT_STRING);
193 assert (label != NULL);
195 if (vls->labels == NULL)
197 val_labs_add (vls, value, label);
201 ivl = hsh_replace (vls->labels, create_int_val_lab (vls, value, label));
206 free_int_val_lab (ivl, vls);
211 /* Removes any value label for VALUE within VLS. Returns nonzero
212 if a value label was removed. Behavior is undefined if VLS's
213 width is greater than MAX_SHORT_STRING. */
215 val_labs_remove (struct val_labs *vls, union value value)
217 assert (vls != NULL);
218 assert (vls->width <= MAX_SHORT_STRING);
220 if (vls->labels != NULL)
222 struct int_val_lab *ivl = create_int_val_lab (vls, value, "");
223 int deleted = hsh_delete (vls->labels, &ivl);
231 /* Searches VLS for a value label for VALUE. If successful,
232 returns the label; otherwise, returns a null pointer. If
233 VLS's width is greater than MAX_SHORT_STRING, always returns a
236 val_labs_find (const struct val_labs *vls, union value value)
238 assert (vls != NULL);
240 if (vls->width > MAX_SHORT_STRING)
243 if (vls->labels != NULL)
245 struct int_val_lab ivl, *vlp;
248 vlp = hsh_find (vls->labels, &ivl);
250 return atom_to_string (vlp->label);
255 /* A value labels iterator. */
256 struct val_labs_iterator
258 void **labels; /* The labels, in order. */
259 void **lp; /* Current label. */
260 struct val_lab vl; /* Structure presented to caller. */
263 /* Sets up *IP for iterating through the value labels in VLS in
264 no particular order. Returns the first value label or a null
265 pointer if VLS is empty. If the return value is non-null,
266 then val_labs_next() may be used to continue iterating or
267 val_labs_done() to free up the iterator. Otherwise, neither
268 function may be called for *IP. */
270 val_labs_first (const struct val_labs *vls, struct val_labs_iterator **ip)
272 struct val_labs_iterator *i;
274 assert (vls != NULL);
277 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
280 i = *ip = xmalloc (sizeof *i);
281 i->labels = hsh_data_copy (vls->labels);
283 return val_labs_next (vls, ip);
286 /* Sets up *IP for iterating through the value labels in VLS in
287 sorted order of values. Returns the first value label or a
288 null pointer if VLS is empty. If the return value is
289 non-null, then val_labs_next() may be used to continue
290 iterating or val_labs_done() to free up the iterator.
291 Otherwise, neither function may be called for *IP. */
293 val_labs_first_sorted (const struct val_labs *vls,
294 struct val_labs_iterator **ip)
296 struct val_labs_iterator *i;
298 assert (vls != NULL);
301 if (vls->labels == NULL || vls->width > MAX_SHORT_STRING)
304 i = *ip = xmalloc (sizeof *i);
305 i->lp = i->labels = hsh_sort_copy (vls->labels);
306 return val_labs_next (vls, ip);
309 /* Returns the next value label in an iteration begun by
310 val_labs_first() or val_labs_first_sorted(). If the return
311 value is non-null, then val_labs_next() may be used to
312 continue iterating or val_labs_done() to free up the iterator.
313 Otherwise, neither function may be called for *IP. */
315 val_labs_next (const struct val_labs *vls, struct val_labs_iterator **ip)
317 struct val_labs_iterator *i;
318 struct int_val_lab *ivl;
320 assert (vls != NULL);
321 assert (vls->width <= MAX_SHORT_STRING);
323 assert (*ip != NULL);
329 i->vl.value = ivl->value;
330 i->vl.label = atom_to_string (ivl->label);
342 /* Discards the state for an incomplete iteration begun by
343 val_labs_first() or val_labs_first_sorted(). */
345 val_labs_done (struct val_labs_iterator **ip)
347 struct val_labs_iterator *i;
350 assert (*ip != NULL);
358 /* Compares two value labels and returns a strcmp()-type result. */
360 compare_int_val_lab (const void *a_, const void *b_, void *vls_)
362 const struct int_val_lab *a = a_;
363 const struct int_val_lab *b = b_;
364 const struct val_labs *vls = vls_;
367 return a->value.f < b->value.f ? -1 : a->value.f > b->value.f;
369 return memcmp (a->value.s, b->value.s, vls->width);
372 /* Hash a value label. */
374 hash_int_val_lab (const void *vl_, void *vls_)
376 const struct int_val_lab *vl = vl_;
377 const struct val_labs *vls = vls_;
380 return hsh_hash_double (vl->value.f);
382 return hsh_hash_bytes (vl->value.s, sizeof vl->value.s);
385 /* Free a value label. */
387 free_int_val_lab (void *vl_, void *vls_ UNUSED)
389 struct int_val_lab *vl = vl_;
391 atom_destroy (vl->label);
400 char *string; /* String value. */
401 unsigned ref_count; /* Number of references. */
404 static hsh_compare_func compare_atoms;
405 static hsh_hash_func hash_atom;
406 static hsh_free_func free_atom;
408 /* Hash table of atoms. */
409 static struct hsh_table *atoms;
411 /* Creates and returns an atom for STRING. */
413 atom_create (const char *string)
418 assert (string != NULL);
421 atoms = hsh_create (8, compare_atoms, hash_atom, free_atom, NULL);
423 a.string = (char *) string;
424 app = hsh_probe (atoms, &a);
427 struct atom *ap = *app;
433 struct atom *ap = xmalloc (sizeof *ap);
434 ap->string = xstrdup (string);
443 atom_destroy (struct atom *atom)
447 assert (atom->ref_count > 0);
449 if (atom->ref_count == 0)
450 hsh_force_delete (atoms, atom);
454 /* Returns the string associated with ATOM. */
456 atom_to_string (const struct atom *atom)
458 assert (atom != NULL);
463 /* A hsh_compare_func that compares A and B. */
465 compare_atoms (const void *a_, const void *b_, void *aux UNUSED)
467 const struct atom *a = a_;
468 const struct atom *b = b_;
470 return strcmp (a->string, b->string);
473 /* A hsh_hash_func that hashes ATOM. */
475 hash_atom (const void *atom_, void *aux UNUSED)
477 const struct atom *atom = atom_;
479 return hsh_hash_string (atom->string);
482 /* A hsh_free_func that destroys ATOM. */
484 free_atom (void *atom_, void *aux UNUSED)
486 struct atom *atom = atom_;
493 /* Get a string representing the value.
494 That is, if it has a label, then return that label,
495 otherwise, if the value is alpha, then return the string for it,
496 else format it and return the formatted string
499 value_to_string(const union value *val, const struct variable *var)
501 static char buf[100];
503 const struct val_labs *val_labs ;
508 val_labs = var->val_labs;
511 s = val_labs_find (val_labs, *val);
516 if ( 0 == var->width )
517 snprintf(buf,100,"%g",val->f);
520 strncpy(buf,val->s,MAX_SHORT_STRING);