X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fvalue.h;h=6955eea0bd358e144fc1bd4ff8ed9e615aa7b68f;hb=a34c42168e0cd3373860a6cc3f518cb0e329db47;hp=1c2a19cdb8977d3bbf911bd1d7289978a25bced8;hpb=dcf9b154cbcaa35c3d8459a201b77eec8bcb30bd;p=pspp-builds.git diff --git a/src/data/value.h b/src/data/value.h index 1c2a19cd..6955eea0 100644 --- a/src/data/value.h +++ b/src/data/value.h @@ -1,78 +1,191 @@ -/* PSPP - computes sample statistics. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . +/* PSPP - a program for statistical analysis. + Copyright (C) 1997-9, 2000, 2007, 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 the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ -#if !value_h -#define value_h 1 +#ifndef DATA_VALUE_H +#define DATA_VALUE_H 1 -#include +#include +#include +#include +#include +#include "xalloc.h" + +/* Maximum length of a "short" string, that is represented in + "union value" without a separate pointer. -#include + This is an implementation detail of the "union value" code. + There is little reason for client code to use it. */ +#define MAX_SHORT_STRING 8 -/* Values. */ +/* A numeric or string value. -/* Max length of a short string value, generally 8 chars. */ -#define MAX_SHORT_STRING ((SIZEOF_DOUBLE)>=8 ? ((SIZEOF_DOUBLE)+1)/2*2 : 8) -#define MIN_LONG_STRING (MAX_SHORT_STRING+1) + The client is responsible for keeping track of the value's + width. -/* Max string length. */ -#define MAX_STRING 255 + This structure is semi-opaque: -/* FYI: It is a bad situation if sizeof(flt64) < MAX_SHORT_STRING: - then short string missing values can be truncated in system files - because there's only room for as many characters as can fit in a - flt64. */ -#if MAX_SHORT_STRING > SHORT_NAME_LEN -#error MAX_SHORT_STRING must be less than or equal to SHORT_NAME_LEN. -#endif + - If the value is a number, clients may access the 'f' + member directly. -/* Special values. */ -#define SYSMIS (-DBL_MAX) -#define LOWEST second_lowest_value -#define HIGHEST DBL_MAX - -/* Describes one value, which is either a floating-point number or a - short string. */ + - Clients should not access other members directly. +*/ union value { - /* A numeric value. */ double f; - - /* A short-string value. */ - char s[MAX_SHORT_STRING]; - - /* Used by evaluate_expression() to return a string result. - As currently implemented, it's a pointer to a dynamic - buffer in the appropriate expression. - - Also used by the AGGREGATE procedure in handling string - values. */ - char *c; + char short_string[MAX_SHORT_STRING]; + char *long_string; }; -/* Maximum number of `union value's in a single number or string - value. */ -#define MAX_ELEMS_PER_VALUE (MAX_STRING / sizeof (union value) + 1) - -int compare_values (const union value *a, const union value *b, int width); - -unsigned hash_value(const union value *v, int width); - - - -#endif /* !value.h */ +static inline void value_init (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); + +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 *, + char pad); +void value_copy_buf_rpad (union value *dst, int dst_width, + const char *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); +unsigned int value_hash (const union value *, int width, unsigned int basis); + +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); + +static inline void value_swap (union value *, union value *); + +struct pool; +void value_init_pool (struct pool *, union value *, int width); +void value_resize_pool (struct pool *, union value *, + int old_width, int new_width); + +/* Initializes V as a value of the given WIDTH, where 0 + represents a numeric value and a positive integer represents a + string value WIDTH bytes long. + + A WIDTH of -1 is ignored. + + The contents of value V are indeterminate after + initialization. */ +static inline void +value_init (union value *v, int width) +{ + if (width > MAX_SHORT_STRING) + v->long_string = xmalloc (width); +} + +/* Returns true if a value of the given WIDTH actually needs to + have the value_init and value_destroy functions called, false + if those functions are no-ops for values of the given WIDTH. + + Using this function is only a valuable optimization if a large + number of values of the given WIDTH are to be initialized*/ +static inline bool +value_needs_init (int width) +{ + return width > MAX_SHORT_STRING; +} + +/* Same as value_init, except that failure to allocate memory + causes it to return false instead of terminating the + program. On success, returns true. */ +static inline bool +value_try_init (union value *v, int width) +{ + if (width > MAX_SHORT_STRING) + { + v->long_string = malloc (width); + return v->long_string != NULL; + } + else + return true; +} + +/* Frees any memory allocated by value_init for V, which must + have the given 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 > MAX_SHORT_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 > MAX_SHORT_STRING ? v->long_string : v->short_string); +} + +/* Copies SRC to DST, given that they both contain data of the + given 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); +} + +/* 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 */