move code around
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 15 Jan 2022 20:11:23 +0000 (12:11 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 2 Apr 2022 01:48:55 +0000 (18:48 -0700)
src/language/stats/ctables.c

index 7a1cb3b2ca08bc7149a0315e28c673dfe5e0a2d2..8ced195ee30efec3233531da82eb0220ff71949b 100644 (file)
@@ -2779,165 +2779,165 @@ ctables_table_output_different_axis (struct ctables *ct, struct ctables_table *t
 }
 
 
-static bool
-ctables_execute (struct dataset *ds, struct ctables *ct)
+static void
+ctables_prepare_table (struct ctables_table *t)
 {
-  for (size_t i = 0; i < ct->n_tables; i++)
-    {
-      struct ctables_table *t = ct->tables[i];
-      for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
-        if (t->axes[a])
-          {
-            t->stacks[a] = enumerate_fts (a, t->axes[a]);
+  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+    if (t->axes[a])
+      {
+        t->stacks[a] = enumerate_fts (a, t->axes[a]);
 
-            for (size_t j = 0; j < t->stacks[a].n; j++)
+        for (size_t j = 0; j < t->stacks[a].n; j++)
+          {
+            struct ctables_nest *nest = &t->stacks[a].nests[j];
+            for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
               {
-                struct ctables_nest *nest = &t->stacks[a].nests[j];
-                for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+                nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
+                nest->n_domains[dt] = 0;
+
+                for (size_t k = 0; k < nest->n; k++)
                   {
-                    nest->domains[dt] = xmalloc (nest->n * sizeof *nest->domains[dt]);
-                    nest->n_domains[dt] = 0;
+                    if (k == nest->scale_idx)
+                      continue;
 
-                    for (size_t k = 0; k < nest->n; k++)
+                    switch (dt)
                       {
-                        if (k == nest->scale_idx)
-                          continue;
+                      case CTDT_TABLE:
+                        continue;
 
-                        switch (dt)
+                      case CTDT_LAYER:
+                        if (a != PIVOT_AXIS_LAYER)
+                          continue;
+                        break;
+
+                      case CTDT_SUBTABLE:
+                      case CTDT_ROW:
+                      case CTDT_COL:
+                        if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
+                            : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
+                            : a == PIVOT_AXIS_ROW)
                           {
-                          case CTDT_TABLE:
-                            continue;
-
-                          case CTDT_LAYER:
-                            if (a != PIVOT_AXIS_LAYER)
-                              continue;
-                            break;
-
-                          case CTDT_SUBTABLE:
-                          case CTDT_ROW:
-                          case CTDT_COL:
-                            if (dt == CTDT_SUBTABLE ? a != PIVOT_AXIS_LAYER
-                                : dt == CTDT_ROW ? a == PIVOT_AXIS_COLUMN
-                                : a == PIVOT_AXIS_ROW)
-                              {
-                                if (k == nest->n - 1
-                                    || (nest->scale_idx == nest->n - 1
-                                        && k == nest->n - 2))
-                                  continue;
-                              }
-                            break;
-
-                          case CTDT_LAYERROW:
-                            if (a == PIVOT_AXIS_COLUMN)
-                              continue;
-                            break;
-
-                          case CTDT_LAYERCOL:
-                            if (a == PIVOT_AXIS_ROW)
+                            if (k == nest->n - 1
+                                || (nest->scale_idx == nest->n - 1
+                                    && k == nest->n - 2))
                               continue;
-                            break;
                           }
+                        break;
 
-                        nest->domains[dt][nest->n_domains[dt]++] = k;
+                      case CTDT_LAYERROW:
+                        if (a == PIVOT_AXIS_COLUMN)
+                          continue;
+                        break;
+
+                      case CTDT_LAYERCOL:
+                        if (a == PIVOT_AXIS_ROW)
+                          continue;
+                        break;
                       }
+
+                    nest->domains[dt][nest->n_domains[dt]++] = k;
                   }
               }
           }
-        else
-          {
-            struct ctables_nest *nest = xmalloc (sizeof *nest);
-            *nest = (struct ctables_nest) { .n = 0 };
-            t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
-          }
+      }
+    else
+      {
+        struct ctables_nest *nest = xmalloc (sizeof *nest);
+        *nest = (struct ctables_nest) { .n = 0 };
+        t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 };
+      }
 
-      struct ctables_stack *stack = &t->stacks[t->summary_axis];
-      for (size_t i = 0; i < stack->n; i++)
+  struct ctables_stack *stack = &t->stacks[t->summary_axis];
+  for (size_t i = 0; i < stack->n; i++)
+    {
+      struct ctables_nest *nest = &stack->nests[i];
+      if (!nest->specs[CSV_CELL].n)
         {
-          struct ctables_nest *nest = &stack->nests[i];
-          if (!nest->specs[CSV_CELL].n)
-            {
-              struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
-              specs->specs = xmalloc (sizeof *specs->specs);
-              specs->n = 1;
-
-              enum ctables_summary_function function
-                = specs->var ? CTSF_MEAN : CTSF_COUNT;
-              struct ctables_var var = { .is_mrset = false, .var = specs->var };
-
-              *specs->specs = (struct ctables_summary_spec) {
-                .function = function,
-                .format = ctables_summary_default_format (function, &var),
-                .label = ctables_summary_default_label (function, 0),
-              };
-              if (!specs->var)
-                specs->var = nest->vars[0];
+          struct ctables_summary_spec_set *specs = &nest->specs[CSV_CELL];
+          specs->specs = xmalloc (sizeof *specs->specs);
+          specs->n = 1;
+
+          enum ctables_summary_function function
+            = specs->var ? CTSF_MEAN : CTSF_COUNT;
+          struct ctables_var var = { .is_mrset = false, .var = specs->var };
+
+          *specs->specs = (struct ctables_summary_spec) {
+            .function = function,
+            .format = ctables_summary_default_format (function, &var),
+            .label = ctables_summary_default_label (function, 0),
+          };
+          if (!specs->var)
+            specs->var = nest->vars[0];
 
-              ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
-                                              &nest->specs[CSV_CELL]);
-            }
-          else if (!nest->specs[CSV_TOTAL].n)
-            ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
-                                            &nest->specs[CSV_CELL]);
+          ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
+                                          &nest->specs[CSV_CELL]);
         }
