1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2012 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #define DATA_VALUE_H 1
20 #include "libpspp/compiler.h"
28 /* A numeric or string value. The client is responsible for keeping track of
36 static inline void value_init (union value *, int width);
37 static inline void value_clone (union value *, const union value *, int width);
38 static inline bool value_needs_init (int width);
39 static inline bool value_try_init (union value *, int width);
40 static inline void value_destroy (union value *, int width);
42 static inline void value_copy (union value *, const union value *, int width);
43 void value_copy_rpad (union value *, int dst_width,
44 const union value *, int src_width,
46 void value_copy_str_rpad (union value *, int dst_width, const uint8_t *,
48 void value_copy_buf_rpad (union value *dst, int dst_width,
49 const uint8_t *src, size_t src_len, char pad);
50 void value_set_missing (union value *, int width);
51 int value_compare_3way (const union value *, const union value *, int width);
52 bool value_equal (const union value *, const union value *, int width);
53 unsigned int value_hash (const union value *, int width, unsigned int basis) WARN_UNUSED_RESULT;
55 bool value_is_resizable (const union value *, int old_width, int new_width);
56 bool value_needs_resize (int old_width, int new_width);
57 void value_resize (union value *, int old_width, int new_width);
59 bool value_is_spaces (const union value *, int width);
61 static inline void value_swap (union value *, union value *);
64 void value_init_pool (struct pool *, union value *, int width);
65 void value_clone_pool (struct pool *, union value *, const union value *,
67 void value_resize_pool (struct pool *, union value *,
68 int old_width, int new_width);
70 /* Initializes V as a value of the given WIDTH, where 0
71 represents a numeric value and a positive integer represents a
72 string value WIDTH bytes long.
74 A WIDTH of -1 is ignored.
76 The contents of value V are indeterminate after
79 value_init (union value *v, int width)
82 v->s = xmalloc (width);
87 /* Initializes V as a value of the given WIDTH, as with value_init(), and
88 copies SRC's value into V as its initial value. */
90 value_clone (union value *v, const union value *src, int width)
95 v->s = xmemdup (src->s, width);
98 /* Returns true if a value of the given WIDTH actually needs to
99 have the value_init and value_destroy functions called, false
100 if those functions are no-ops for values of the given WIDTH.
102 Using this function is only a valuable optimization if a large
103 number of values of the given WIDTH are to be initialized*/
105 value_needs_init (int width)
110 /* Same as value_init, except that failure to allocate memory
111 causes it to return false instead of terminating the
112 program. On success, returns true. */
114 value_try_init (union value *v, int width)
118 v->s = malloc (width);
125 /* Frees any memory allocated by value_init for V, which must
126 have the given WIDTH. */
128 value_destroy (union value *v, int width)
134 /* Copies SRC to DST, given that they both contain data of the
137 value_copy (union value *dst, const union value *src, int width)
142 memcpy (dst->s, src->s, width);
145 /* Exchanges the contents of A and B. */
147 value_swap (union value *a, union value *b)
149 union value tmp = *a;
154 #endif /* data/value.h */