New library for interned strings.
[pspp-builds.git] / src / data / value-labels.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "value-labels.h"
20
21 #include <stdlib.h>
22
23 #include <data/data-out.h>
24 #include <data/value.h>
25 #include <data/variable.h>
26 #include <libpspp/array.h>
27 #include <libpspp/cast.h>
28 #include <libpspp/compiler.h>
29 #include <libpspp/hash-functions.h>
30 #include <libpspp/hmap.h>
31 #include <libpspp/intern.h>
32 #include <libpspp/message.h>
33 #include <libpspp/str.h>
34
35 #include "xalloc.h"
36
37 /* Creates and returns a new, empty set of value labels with the
38    given WIDTH. */
39 struct val_labs *
40 val_labs_create (int width)
41 {
42   struct val_labs *vls = xmalloc (sizeof *vls);
43   vls->width = width;
44   hmap_init (&vls->labels);
45   return vls;
46 }
47
48 /* Creates and returns a new set of value labels identical to
49    VLS.  Returns a null pointer if VLS is null. */
50 struct val_labs *
51 val_labs_clone (const struct val_labs *vls)
52 {
53   struct val_labs *copy;
54   struct val_lab *label;
55
56   if (vls == NULL)
57     return NULL;
58
59   copy = val_labs_create (vls->width);
60   HMAP_FOR_EACH (label, struct val_lab, node, &vls->labels)
61     val_labs_add (copy, &label->value, label->label);
62   return copy;
63 }
64
65 /* Determines whether VLS's width can be changed to NEW_WIDTH,
66    using the rules checked by value_is_resizable. */
67 bool
68 val_labs_can_set_width (const struct val_labs *vls, int new_width)
69 {
70   struct val_lab *label;
71
72   HMAP_FOR_EACH (label, struct val_lab, node, &vls->labels)
73     if (!value_is_resizable (&label->value, vls->width, new_width))
74       return false;
75
76   return true;
77 }
78
79 /* Changes the width of VLS to NEW_WIDTH.  The original and new
80    width must be both numeric or both string. */
81 void
82 val_labs_set_width (struct val_labs *vls, int new_width)
83 {
84   assert (val_labs_can_set_width (vls, new_width));
85   if (value_needs_resize (vls->width, new_width))
86     {
87       struct val_lab *label;
88       HMAP_FOR_EACH (label, struct val_lab, node, &vls->labels)
89         value_resize (&label->value, vls->width, new_width);
90     }
91   vls->width = new_width;
92 }
93
94 /* Destroys VLS. */
95 void
96 val_labs_destroy (struct val_labs *vls)
97 {
98   if (vls != NULL)
99     {
100       val_labs_clear (vls);
101       hmap_destroy (&vls->labels);
102       free (vls);
103     }
104 }
105
106 /* Removes all the value labels from VLS. */
107 void
108 val_labs_clear (struct val_labs *vls)
109 {
110   struct val_lab *label, *next;
111
112   HMAP_FOR_EACH_SAFE (label, next, struct val_lab, node, &vls->labels)
113     {
114       hmap_delete (&vls->labels, &label->node);
115       value_destroy (&label->value, vls->width);
116       intern_unref (label->label);
117       free (label);
118     }
119 }
120
121 /* Returns the number of value labels in VLS.
122    Returns 0 if VLS is null. */
123 size_t
124 val_labs_count (const struct val_labs *vls)
125 {
126   return vls == NULL ? 0 : hmap_count (&vls->labels);
127 }
128 \f
129 static void
130 do_add_val_lab (struct val_labs *vls, const union value *value,
131                 const char *label)
132 {
133   struct val_lab *lab = xmalloc (sizeof *lab);
134   value_init (&lab->value, vls->width);
135   value_copy (&lab->value, value, vls->width);
136   lab->label = intern_new (label);
137   hmap_insert (&vls->labels, &lab->node, value_hash (value, vls->width, 0));
138 }
139
140 /* If VLS does not already contain a value label for VALUE, adds
141    LABEL for it and returns true.  Otherwise, returns false. */
142 bool
143 val_labs_add (struct val_labs *vls, const union value *value,
144               const char *label)
145 {
146   const struct val_lab *lab = val_labs_lookup (vls, value);
147   if (lab == NULL)
148     {
149       do_add_val_lab (vls, value, label);
150       return true;
151     }
152   else
153     return false;
154 }
155
156 /* Sets LABEL as the value label for VALUE in VLS, replacing any
157    existing label for VALUE. */
158 void
159 val_labs_replace (struct val_labs *vls, const union value *value,
160                   const char *label)
161 {
162   struct val_lab *vl = val_labs_lookup (vls, value);
163   if (vl != NULL)
164     {
165       intern_unref (vl->label);
166       vl->label = intern_new (label);
167     }
168   else
169     do_add_val_lab (vls, value, label);
170 }
171
172 /* Removes LABEL from VLS. */
173 void
174 val_labs_remove (struct val_labs *vls, struct val_lab *label)
175 {
176   hmap_delete (&vls->labels, &label->node);
177   value_destroy (&label->value, vls->width);
178   intern_unref (label->label);
179   free (label);
180 }
181
182 /* Searches VLS for a value label for VALUE.  If successful,
183    returns the string used as the label; otherwise, returns a
184    null pointer.  Returns a null pointer if VLS is null. */
185 const char *
186 val_labs_find (const struct val_labs *vls, const union value *value)
187 {
188   const struct val_lab *label = val_labs_lookup (vls, value);
189   return label ? label->label : NULL;
190 }
191
192 /* Searches VLS for a value label for VALUE.  If successful,
193    returns the value label; otherwise, returns a null pointer.
194    Returns a null pointer if VLS is null. */
195 struct val_lab *
196 val_labs_lookup (const struct val_labs *vls, const union value *value)
197 {
198   if (vls != NULL)
199     {
200       struct val_lab *label;
201       HMAP_FOR_EACH_WITH_HASH (label, struct val_lab, node,
202                                value_hash (value, vls->width, 0), &vls->labels)
203         if (value_equal (&label->value, value, vls->width))
204           return label;
205     }
206   return NULL;
207 }
208 \f
209 /* Returns the first value label in VLS, in arbitrary order, or a
210    null pointer if VLS is empty or if VLS is a null pointer.  If
211    the return value is non-null, then val_labs_next() may be used
212    to continue iterating. */
213 const struct val_lab *
214 val_labs_first (const struct val_labs *vls)
215 {
216   return vls ? HMAP_FIRST (struct val_lab, node, &vls->labels) : NULL;
217 }
218
219 /* Returns the next value label in an iteration begun by
220    val_labs_first().  If the return value is non-null, then
221    val_labs_next() may be used to continue iterating. */
222 const struct val_lab *
223 val_labs_next (const struct val_labs *vls, const struct val_lab *label)
224 {
225   return HMAP_NEXT (label, struct val_lab, node, &vls->labels);
226 }
227
228 static int
229 compare_labels_by_value_3way (const void *a_, const void *b_, const void *vls_)
230 {
231   const struct val_lab *const *a = a_;
232   const struct val_lab *const *b = b_;
233   const struct val_labs *vls = vls_;
234   return value_compare_3way (&(*a)->value, &(*b)->value, vls->width);
235 }
236
237 /* Allocates and returns an array of pointers to value labels
238    that is sorted in increasing order by value.  The array has
239    val_labs_count(VLS) elements.  The caller is responsible for
240    freeing the array. */
241 const struct val_lab **
242 val_labs_sorted (const struct val_labs *vls)
243 {
244   if (vls != NULL)
245     {
246       const struct val_lab *label;
247       const struct val_lab **labels;
248       size_t i;
249
250       labels = xmalloc (val_labs_count (vls) * sizeof *labels);
251       i = 0;
252       HMAP_FOR_EACH (label, struct val_lab, node, &vls->labels)
253         labels[i++] = label;
254       assert (i == val_labs_count (vls));
255       sort (labels, val_labs_count (vls), sizeof *labels,
256             compare_labels_by_value_3way, vls);
257       return labels;
258     }
259   else
260     return NULL;
261 }