Fixed bug reporting the significance of paired value t-test.
[pspp-builds.git] / src / data / value.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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 #include <config.h>
18 #include <data/value.h>
19
20 #include <data/val-type.h>
21 #include <libpspp/hash.h>
22 #include <libpspp/str.h>
23
24 #include "xalloc.h"
25
26 /* Duplicate a value.
27    The caller is responsible for freeing the returned value. */
28 union value *
29 value_dup (const union value *val, int width)
30 {
31   return xmemdup (val, MAX (width, sizeof *val));
32 }
33
34
35 /* Create a value of specified width.
36    The caller is responsible for freeing the returned value. */
37 union value *
38 value_create (int width)
39 {
40   return xnmalloc (value_cnt_from_width (width), sizeof (union value));
41 }
42
43
44 /* Compares A and B, which both have the given WIDTH, and returns
45    a strcmp()-type result.
46    Only the short string portion of longer strings are
47    compared. */
48 int
49 compare_values (const union value *a, const union value *b, int width)
50 {
51   return (width == 0
52           ? (a->f < b->f ? -1 : a->f > b->f)
53           : memcmp (a->s, b->s, MIN (MAX_SHORT_STRING, width)));
54 }
55
56 /* Create a hash of V, which has the given WIDTH.
57    Only the short string portion of a longer string is hashed. */
58 unsigned
59 hash_value (const union value *v, int width)
60 {
61   return (width == 0
62           ? hsh_hash_double (v->f)
63           : hsh_hash_bytes (v->s, MIN (MAX_SHORT_STRING, width)));
64 }
65
66
67 int
68 compare_ptr_values (const union value **v1, const union value **v2, int width)
69 {
70   return compare_values (*v1, *v2, width);
71 }
72
73 unsigned
74 hash_ptr_value (const union value **v, int width)
75 {
76   return hash_value (*v, width);
77 }
78
79
80 /* Copies SRC to DST, given that they both contain data of the
81    given WIDTH. */
82 void
83 value_copy (union value *dst, const union value *src, int width)
84 {
85   if (width == 0)
86     dst->f = src->f;
87   else
88     memcpy (dst->s, src->s, width);
89 }
90
91 /* Sets V to the system-missing value for data of the given
92    WIDTH. */
93 void
94 value_set_missing (union value *v, int width)
95 {
96   if (width == 0)
97     v->f = SYSMIS;
98   else
99     memset (v->s, ' ', width);
100 }
101
102 /* Tests whether VALUE may be resized from OLD_WIDTH to
103    NEW_WIDTH, using the following rules that match those for
104    resizing missing values and value labels.  First, OLD_WIDTH
105    and NEW_WIDTH must be both numeric or both string.  Second, if
106    NEW_WIDTH is less than OLD_WIDTH, then the bytes that would be
107    trimmed off the right end of VALUE must be all spaces. */
108 bool
109 value_is_resizable (const union value *value, int old_width, int new_width)
110 {
111   int i;
112
113   if (val_type_from_width (old_width) != val_type_from_width (new_width))
114     return false;
115   for (i = new_width; i < old_width; i++)
116     if (value->s[i] != ' ')
117       return false;
118   return true;
119 }
120
121 /* Resizes VALUE from OLD_WIDTH to NEW_WIDTH.  The arguments must
122    satisfy the rules specified above for value_is_resizable. */
123 void
124 value_resize (union value *value, int old_width, int new_width)
125 {
126   assert (value_is_resizable (value, old_width, new_width));
127   if (new_width > old_width)
128     memset (&value->s[old_width], ' ', new_width - old_width);
129 }