97c24050ef6264712e05c2dd8b98ea069a0ccd89
[pspp-builds.git] / src / data / value.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 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/misc.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include "minmax.h"
24
25 /* "Short" strings, which are generally those no more than 8
26    characters wide, can participate in more operations than
27    longer strings. */
28 #define MAX_SHORT_STRING (MAX (ROUND_UP (SIZEOF_DOUBLE, 2), 8))
29 #define MIN_LONG_STRING (MAX_SHORT_STRING + 1)
30
31 /* A numeric or short string value.
32    Multiple consecutive values represent a long string. */
33 union value
34   {
35     double f;
36     char s[MAX_SHORT_STRING];
37   };
38
39 union value *value_dup (const union value *, int width);
40 union value *value_create (int width);
41
42 int compare_values (const void *, const void *, const void *var);
43 unsigned hash_value (const void *, const void *var);
44
45 int compare_values_short (const void *, const void *, const void *var);
46 unsigned hash_value_short (const void *, const void *var);
47
48 static inline size_t value_cnt_from_width (int width);
49 void value_copy (union value *, const union value *, int width);
50 void value_set_missing (union value *, int width);
51 bool value_is_resizable (const union value *, int old_width, int new_width);
52 void value_resize (union value *, int old_width, int new_width);
53 int value_compare_3way (const union value *, const union value *, int width);
54
55 /* Number of "union value"s required for a variable of the given
56    WIDTH. */
57 static inline size_t
58 value_cnt_from_width (int width)
59 {
60   return width == 0 ? 1 : DIV_RND_UP (width, MAX_SHORT_STRING);
61 }
62
63 #endif /* data/value.h */