+      else if (!nest->specs[CSV_TOTAL].n)
+        ctables_summary_spec_set_clone (&nest->specs[CSV_TOTAL],
+                                        &nest->specs[CSV_CELL]);
+    }
 
-      struct ctables_summary_spec_set *merged = &t->summary_specs;
-      struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
-      size_t n_left = 0;
-      for (size_t j = 0; j < stack->n; j++)
-        {
-          const struct ctables_nest *nest = &stack->nests[j];
-          if (nest->n)
-            for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
-              items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
-        }
+  struct ctables_summary_spec_set *merged = &t->summary_specs;
+  struct merge_item *items = xnmalloc (2 * stack->n, sizeof *items);
+  size_t n_left = 0;
+  for (size_t j = 0; j < stack->n; j++)
+    {
+      const struct ctables_nest *nest = &stack->nests[j];
+      if (nest->n)
+        for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
+          items[n_left++] = (struct merge_item) { .set = &nest->specs[sv] };
+    }
 
-      while (n_left > 0)
-        {
-          struct merge_item min = items[0];
-          for (size_t j = 1; j < n_left; j++)
-            if (merge_item_compare_3way (&items[j], &min) < 0)
-              min = items[j];
+  while (n_left > 0)
+    {
+      struct merge_item min = items[0];
+      for (size_t j = 1; j < n_left; j++)
+        if (merge_item_compare_3way (&items[j], &min) < 0)
+          min = items[j];
 
-          if (merged->n >= merged->allocated)
-            merged->specs = x2nrealloc (merged->specs, &merged->allocated,
-                                        sizeof *merged->specs);
-          merged->specs[merged->n++] = min.set->specs[min.ofs];
+      if (merged->n >= merged->allocated)
+        merged->specs = x2nrealloc (merged->specs, &merged->allocated,
+                                    sizeof *merged->specs);
+      merged->specs[merged->n++] = min.set->specs[min.ofs];
 
-          for (size_t j = 0; j < n_left; )
+      for (size_t j = 0; j < n_left; )
+        {
+          if (merge_item_compare_3way (&items[j], &min) == 0)
             {
-              if (merge_item_compare_3way (&items[j], &min) == 0)
+              struct merge_item *item = &items[j];
+              item->set->specs[item->ofs].axis_idx = merged->n - 1;
+              if (++item->ofs >= item->set->n)
                 {
-                  struct merge_item *item = &items[j];
-                  item->set->specs[item->ofs].axis_idx = merged->n - 1;
-                  if (++item->ofs >= item->set->n)
-                    {
-                      items[j] = items[--n_left];
-                      continue;
-                    }
+                  items[j] = items[--n_left];
+                  continue;
                 }
-              j++;
             }
+          j++;
         }
+    }
 
 #if 0
-      for (size_t j = 0; j < merged->n; j++)
-        printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
+  for (size_t j = 0; j < merged->n; j++)
+    printf ("%s\n", ctables_summary_function_name (merged->specs[j].function));
 
-      for (size_t j = 0; j < stack->n; j++)
+  for (size_t j = 0; j < stack->n; j++)
+    {
+      const struct ctables_nest *nest = &stack->nests[j];
+      for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
         {
-          const struct ctables_nest *nest = &stack->nests[j];
-          for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
-            {
-              const struct ctables_summary_spec_set *specs = &nest->specs[sv];
-              for (size_t k = 0; k < specs->n; k++)
-                printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
-                        specs->specs[k].axis_idx);
-              printf ("\n");
-            }
+          const struct ctables_summary_spec_set *specs = &nest->specs[sv];
+          for (size_t k = 0; k < specs->n; k++)
+            printf ("(%s, %zu) ", ctables_summary_function_name (specs->specs[k].function),
+                    specs->specs[k].axis_idx);
+          printf ("\n");
         }
-#endif
     }
+#endif
+}
 
+static bool
+ctables_execute (struct dataset *ds, struct ctables *ct)
+{
   struct casereader *input = casereader_create_filter_weight (proc_open (ds),
                                                               dataset_dict (ds),
                                                               NULL, NULL);
@@ -3643,6 +3643,9 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds)
           goto error;
         }
 
+      ctables_prepare_table (t);
+
+      
     }
   while (lex_token (lexer) != T_ENDCMD);