Categorical value cache added
[pspp-builds.git] / src / dictionary.c
index 0fd59e1172113cd1f8754b82c3b08a48d5072943..f916fd0c3fa10219c2600279de622ac2ff721bce 100644 (file)
@@ -24,6 +24,7 @@
 #include "algorithm.h"
 #include "alloc.h"
 #include "case.h"
+#include "cat.h"
 #include "error.h"
 #include "hash.h"
 #include "misc.h"
@@ -32,6 +33,9 @@
 #include "value-labels.h"
 #include "var.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 /* A dictionary. */
 struct dictionary
   {
@@ -97,7 +101,7 @@ dict_clone (const struct dictionary *s)
   d->split_cnt = s->split_cnt;
   if (d->split_cnt > 0) 
     {
-      d->split = xmalloc (d->split_cnt * sizeof *d->split);
+      d->split = xnmalloc (d->split_cnt, sizeof *d->split);
       for (i = 0; i < d->split_cnt; i++) 
         d->split[i] = dict_lookup_var_assert (d, s->split[i]->name);
     }
@@ -113,7 +117,7 @@ dict_clone (const struct dictionary *s)
   dict_set_documents (d, dict_get_documents (s));
 
   d->vector_cnt = s->vector_cnt;
-  d->vector = xmalloc (d->vector_cnt * sizeof *d->vector);
+  d->vector = xnmalloc (d->vector_cnt, sizeof *d->vector);
   for (i = 0; i < s->vector_cnt; i++) 
     {
       struct vector *sv = s->vector[i];
@@ -123,7 +127,7 @@ dict_clone (const struct dictionary *s)
       dv->idx = i;
       strcpy (dv->name, sv->name);
       dv->cnt = sv->cnt;
-      dv->var = xmalloc (dv->cnt * sizeof *dv->var);
+      dv->var = xnmalloc (dv->cnt, sizeof *dv->var);
       for (j = 0; j < dv->cnt; j++)
         dv->var[j] = d->var[sv->var[j]->index];
     }
@@ -237,7 +241,7 @@ dict_get_vars (const struct dictionary *d, struct variable ***vars,
     if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
       count++;
 
-  *vars = xmalloc (count * sizeof **vars);
+  *vars = xnmalloc (count, sizeof **vars);
   *cnt = 0;
   for (i = 0; i < d->var_cnt; i++)
     if (!(exclude_classes & (1u << dict_class_from_id (d->var[i]->name))))
@@ -257,10 +261,9 @@ dict_create_var (struct dictionary *d, const char *name, int width)
   assert (d != NULL);
   assert (name != NULL);
 
-  assert (strlen (name) >= 1);
-  assert (strlen (name) <= LONG_NAME_LEN);
-
   assert (width >= 0 && width < 256);
+
+  assert (var_is_valid_name(name,0));
     
   /* Make sure there's not already a variable by that name. */
   if (dict_lookup_var (d, name) != NULL)
@@ -276,23 +279,17 @@ dict_create_var (struct dictionary *d, const char *name, int width)
   v->init = 1;
   v->reinit = dict_class_from_id (v->name) != DC_SCRATCH;
   v->index = d->var_cnt;
-  v->miss_type = MISSING_NONE;
+  mv_init (&v->miss, width);
   if (v->type == NUMERIC)
     {
-      v->print.type = FMT_F;
-      v->print.w = 8;
-      v->print.d = 2;
-
+      v->print = f8_2;
       v->alignment = ALIGN_RIGHT;
       v->display_width = 8;
       v->measure = MEASURE_SCALE;
     }
   else
     {
-      v->print.type = FMT_A;
-      v->print.w = v->width;
-      v->print.d = 0;
-
+      v->print = make_output_format (FMT_A, v->width, 0);
       v->alignment = ALIGN_LEFT;
       v->display_width = 8;
       v->measure = MEASURE_NOMINAL;
@@ -303,12 +300,13 @@ dict_create_var (struct dictionary *d, const char *name, int width)
   var_clear_short_name (v);
   v->aux = NULL;
   v->aux_dtor = NULL;
+  v->obs_vals = NULL;
 
   /* Update dictionary. */
   if (d->var_cnt >= d->var_cap) 
     {
       d->var_cap = 8 + 2 * d->var_cap; 
-      d->var = xrealloc (d->var, d->var_cap * sizeof *d->var);
+      d->var = xnrealloc (d->var, d->var_cap, sizeof *d->var);
     }
   d->var[v->index] = v;
   d->var_cnt++;
@@ -357,8 +355,7 @@ dict_clone_var (struct dictionary *d, const struct variable *ov,
      the same short name. */
   nv->init = 1;
   nv->reinit = ov->reinit;
-  nv->miss_type = ov->miss_type;
-  memcpy (nv->missing, ov->missing, sizeof nv->missing);
+  mv_copy (&nv->miss, &ov->miss);
   nv->print = ov->print;
   nv->write = ov->write;
   val_labs_destroy (nv->val_labs);
@@ -476,6 +473,7 @@ dict_delete_var (struct dictionary *d, struct variable *v)
 
   /* Free memory. */
   val_labs_destroy (v->val_labs);
+  cat_stored_values_destroy (v);
   free (v->label);
   free (v);
 }
@@ -495,6 +493,23 @@ dict_delete_vars (struct dictionary *d,
     dict_delete_var (d, *vars++);
 }
 
+/* Deletes scratch variables from dictionary D. */
+void
+dict_delete_scratch_vars (struct dictionary *d)
+{
+  int i;
+
+  /* FIXME: this can be done in O(count) time, but this algorithm
+     is O(count**2). */
+  assert (d != NULL);
+
+  for (i = 0; i < d->var_cnt; )
+    if (dict_class_from_id (d->var[i]->name) == DC_SCRATCH)
+      dict_delete_var (d, d->var[i]);
+    else
+      i++;
+}
+
 /* Moves V to 0-based position IDX in D.  Other variables in D,
    if any, retain their relative positions.  Runs in time linear
    in the distance moved. */
@@ -533,7 +548,7 @@ dict_reorder_vars (struct dictionary *d,
   assert (count == 0 || order != NULL);
   assert (count <= d->var_cnt);
 
-  new_var = xmalloc (d->var_cnt * sizeof *new_var);
+  new_var = xnmalloc (d->var_cnt, sizeof *new_var);
   memcpy (new_var, order, count * sizeof *new_var);
   for (i = 0; i < count; i++) 
     {
@@ -597,7 +612,7 @@ dict_rename_vars (struct dictionary *d,
 
   /* Remove the variables to be renamed from the name hash,
      save their names, and rename them. */
-  old_names = xmalloc (count * sizeof *old_names);
+  old_names = xnmalloc (count, sizeof *old_names);
   for (i = 0; i < count; i++) 
     {
       assert (d->var[vars[i]->index] == vars[i]);
@@ -681,7 +696,7 @@ dict_get_case_weight (const struct dictionary *d, const struct ccase *c,
   else 
     {
       double w = case_num (c, d->weight->fv);
-      if ( w < 0.0 || w == SYSMIS || is_num_user_missing(w, d->weight) )
+      if (w < 0.0 || mv_is_num_missing (&d->weight->miss, w))
         w = 0.0;
       if ( w == 0.0 && *warn_on_invalid ) {
          *warn_on_invalid = 0;
@@ -842,7 +857,7 @@ dict_get_compacted_idx_to_fv (const struct dictionary *d)
   size_t next_value_idx;
   int *idx_to_fv;
   
-  idx_to_fv = xmalloc (d->var_cnt * sizeof *idx_to_fv);
+  idx_to_fv = xnmalloc (d->var_cnt, sizeof *idx_to_fv);
   next_value_idx = 0;
   for (i = 0; i < d->var_cnt; i++)
     {
@@ -889,7 +904,7 @@ dict_set_split_vars (struct dictionary *d,
   assert (cnt == 0 || split != NULL);
 
   d->split_cnt = cnt;
-  d->split = xrealloc (d->split, cnt * sizeof *d->split);
+  d->split = xnrealloc (d->split, cnt, sizeof *d->split);
   memcpy (d->split, split, cnt * sizeof *d->split);
 }
 
@@ -967,11 +982,11 @@ dict_create_vector (struct dictionary *d,
   if (dict_lookup_vector (d, name) != NULL)
     return 0;
 
-  d->vector = xrealloc (d->vector, (d->vector_cnt + 1) * sizeof *d->vector);
+  d->vector = xnrealloc (d->vector, d->vector_cnt + 1, sizeof *d->vector);
   vector = d->vector[d->vector_cnt] = xmalloc (sizeof *vector);
   vector->idx = d->vector_cnt++;
   str_copy_trunc (vector->name, sizeof vector->name, name);
-  vector->var = xmalloc (cnt * sizeof *var);
+  vector->var = xnmalloc (cnt, sizeof *var);
   for (i = 0; i < cnt; i++)
     {
       assert (dict_contains_var (d, var[i]));