From 039238c6fd5cc2310e0d6b40358f29347cfcbce7 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 3 Mar 2023 18:45:00 -0800 Subject: [PATCH] dictionary: Get rid of "internal var" concept. It's nasty and doesn't conform to the usual invariants. --- src/data/dictionary.c | 49 --------------------------------------- src/data/dictionary.h | 4 ---- src/language/commands/aggregate.c | 11 +++++---- src/language/commands/wilcoxon.c | 20 +++++++--------- src/language/commands/wilcoxon.h | 3 +++ 5 files changed, 17 insertions(+), 70 deletions(-) diff --git a/src/data/dictionary.c b/src/data/dictionary.c index 23a07ae58..71c6e8136 100644 --- a/src/data/dictionary.c +++ b/src/data/dictionary.c @@ -1929,55 +1929,6 @@ dict_var_changed (const struct variable *v, unsigned int what, struct variable * -/* 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; - } - } -} - int vardict_get_dict_index (const struct vardict_info *vardict) { diff --git a/src/data/dictionary.h b/src/data/dictionary.h index 092a2b23e..cec706b35 100644 --- a/src/data/dictionary.h +++ b/src/data/dictionary.h @@ -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 { diff --git a/src/language/commands/aggregate.c b/src/language/commands/aggregate.c index fedfbaf5f..8afa5f92e 100644 --- a/src/language/commands/aggregate.c +++ b/src/language/commands/aggregate.c @@ -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); diff --git a/src/language/commands/wilcoxon.c b/src/language/commands/wilcoxon.c index 8e3fd8193..363d86510 100644 --- a/src/language/commands/wilcoxon.c +++ b/src/language/commands/wilcoxon.c @@ -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); } diff --git a/src/language/commands/wilcoxon.h b/src/language/commands/wilcoxon.h index 529cf5656..a155b9e94 100644 --- a/src/language/commands/wilcoxon.h +++ b/src/language/commands/wilcoxon.h @@ -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; -- 2.11.4.GIT