Improve VECTOR implementation.
[pspp-builds.git] / src / data / dictionary.c
index 24f518b2681e2014d74174cc8c2632d84176b6e7..ca1a286c4d135ad20345fabe11484242bac3d9af 100644 (file)
@@ -155,17 +155,17 @@ dict_clear (struct dictionary *d)
 {
   /* FIXME?  Should we really clear case_limit, label, documents?
      Others are necessarily cleared by deleting all the variables.*/
-  int i;
-
   assert (d != NULL);
 
-  for (i = 0; i < d->var_cnt; i++)
+  while (d->var_cnt > 0 )
     {
-      if (d->callbacks &&  d->callbacks->var_deleted )
-       d->callbacks->var_deleted (d, i, d->cb_data);
+      var_clear_vardict (d->var[d->var_cnt - 1]);
+      var_destroy (d->var[d->var_cnt -1]);
 
-      var_clear_vardict (d->var[i]);
-      var_destroy (d->var[i]);
+      d->var_cnt--;
+
+      if (d->callbacks &&  d->callbacks->var_deleted )
+       d->callbacks->var_deleted (d, d->var_cnt, d->cb_data);
     }
   free (d->var);
   d->var = NULL;
@@ -291,7 +291,7 @@ add_var (struct dictionary *d, struct variable *v)
   hsh_force_insert (d->name_tab, v);
 
   if ( d->callbacks &&  d->callbacks->var_added )
-    d->callbacks->var_added (d, d->next_value_idx, d->cb_data);
+    d->callbacks->var_added (d, var_get_dict_index (v), d->cb_data);
 
   d->next_value_idx += var_get_value_cnt (v);
 
@@ -352,9 +352,16 @@ dict_clone_var_assert (struct dictionary *d, const struct variable *old_var,
 struct variable *
 dict_lookup_var (const struct dictionary *d, const char *name)
 {
-  struct variable *target = var_create (name, 0);
-  struct variable *result = hsh_find (d->name_tab, target);
+  struct variable *target ;
+  struct variable *result ;
+
+  if ( ! var_is_valid_name (name, false))
+    return NULL;
+
+  target = var_create (name, 0);
+  result = hsh_find (d->name_tab, target);
   var_destroy (target);
+
   return result;
 }
 
@@ -1130,6 +1137,18 @@ dict_create_vector (struct dictionary *d,
     return false;
 }
 
+/* Creates in D a vector named NAME that contains the CNT
+   variables in VAR.  A vector named NAME must not already exist
+   in D. */
+void
+dict_create_vector_assert (struct dictionary *d,
+                           const char *name,
+                           struct variable **var, size_t cnt)
+{
+  assert (dict_lookup_vector (d, name) == NULL);
+  dict_create_vector (d, name, var, cnt);
+}
+
 /* Returns the vector in D with index IDX, which must be less
    than dict_get_vector_cnt (D). */
 const struct vector *