Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / data / value.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include "value.h"
21
22 #include <libpspp/hash.h>
23 #include <libpspp/str.h>
24
25 #include "xalloc.h"
26
27 /* Duplicate a value.
28    The caller is responsible for freeing the returned value. */
29 union value *
30 value_dup (const union value *val, int width)
31 {
32   return xmemdup (val, MAX (width, sizeof *val));
33 }
34
35 /* Compares A and B, which both have the given WIDTH, and returns
36    a strcmp()-type result.
37    Only the short string portion of longer strings are
38    compared. */
39 int
40 compare_values (const union value *a, const union value *b, int width) 
41 {
42   return (width == 0
43           ? (a->f < b->f ? -1 : a->f > b->f)
44           : memcmp (a->s, b->s, MIN (MAX_SHORT_STRING, width)));
45 }
46
47 /* Create a hash of V, which has the given WIDTH.
48    Only the short string portion of a longer string is hashed. */
49 unsigned 
50 hash_value (const union value *v, int width)
51 {
52   return (width == 0
53           ? hsh_hash_double (v->f)
54           : hsh_hash_bytes (v->s, MIN (MAX_SHORT_STRING, width)));
55 }