X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fvalue.h;h=8e624a8464b73163e9638c88c26dff5fa6a444cd;hb=051724c1769c04a715f00f22f75c4a810f5bff11;hp=8f1746337ea37ceaefa3523fa73ff0c2666b63f8;hpb=5c3291dc396b795696e94f47780308fd7ace6fc4;p=pspp diff --git a/src/data/value.h b/src/data/value.h index 8f1746337e..8e624a8464 100644 --- a/src/data/value.h +++ b/src/data/value.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000, 2007, 2009 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2012 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 @@ -17,65 +17,53 @@ #ifndef DATA_VALUE_H #define DATA_VALUE_H 1 +#include "libpspp/compiler.h" #include #include #include +#include #include #include "xalloc.h" -#define MAX_SHORT_STRING 8 -#define MIN_LONG_STRING (MAX_SHORT_STRING + 1) - -/* A numeric or string value. - - The client is responsible for keeping track of the value's - width. - - This structure is semi-opaque: - - - If the value is a number, clients may access the 'f' - member directly. - - - Clients should not access other members directly. -*/ +/* A numeric or string value. The client is responsible for keeping track of + the value's width. */ union value { double f; - char short_string[MAX_SHORT_STRING]; - char *long_string; + uint8_t *s; }; static inline void value_init (union value *, int width); +static inline void value_clone (union value *, const union value *, int width); static inline bool value_needs_init (int width); static inline bool value_try_init (union value *, int width); static inline void value_destroy (union value *, int width); -static inline double value_num (const union value *); -static inline const char *value_str (const union value *, int width); -static inline char *value_str_rw (union value *, int width); - -int compare_values (const void *, const void *, const void *var); -unsigned hash_value (const void *, const void *var); - static inline void value_copy (union value *, const union value *, int width); void value_copy_rpad (union value *, int dst_width, const union value *, int src_width, char pad); -void value_copy_str_rpad (union value *, int dst_width, const char *, +void value_copy_str_rpad (union value *, int dst_width, const uint8_t *, char pad); void value_copy_buf_rpad (union value *dst, int dst_width, - const char *src, size_t src_len, char pad); + const uint8_t *src, size_t src_len, char pad); void value_set_missing (union value *, int width); int value_compare_3way (const union value *, const union value *, int width); bool value_equal (const union value *, const union value *, int width); -size_t value_hash (const union value *, int width, unsigned int basis); +unsigned int value_hash (const union value *, int width, unsigned int basis) WARN_UNUSED_RESULT; bool value_is_resizable (const union value *, int old_width, int new_width); bool value_needs_resize (int old_width, int new_width); void value_resize (union value *, int old_width, int new_width); +bool value_is_spaces (const union value *, int width); + +static inline void value_swap (union value *, union value *); + struct pool; void value_init_pool (struct pool *, union value *, int width); +void value_clone_pool (struct pool *, union value *, const union value *, + int width); void value_resize_pool (struct pool *, union value *, int old_width, int new_width); @@ -90,8 +78,19 @@ void value_resize_pool (struct pool *, union value *, static inline void value_init (union value *v, int width) { - if (width > MAX_SHORT_STRING) - v->long_string = xmalloc (width); + if (width > 0) + v->s = xmalloc (width); +} + +/* Initializes V as a value of the given WIDTH, as with value_init(), and + copies SRC's value into V as its initial value. */ +static inline void +value_clone (union value *v, const union value *src, int width) +{ + if (width <= 0) + v->f = src->f; + else + v->s = xmemdup (src->s, width); } /* Returns true if a value of the given WIDTH actually needs to @@ -103,7 +102,7 @@ value_init (union value *v, int width) static inline bool value_needs_init (int width) { - return width > MAX_SHORT_STRING; + return width > 0; } /* Same as value_init, except that failure to allocate memory @@ -112,10 +111,10 @@ value_needs_init (int width) static inline bool value_try_init (union value *v, int width) { - if (width > MAX_SHORT_STRING) + if (width > 0) { - v->long_string = malloc (width); - return v->long_string != NULL; + v->s = malloc (width); + return v->s != NULL; } else return true; @@ -126,43 +125,8 @@ value_try_init (union value *v, int width) static inline void value_destroy (union value *v, int width) { - if (width > MAX_SHORT_STRING) - free (v->long_string); -} - -/* Returns the numeric value in V, which must have width 0. */ -static inline double -value_num (const union value *v) -{ - return v->f; -} - -/* Returns the string value in V, which must have width WIDTH. - - The returned value is not null-terminated. - - It is important that WIDTH be the actual value that was passed - to value_init. Passing, e.g., a smaller value because only - that number of bytes will be accessed will not always work. */ -static inline const char * -value_str (const union value *v, int width) -{ - assert (width > 0); - return (width >= MIN_LONG_STRING ? v->long_string : v->short_string); -} - -/* Returns the string value in V, which must have width WIDTH. - - The returned value is not null-terminated. - - It is important that WIDTH be the actual value that was passed - to value_init. Passing, e.g., a smaller value because only - that number of bytes will be accessed will not always work. */ -static inline char * -value_str_rw (union value *v, int width) -{ - assert (width > 0); - return (width >= MIN_LONG_STRING ? v->long_string : v->short_string); + if (width > 0) + free (v->s); } /* Copies SRC to DST, given that they both contain data of the @@ -170,10 +134,19 @@ value_str_rw (union value *v, int width) static inline void value_copy (union value *dst, const union value *src, int width) { - if (width <= MAX_SHORT_STRING) - *dst = *src; - else if (dst != src) - memcpy (dst->long_string, src->long_string, width); + if (width <= 0) + dst->f = src->f; + else + memcpy (dst->s, src->s, width); +} + +/* Exchanges the contents of A and B. */ +static inline void +value_swap (union value *a, union value *b) +{ + union value tmp = *a; + *a = *b; + *b = tmp; } #endif /* data/value.h */