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