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