From 24fee9d98d7ef2241ee6b565543df3545b5f56e1 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 3 Jan 2022 22:55:15 -0800 Subject: [PATCH] nested totals --- src/language/stats/ctables.c | 54 ++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/language/stats/ctables.c b/src/language/stats/ctables.c index ce635b15b8..24924a3578 100644 --- a/src/language/stats/ctables.c +++ b/src/language/stats/ctables.c @@ -2207,6 +2207,38 @@ summarize: f->domains[dt]->valid += weight; } +static void +recurse_totals (struct ctables_table *t, const struct ccase *c, + size_t ix[PIVOT_N_AXES], + const struct ctables_category *cats[PIVOT_N_AXES][10], + double weight, + enum pivot_axis_type start_a, size_t start_va) +{ + for (enum pivot_axis_type a = start_a; a < PIVOT_N_AXES; a++) + { + const struct var_array *va = &t->vaas[a].vas[ix[a]]; + for (size_t i = start_va; i < va->n; i++) + { + if (i == va->scale_idx) + continue; + + const struct variable *var = va->vars[i]; + + const struct ctables_category *total = ctables_categories_total ( + t->categories[var_get_dict_index (var)]); + if (total) + { + const struct ctables_category *save = cats[a][i]; + cats[a][i] = total; + ctables_cell_insert__ (t, c, ix, cats, weight); + recurse_totals (t, c, ix, cats, weight, a, i + 1); + cats[a][i] = save; + } + } + start_va = 0; + } +} + static void ctables_cell_insert (struct ctables_table *t, const struct ccase *c, @@ -2243,27 +2275,7 @@ ctables_cell_insert (struct ctables_table *t, ctables_cell_insert__ (t, c, ix, cats, weight); - for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++) - { - const struct var_array *va = &t->vaas[a].vas[ix[a]]; - for (size_t i = 0; i < va->n; i++) - { - if (i == va->scale_idx) - continue; - - const struct variable *var = va->vars[i]; - - const struct ctables_category *total = ctables_categories_total ( - t->categories[var_get_dict_index (var)]); - if (total) - { - const struct ctables_category *save = cats[a][i]; - cats[a][i] = total; - ctables_cell_insert__ (t, c, ix, cats, weight); - cats[a][i] = save; - } - } - } + recurse_totals (t, c, ix, cats, weight, 0, 0); } static bool -- 2.30.2