X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fvalue.c;h=180f1b6d1e064d8195fda7f033bf72ecb3cccfd6;hb=e8b19bfb4e94185514f3e22d92da41cb4581b115;hp=e9411c8cefa976ff4a4c46d94db837e9eefffb23;hpb=43b1296aafe7582e7dbe6c2b6a8b478d7d9b0fcf;p=pspp-builds.git
diff --git a/src/data/value.c b/src/data/value.c
index e9411c8c..180f1b6d 100644
--- a/src/data/value.c
+++ b/src/data/value.c
@@ -1,5 +1,5 @@
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -15,10 +15,12 @@
along with this program. If not, see . */
#include
-#include "value.h"
+#include
+#include
#include
#include
+#include "variable.h"
#include "xalloc.h"
@@ -30,28 +32,43 @@ value_dup (const union value *val, int width)
return xmemdup (val, MAX (width, sizeof *val));
}
+
+/* Create a value of specified width.
+ The caller is responsible for freeing the returned value. */
+union value *
+value_create (int width)
+{
+ return xnmalloc (value_cnt_from_width (width), sizeof (union value));
+}
+
+
/* Compares A and B, which both have the given WIDTH, and returns
a strcmp()-type result.
Only the short string portion of longer strings are
compared. */
int
-compare_values (const union value *a, const union value *b, int width)
+compare_values_short (const void *a_, const void *b_, const void *var_)
{
- return (width == 0
- ? (a->f < b->f ? -1 : a->f > b->f)
- : memcmp (a->s, b->s, MIN (MAX_SHORT_STRING, width)));
+ const union value *a = a_;
+ const union value *b = b_;
+ const struct variable *var = var_;
+ int width = var_get_width (var);
+ return value_compare_3way (a, b, MIN (width, MAX_SHORT_STRING));
}
+
/* Create a hash of V, which has the given WIDTH.
Only the short string portion of a longer string is hashed. */
unsigned
-hash_value (const union value *v, int width)
+hash_value_short (const void *v_, const void *var_)
{
- return (width == 0
- ? hsh_hash_double (v->f)
- : hsh_hash_bytes (v->s, MIN (MAX_SHORT_STRING, width)));
+ const union value *v = v_;
+ const struct variable *var = var_;
+ int width = var_get_width (var);
+ return width == 0 ? hash_double (v->f, 0) : hash_bytes (v->s, width, 0);
}
+
/* Copies SRC to DST, given that they both contain data of the
given WIDTH. */
void
@@ -73,3 +90,42 @@ value_set_missing (union value *v, int width)
else
memset (v->s, ' ', width);
}
+
+/* Tests whether VALUE may be resized from OLD_WIDTH to
+ NEW_WIDTH, using the following rules that match those for
+ resizing missing values and value labels. First, OLD_WIDTH
+ and NEW_WIDTH must be both numeric or both string. Second, if
+ NEW_WIDTH is less than OLD_WIDTH, then the bytes that would be
+ trimmed off the right end of VALUE must be all spaces. */
+bool
+value_is_resizable (const union value *value, int old_width, int new_width)
+{
+ int i;
+
+ if (val_type_from_width (old_width) != val_type_from_width (new_width))
+ return false;
+ for (i = new_width; i < old_width; i++)
+ if (value->s[i] != ' ')
+ return false;
+ return true;
+}
+
+/* Resizes VALUE from OLD_WIDTH to NEW_WIDTH. The arguments must
+ satisfy the rules specified above for value_is_resizable. */
+void
+value_resize (union value *value, int old_width, int new_width)
+{
+ assert (value_is_resizable (value, old_width, new_width));
+ if (new_width > old_width)
+ memset (&value->s[old_width], ' ', new_width - old_width);
+}
+
+/* Compares A and B, which both have the given WIDTH, and returns
+ a strcmp()-type result. */
+int
+value_compare_3way (const union value *a, const union value *b, int width)
+{
+ return (width == 0
+ ? (a->f < b->f ? -1 : a->f > b->f)
+ : memcmp (a->s, b->s, width));
+}