nested totals
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 4 Jan 2022 06:55:15 +0000 (22:55 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 2 Apr 2022 01:48:55 +0000 (18:48 -0700)
src/language/stats/ctables.c

index ce635b15b8eaf67864c54c2befc24a357eafa66f..24924a35783a87389ce1a1f0a07181f832f0e35c 100644 (file)
@@ -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