X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fvalue.h;h=8e624a8464b73163e9638c88c26dff5fa6a444cd;hb=8642df034e82487ec7ff476fbfbacbd5b65eb88a;hp=118e6cd815fe012665392eeabc0b704560b52529;hpb=d22c3971e926ceaf62416c6482fe0fb1dc5407f0;p=pspp diff --git a/src/data/value.h b/src/data/value.h index 118e6cd815..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 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 @@ -14,51 +14,139 @@ You should have received a copy of the GNU General Public License 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 "minmax.h" -#include +#include "libpspp/compiler.h" +#include +#include +#include +#include +#include +#include "xalloc.h" + +/* A numeric or string value. The client is responsible for keeping track of + the value's width. */ +union value + { + double f; + 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 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 uint8_t *, + char pad); +void value_copy_buf_rpad (union value *dst, int dst_width, + 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); +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); -/* Values. */ +bool value_is_spaces (const union value *, int width); -/* "Short" strings, which are generally those no more than 8 - characters wide, can participate in more operations than - longer strings. */ -#define MAX_SHORT_STRING (MAX (ROUND_UP (SIZEOF_DOUBLE, 2), 8)) -#define MIN_LONG_STRING (MAX_SHORT_STRING + 1) -#define MAX_STRING 32767 +static inline void value_swap (union value *, union value *); -/* Special values. */ -#define SYSMIS (-DBL_MAX) -#define LOWEST second_lowest_value -#define HIGHEST DBL_MAX +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); + +/* 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. -/* Number of "union value"s required for a variable of the given - WIDTH. */ -static inline size_t -value_cnt_from_width (int width) + 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) { - return width == 0 ? 1 : DIV_RND_UP (width, MAX_SHORT_STRING); + if (width > 0) + v->s = xmalloc (width); } -/* A numeric or short string value. - Multiple consecutive values represent a long string. */ -union value - { - double f; - char s[MAX_SHORT_STRING]; - }; +/* 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); +} -union value *value_dup (const union value *, int width); -union value *value_create (int 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. -int compare_values (const union value *, const union value *, int width); -unsigned hash_value (const union value *, int 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 > 0; +} -void value_copy (union value *, const union value *, int width); -void value_set_missing (union value *, int width); +/* 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 > 0) + { + v->s = malloc (width); + return v->s != 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 > 0) + free (v->s); +} + +/* 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 <= 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 /* !value.h */ +#endif /* data/value.h */