(dump_aggregate_info) Properly test whether the destination variable
[pspp] / src / dictionary.c
index f1c5f462f3a437170d216822c9de865ccf24f25b..99ea5b8bcbec501a1cd3c57fa3252351b7d0fb17 100644 (file)
@@ -56,7 +56,7 @@ dict_create (void)
   
   d->var = NULL;
   d->var_cnt = d->var_cap = 0;
-  d->name_tab = hsh_create (8, compare_variables, hash_variable, NULL, NULL);
+  d->name_tab = hsh_create (8, compare_var_names, hash_var_name, NULL, NULL);
   d->next_value_idx = 0;
   d->split = NULL;
   d->split_cnt = 0;
@@ -392,17 +392,12 @@ dict_contains_var (const struct dictionary *d, const struct variable *v)
 /* Compares two double pointers to variables, which should point
    to elements of a struct dictionary's `var' member array. */
 static int
-compare_variable_dblptrs (const void *a_, const void *b_, void *aux UNUSED) 
+compare_var_ptrs (const void *a_, const void *b_, void *aux UNUSED) 
 {
   struct variable *const *a = a_;
   struct variable *const *b = b_;
 
-  if (a > b)
-    return 1;
-  else if (a < b)
-    return -1;
-  else
-    return 0;
+  return *a < *b ? -1 : *a > *b;
 }
 
 /* Deletes variable V from dictionary D and frees V.
@@ -432,8 +427,7 @@ dict_delete_var (struct dictionary *d, struct variable *v)
 
   /* Remove V from splits, weight, filter variables. */
   d->split_cnt = remove_equal (d->split, d->split_cnt, sizeof *d->split,
-                               &v,
-                               compare_variable_dblptrs, NULL);
+                               &v, compare_var_ptrs, NULL);
   if (d->weight == v)
     d->weight = NULL;
   if (d->filter == v)
@@ -441,9 +435,8 @@ dict_delete_var (struct dictionary *d, struct variable *v)
   dict_clear_vectors (d);
 
   /* Remove V from var array. */
+  remove_element (d->var, d->var_cnt, sizeof *d->var, v->index);
   d->var_cnt--;
-  memmove (d->var + v->index, d->var + v->index + 1,
-           (d->var_cnt - v->index) * sizeof *d->var);
 
   /* Update index. */
   for (i = v->index; i < d->var_cnt; i++)
@@ -707,6 +700,30 @@ dict_compact_values (struct dictionary *d)
     }
 }
 
+/* Copies values from SRC, which represents a case arranged
+   according to dictionary D, to DST, which represents a case
+   arranged according to the dictionary that will be produced by
+   dict_compact_values(D). */
+void
+dict_compact_case (const struct dictionary *d,
+                   struct ccase *dst, const struct ccase *src)
+{
+  size_t i;
+  size_t value_idx;
+
+  value_idx = 0;
+  for (i = 0; i < d->var_cnt; i++) 
+    {
+      struct variable *v = d->var[i];
+
+      if (dict_class_from_id (v->name) != DC_SCRATCH)
+        {
+          case_copy (dst, value_idx, src, v->fv, v->nv);
+          value_idx += v->nv;
+        }
+    }
+}
+
 /* Returns the number of values that would be used by a case if
    dict_compact_values() were called. */
 size_t