Add the WARN_UNUSED_RESULT attribute to all hash functions.
[pspp] / src / data / value.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2012 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 #ifndef DATA_VALUE_H
18 #define DATA_VALUE_H 1
19
20 #include "libpspp/compiler.h"
21 #include <assert.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include "xalloc.h"
27 \f
28 /* Maximum length of a "short" string, that is represented in
29    "union value" without a separate pointer.
30
31    This is an implementation detail of the "union value" code.
32    There is little reason for client code to use it. */
33 #define MAX_SHORT_STRING 8
34
35 /* A numeric or string value.
36
37    The client is responsible for keeping track of the value's
38    width.
39
40    This structure is semi-opaque:
41
42        - If the value is a number, clients may access the 'f'
43          member directly.
44
45        - Clients should not access other members directly.
46 */
47 union value
48   {
49     double f;
50     uint8_t short_string[MAX_SHORT_STRING];
51     uint8_t *long_string;
52   };
53
54 static inline void value_init (union value *, int width);
55 static inline void value_clone (union value *, const union value *, int width);
56 static inline bool value_needs_init (int width);
57 static inline bool value_try_init (union value *, int width);
58 static inline void value_destroy (union value *, int width);
59
60 static inline double value_num (const union value *);
61 static inline const uint8_t *value_str (const union value *, int width);
62 static inline uint8_t *value_str_rw (union value *, int width);
63
64 static inline void value_copy (union value *, const union value *, int width);
65 void value_copy_rpad (union value *, int dst_width,
66                       const union value *, int src_width,
67                       char pad);
68 void value_copy_str_rpad (union value *, int dst_width, const uint8_t *,
69                           char pad);
70 void value_copy_buf_rpad (union value *dst, int dst_width,
71                           const uint8_t *src, size_t src_len, char pad);
72 void value_set_missing (union value *, int width);
73 int value_compare_3way (const union value *, const union value *, int width);
74 bool value_equal (const union value *, const union value *, int width);
75 unsigned int value_hash (const union value *, int width, unsigned int basis) WARN_UNUSED_RESULT;
76
77 bool value_is_resizable (const union value *, int old_width, int new_width);
78 bool value_needs_resize (int old_width, int new_width);
79 void value_resize (union value *, int old_width, int new_width);
80
81 bool value_is_spaces (const union value *, int width);
82
83 static inline void value_swap (union value *, union value *);
84
85 struct pool;
86 void value_init_pool (struct pool *, union value *, int width);
87 void value_clone_pool (struct pool *, union value *, const union value *,
88                        int width);
89 void value_resize_pool (struct pool *, union value *,
90                         int old_width, int new_width);
91 \f
92 /* Initializes V as a value of the given WIDTH, where 0
93    represents a numeric value and a positive integer represents a
94    string value WIDTH bytes long.
95
96    A WIDTH of -1 is ignored.
97
98    The contents of value V are indeterminate after
99    initialization. */
100 static inline void
101 value_init (union value *v, int width)
102 {
103   if (width > MAX_SHORT_STRING)
104     v->long_string = xmalloc (width);
105 }
106
107 /* Initializes V as a value of the given WIDTH, as with value_init(), and
108    copies SRC's value into V as its initial value. */
109 static inline void
110 value_clone (union value *v, const union value *src, int width)
111 {
112   if (width <= MAX_SHORT_STRING)
113     *v = *src;
114   else
115     v->long_string = xmemdup (src->long_string, width);
116 }
117
118 /* Returns true if a value of the given WIDTH actually needs to
119    have the value_init and value_destroy functions called, false
120    if those functions are no-ops for values of the given WIDTH.
121
122    Using this function is only a valuable optimization if a large
123    number of values of the given WIDTH are to be initialized*/
124 static inline bool
125 value_needs_init (int width)
126 {
127   return width > MAX_SHORT_STRING;
128 }
129
130 /* Same as value_init, except that failure to allocate memory
131    causes it to return false instead of terminating the
132    program.  On success, returns true. */
133 static inline bool
134 value_try_init (union value *v, int width)
135 {
136   if (width > MAX_SHORT_STRING)
137     {
138       v->long_string = malloc (width);
139       return v->long_string != NULL;
140     }
141   else
142     return true;
143 }
144
145 /* Frees any memory allocated by value_init for V, which must
146    have the given WIDTH. */
147 static inline void
148 value_destroy (union value *v, int width)
149 {
150   if (width > MAX_SHORT_STRING)
151     free (v->long_string);
152 }
153
154 /* Returns the numeric value in V, which must have width 0. */
155 static inline double
156 value_num (const union value *v)
157 {
158   return v->f;
159 }
160
161 /* Returns the string value in V, which must have width WIDTH.
162
163    The returned value is not null-terminated.
164
165    It is important that WIDTH be the actual value that was passed
166    to value_init.  Passing, e.g., a smaller value because only
167    that number of bytes will be accessed will not always work. */
168 static inline const uint8_t *
169 value_str (const union value *v, int width)
170 {
171   assert (width > 0);
172   return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
173 }
174
175 /* Returns the string value in V, which must have width WIDTH.
176
177    The returned value is not null-terminated.
178
179    It is important that WIDTH be the actual value that was passed
180    to value_init.  Passing, e.g., a smaller value because only
181    that number of bytes will be accessed will not always work. */
182 static inline uint8_t *
183 value_str_rw (union value *v, int width)
184 {
185   assert (width > 0);
186   return (width > MAX_SHORT_STRING ? v->long_string : v->short_string);
187 }
188
189 /* Copies SRC to DST, given that they both contain data of the
190    given WIDTH. */
191 static inline void
192 value_copy (union value *dst, const union value *src, int width)
193 {
194   if (width <= MAX_SHORT_STRING)
195     *dst = *src;
196   else if (dst != src)
197     memcpy (dst->long_string, src->long_string, width);
198 }
199
200 /* Exchanges the contents of A and B. */
201 static inline void
202 value_swap (union value *a, union value *b)
203 {
204   union value tmp = *a;
205   *a = *b;
206   *b = tmp;
207 }
208
209 #endif /* data/value.h */