/* PSPP - computes sample statistics.
Copyright (C) 2004 Free Software Foundation, Inc.
- Written by Ben Pfaff <blp@gnu.org>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
#include <limits.h>
#include <stdlib.h>
#include "value.h"
-#include "alloc.h"
-#include "str.h"
+#include <libpspp/alloc.h>
+#include <libpspp/str.h>
#include "variable.h"
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
#undef NDEBUG
#else
#ifndef NDEBUG
{
struct case_data *cd;
- assert (c != NULL);
- assert (c->this == c);
- assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 1);
cd = c->case_data;
+ value_cnt * sizeof (union value));
}
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Initializes C as a null case. */
void
case_nullify (struct ccase *c)
{
c->case_data = NULL;
- c->this = c;
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Returns true iff C is a null case. */
int
case_is_null (const struct ccase *c)
{
return c->case_data == NULL;
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
/* Initializes C as a new case that can store VALUE_CNT values.
The values have indeterminate contents until explicitly
xalloc_die ();
}
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Initializes CLONE as a copy of ORIG. */
void
case_clone (struct ccase *clone, const struct ccase *orig)
{
- assert (orig != NULL);
- assert (orig->this == orig);
- assert (orig->case_data != NULL);
assert (orig->case_data->ref_cnt > 0);
- assert (clone != NULL);
if (clone != orig)
- {
- *clone = *orig;
- clone->this = clone;
- }
+ *clone = *orig;
orig->case_data->ref_cnt++;
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Replaces DST by SRC and nullifies SRC.
DST and SRC must be initialized cases at entry. */
void
case_move (struct ccase *dst, struct ccase *src)
{
- assert (src != NULL);
- assert (src->this == src);
- assert (src->case_data != NULL);
assert (src->case_data->ref_cnt > 0);
- assert (dst != NULL);
-
- *dst = *src;
- dst->this = dst;
- case_nullify (src);
+
+ if (dst != src)
+ {
+ *dst = *src;
+ case_nullify (src);
+ }
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Destroys case C. */
void
case_destroy (struct ccase *c)
struct case_data *cd;
assert (c != NULL);
- assert (c->this == c);
cd = c->case_data;
if (cd != NULL && --cd->ref_cnt == 0)
free (cd);
}
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
/* Resizes case C from OLD_CNT to NEW_CNT values. */
void
}
/* Attempts to create C as a new case that holds VALUE_CNT
- values. Returns nonzero if successful, zero if memory
+ values. Returns true if successful, false if memory
allocation failed. */
-int
+bool
case_try_create (struct ccase *c, size_t value_cnt)
{
c->case_data = malloc (case_size (value_cnt));
if (c->case_data != NULL)
{
-#ifdef GLOBAL_DEBUGGING
- c->this = c;
-#endif
c->case_data->value_cnt = value_cnt;
c->case_data->ref_cnt = 1;
- return 1;
- }
- else
- {
-#ifdef GLOBAL_DEBUGGING
- c->this = c;
-#endif
- return 0;
+ return true;
}
+
+ return false;
}
/* Tries to initialize CLONE as a copy of ORIG.
- Returns nonzero if successful, zero if memory allocation
+ Returns true if successful, false if memory allocation
failed. */
-int
+bool
case_try_clone (struct ccase *clone, const struct ccase *orig)
{
case_clone (clone, orig);
- return 1;
+ return true;
}
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Copies VALUE_CNT values from SRC (starting at SRC_IDX) to DST
(starting at DST_IDX). */
void
const struct ccase *src, size_t src_idx,
size_t value_cnt)
{
- assert (dst != NULL);
- assert (dst->this == dst);
- assert (dst->case_data != NULL);
assert (dst->case_data->ref_cnt > 0);
assert (dst_idx + value_cnt <= dst->case_data->value_cnt);
- assert (src != NULL);
- assert (src->this == src);
- assert (src->case_data != NULL);
assert (src->case_data->ref_cnt > 0);
- assert (src_idx + value_cnt <= dst->case_data->value_cnt);
+ assert (src_idx + value_cnt <= src->case_data->value_cnt);
- if (dst->case_data->ref_cnt > 1)
- case_unshare (dst);
if (dst->case_data != src->case_data || dst_idx != src_idx)
- memmove (dst->case_data->values + dst_idx,
- src->case_data->values + src_idx,
- sizeof *dst->case_data->values * value_cnt);
+ {
+ if (dst->case_data->ref_cnt > 1)
+ case_unshare (dst);
+ memmove (dst->case_data->values + dst_idx,
+ src->case_data->values + src_idx,
+ sizeof *dst->case_data->values * value_cnt);
+ }
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Copies case C to OUTPUT.
OUTPUT_SIZE is the number of `union values' in OUTPUT,
which must match the number of `union values' in C. */
case_to_values (const struct ccase *c, union value *output,
size_t output_size UNUSED)
{
- assert (c != NULL);
- assert (c->this == c);
- assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (output_size == c->case_data->value_cnt);
assert (output != NULL || output_size == 0);
memcpy (output, c->case_data->values,
c->case_data->value_cnt * sizeof *output);
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Copies INPUT into case C.
INPUT_SIZE is the number of `union values' in INPUT,
which must match the number of `union values' in C. */
size_t input_size UNUSED)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (input_size == c->case_data->value_cnt);
memcpy (c->case_data->values, input,
c->case_data->value_cnt * sizeof *input);
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Returns a pointer to the `union value' used for the
element of C numbered IDX.
The caller must not modify the returned data. */
const union value *
-case_data (const struct ccase *c, size_t idx)
+case_data_idx (const struct ccase *c, size_t idx)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (idx < c->case_data->value_cnt);
return &c->case_data->values[idx];
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Returns the numeric value of the `union value' in C numbered
IDX. */
double
-case_num (const struct ccase *c, size_t idx)
+case_num_idx (const struct ccase *c, size_t idx)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (idx < c->case_data->value_cnt);
return c->case_data->values[idx].f;
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Returns the string value of the `union value' in C numbered
IDX.
(Note that the value is not null-terminated.)
The caller must not modify the return value. */
const char *
-case_str (const struct ccase *c, size_t idx)
+case_str_idx (const struct ccase *c, size_t idx)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (idx < c->case_data->value_cnt);
return c->case_data->values[idx].s;
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
-#ifdef GLOBAL_DEBUGGING
+#ifdef DEBUGGING
/* Returns a pointer to the `union value' used for the
element of C numbered IDX.
The caller is allowed to modify the returned data. */
union value *
-case_data_rw (struct ccase *c, size_t idx)
+case_data_rw_idx (struct ccase *c, size_t idx)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
assert (idx < c->case_data->value_cnt);
case_unshare (c);
return &c->case_data->values[idx];
}
-#endif /* GLOBAL_DEBUGGING */
+#endif /* DEBUGGING */
/* Compares the values of the VAR_CNT variables in VP
in cases A and B and returns a strcmp()-type result. */
const struct variable *va = *vap;
const struct variable *vb = *vbp;
- assert (va->type == vb->type);
- assert (va->width == vb->width);
+ assert (var_get_width (va) == var_get_width (vb));
- if (va->width == 0)
+ if (var_get_width (va) == 0)
{
- double af = case_num (ca, va->fv);
- double bf = case_num (cb, vb->fv);
+ double af = case_num (ca, va);
+ double bf = case_num (cb, vb);
if (af != bf)
return af > bf ? 1 : -1;
}
else
{
- const char *as = case_str (ca, va->fv);
- const char *bs = case_str (cb, vb->fv);
- int cmp = memcmp (as, bs, va->width);
+ const char *as = case_str (ca, va);
+ const char *bs = case_str (cb, vb);
+ int cmp = memcmp (as, bs, var_get_width (va));
if (cmp != 0)
return cmp;
case_data_all (const struct ccase *c)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);
case_data_all_rw (struct ccase *c)
{
assert (c != NULL);
- assert (c->this == c);
assert (c->case_data != NULL);
assert (c->case_data->ref_cnt > 0);