/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 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
#include <stdlib.h>
-#include "category.h"
-#include "data-out.h"
-#include "format.h"
-#include "dictionary.h"
-#include "identifier.h"
-#include "missing-values.h"
-#include "value-labels.h"
-#include "vardict.h"
+#include <data/attributes.h>
+#include <data/category.h>
+#include <data/data-out.h>
+#include <data/format.h>
+#include <data/dictionary.h>
+#include <data/identifier.h>
+#include <data/missing-values.h>
+#include <data/value-labels.h>
+#include <data/vardict.h>
#include <libpspp/misc.h>
#include <libpspp/assertion.h>
vectors with binary entries, so any variable of type ALPHA will
have its values stored here. */
struct cat_vals *obs_vals;
+
+ /* Custom attributes. */
+ struct attrset attributes;
};
\f
/* Creates and returns a new variable with the given NAME and
v->aux = NULL;
v->aux_dtor = NULL;
v->obs_vals = NULL;
+ attrset_init (&v->attributes);
return v;
}
var_set_display_width (new_var, var_get_display_width (old_var));
var_set_alignment (new_var, var_get_alignment (old_var));
var_set_leave (new_var, var_get_leave (old_var));
+ var_set_attributes (new_var, var_get_attributes (old_var));
return new_var;
}
-/* Create a variable to be used for internal calculations only */
+/* Create a variable to be used for internal calculations only.
+ The variable is assigned a unique dictionary index and a case
+ index of CASE_IDX. */
struct variable *
var_create_internal (int case_idx)
{
struct variable *v = var_create ("$internal", 0);
-
struct vardict_info vdi;
+ static int counter = INT_MAX / 2;
vdi.dict = NULL;
- vdi.dict_index = 0;
vdi.case_index = case_idx;
+ vdi.dict_index = counter++;
+ if (counter == INT_MAX)
+ counter = INT_MAX / 2;
var_set_vardict (v, &vdi);
{
const struct variable *v = v_;
- return hsh_hash_case_string (v->name);
+ return hash_case_string (v->name, 0);
}
/* A hsh_compare_func that orders pointers to variables A and B
return strcasecmp (var_get_name (*a), var_get_name (*b));
}
+/* A hsh_compare_func that orders pointers to variables A and B
+ by their dictionary indexes. */
+int
+compare_var_ptrs_by_dict_index (const void *a_, const void *b_,
+ const void *aux UNUSED)
+{
+ struct variable *const *a = a_;
+ struct variable *const *b = b_;
+ size_t a_index = var_get_dict_index (*a);
+ size_t b_index = var_get_dict_index (*b);
+
+ return a_index < b_index ? -1 : a_index > b_index;
+}
+
/* A hsh_hash_func that hashes pointer to variable V based on its
name. */
unsigned
{
struct variable *const *v = v_;
- return hsh_hash_case_string (var_get_name (*v));
+ return hash_case_string (var_get_name (*v), 0);
}
\f
/* Returns the type of variable V. */
ds_put_cstr (str, name);
}
\f
-\f
/* Print and write formats. */
/* Returns V's print format specification. */
ss_truncate (&s, 255);
if (!ss_is_empty (s))
v->label = ss_xstrdup (s);
- dict_var_changed (v);
}
+ dict_var_changed (v);
}
/* Removes any variable label from V. */
/* Sets V's display width to DISPLAY_WIDTH. */
void
-var_set_display_width (struct variable *v, int display_width)
+var_set_display_width (struct variable *v, int new_width)
{
- v->display_width = display_width;
+ int old_width = v->display_width;
+
+ v->display_width = new_width;
+
+ if ( old_width != new_width)
+ dict_var_display_width_changed (v);
+
dict_var_changed (v);
}
return v->obs_vals != NULL;
}
\f
+/* Returns variable V's attribute set. The caller may examine or
+ modify the attribute set, but must not destroy it. Destroying
+ V, or calling var_set_attributes() on V, will also destroy its
+ attribute set. */
+struct attrset *
+var_get_attributes (const struct variable *v)
+{
+ return (struct attrset *) &v->attributes;
+}
+
+/* Replaces variable V's attributes set by a copy of ATTRS. */
+void
+var_set_attributes (struct variable *v, const struct attrset *attrs)
+{
+ attrset_destroy (&v->attributes);
+ attrset_clone (&v->attributes, attrs);
+}
+
+/* Returns true if V has any custom attributes, false if it has none. */
+bool
+var_has_attributes (const struct variable *v)
+{
+ return attrset_count (&v->attributes) > 0;
+}
+\f
/* Returns V's vardict structure. */
const struct vardict_info *
var_get_vardict (const struct variable *v)