dictionary: Get rid of "internal var" concept.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 4 Mar 2023 02:45:00 +0000 (18:45 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 5 Mar 2023 19:23:16 +0000 (11:23 -0800)
It's nasty and doesn't conform to the usual invariants.

src/data/dictionary.c
src/data/dictionary.h
src/language/commands/aggregate.c
src/language/commands/wilcoxon.c
src/language/commands/wilcoxon.h

index 23a07ae5863fbeecda6e804c1b97487787b5e982..71c6e8136e7fca2b419d5580961d32a82304e9d9 100644 (file)
@@ -1928,55 +1928,6 @@ dict_var_changed (const struct variable *v, unsigned int what, struct variable *
 }
 
 
-\f
-/* Dictionary used to contain "internal variables". */
-static struct dictionary *internal_dict;
-
-/* Create a variable of the specified WIDTH to be used for internal
-   calculations only.  The variable is assigned case index CASE_IDX. */
-struct variable *
-dict_create_internal_var (int case_idx, int width)
-{
-  if (internal_dict == NULL)
-    internal_dict = dict_create ("UTF-8");
-
-  for (;;)
-    {
-      static int counter = INT_MAX / 2;
-      struct variable *var;
-      char name[64];
-
-      if (++counter == INT_MAX)
-        counter = INT_MAX / 2;
-
-      sprintf (name, "$internal%d", counter);
-      var = dict_create_var (internal_dict, name, width);
-      if (var != NULL)
-        {
-          set_var_case_index (var, case_idx);
-          return var;
-        }
-    }
-}
-
-/* Destroys VAR, which must have been created with
-   dict_create_internal_var(). */
-void
-dict_destroy_internal_var (struct variable *var)
-{
-  if (var != NULL)
-    {
-      dict_delete_var (internal_dict, var);
-
-      /* Destroy internal_dict if it has no variables left, just so that
-         valgrind --leak-check --show-reachable won't show internal_dict. */
-      if (dict_get_n_vars (internal_dict) == 0)
-        {
-          dict_unref (internal_dict);
-          internal_dict = NULL;
-        }
-    }
-}
 \f
 int
 vardict_get_dict_index (const struct vardict_info *vardict)
index 092a2b23e805baeac5c6494be8904a180498b1cd..cec706b35503aa631d4a237ea13a848e59d505df 100644 (file)
@@ -201,10 +201,6 @@ char *dict_id_is_valid__ (const struct dictionary *, const char *id)
   WARN_UNUSED_RESULT;
 bool dict_id_is_valid (const struct dictionary *, const char *id);
 
-/* Internal variables. */
-struct variable *dict_create_internal_var (int case_idx, int width);
-void dict_destroy_internal_var (struct variable *);
-
 /* Functions to be called upon dictionary changes. */
 struct dict_callbacks
  {
index fedfbaf5fc67772d07b56de9c699b114089b4ade..8afa5f92e60deb031b4cee4603d9cf261da0b491 100644 (file)
@@ -85,6 +85,7 @@ struct agr_var
     bool saw_missing;
     struct moments1 *moments;
 
+    struct dictionary *dict;
     struct variable *subject;
     struct variable *weight;
     struct casewriter *writer;
@@ -693,8 +694,7 @@ agr_destroy (struct agr_proc *agr)
       if (av->function == AGRF_SD)
         moments1_destroy (av->moments);
 
-      dict_destroy_internal_var (av->subject);
-      dict_destroy_internal_var (av->weight);
+      dict_unref (av->dict);
     }
   free (agr->agr_vars);
   if (agr->dict != NULL)
@@ -1086,11 +1086,12 @@ initialize_aggregate_info (struct agr_proc *agr)
             proto = caseproto_add_width (proto, 0);
             proto = caseproto_add_width (proto, 0);
 
+            if (!av->dict)
+              av->dict = dict_create ("UTF-8");
            if (! av->subject)
-             av->subject = dict_create_internal_var (0, 0);
-
+             av->subject = dict_create_var (av->dict, "subject", 0);
            if (! av->weight)
-             av->weight = dict_create_internal_var (1, 0);
+             av->weight = dict_create_var (av->dict, "weight", 0);
 
             struct subcase ordering;
             subcase_init_var (&ordering, av->subject, SC_ASCEND);
index 8e3fd8193ff0cf1ab34b2a6db64357a8e6680a84..363d86510c49b4b4be20a15c9b42242da06a5875 100644 (file)
@@ -87,7 +87,6 @@ wilcoxon_execute (const struct dataset *ds,
 
   struct wilcoxon_state *ws = XCALLOC (t2s->n_pairs,  struct wilcoxon_state);
   const struct variable *weight = dict_get_weight (dict);
-  struct variable *weightx = dict_create_internal_var (WEIGHT_IDX, 0);
   struct caseproto *proto;
 
   input =
@@ -107,8 +106,10 @@ wilcoxon_execute (const struct dataset *ds,
       struct subcase ordering;
       variable_pair *vp = &t2s->pairs[i];
 
-      ws[i].sign = dict_create_internal_var (0, 0);
-      ws[i].absdiff = dict_create_internal_var (1, 0);
+      ws[i].dict = dict_create ("UTF-8");
+      ws[i].sign = dict_create_var (ws[i].dict, "sign", 0);
+      ws[i].absdiff = dict_create_var (ws[i].dict, "absdiff", 0);
+      ws[i].weight = dict_create_var (ws[i].dict, "weight", 0);
 
       r = casereader_create_filter_missing (r, *vp, 2,
                                            exclude,
@@ -142,7 +143,7 @@ wilcoxon_execute (const struct dataset *ds,
          *case_num_rw (output, ws[i].absdiff) = fabs (d);
 
          if (weight)
-          *case_num_rw (output, weightx) = case_num (c, weight);
+          *case_num_rw (output, ws[i].weight) = case_num (c, weight);
 
          casewriter_write (writer, output);
        }
@@ -158,7 +159,7 @@ wilcoxon_execute (const struct dataset *ds,
       enum rank_error err = 0;
 
       rr = casereader_create_append_rank (ws[i].reader, ws[i].absdiff,
-                                         weight ? weightx : NULL, &err,
+                                         weight ? ws[i].weight : NULL, &err,
                                          distinct_callback, &ws[i]
                                        );
 
@@ -166,7 +167,7 @@ wilcoxon_execute (const struct dataset *ds,
        {
          double sign = case_num (c, ws[i].sign);
          double rank = case_num_idx (c, weight ? 3 : 2);
-         double w = weight ? case_num (c, weightx) : 1.0;
+         double w = weight ? case_num (c, ws[i].weight) : 1.0;
 
          if (sign > 0)
            {
@@ -187,16 +188,11 @@ wilcoxon_execute (const struct dataset *ds,
 
   casereader_destroy (input);
 
-  dict_destroy_internal_var (weightx);
-
   show_ranks_box (ws, t2s, dict);
   show_tests_box (ws, t2s, exact, timer);
 
   for (i = 0 ; i < t2s->n_pairs; ++i)
-    {
-      dict_destroy_internal_var (ws[i].sign);
-      dict_destroy_internal_var (ws[i].absdiff);
-    }
+    dict_unref (ws[i].dict);
 
   free (ws);
 }
index 529cf5656271aa43cb6b6f0a6a0086e4f134a491..a155b9e946921341feef66da8903bfb7dc625a53 100644 (file)
@@ -32,8 +32,11 @@ struct rank_sum
 struct wilcoxon_state
 {
   struct casereader *reader;
+
+  struct dictionary *dict;
   struct variable *sign;
   struct variable *absdiff;
+  struct variable *weight;
 
   struct rank_sum positives;
   struct rank_sum negatives;