figure out frequency table structure
authorBen Pfaff <blp@cs.stanford.edu>
Wed, 29 Dec 2021 19:57:06 +0000 (11:57 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 1 Jan 2022 19:17:32 +0000 (11:17 -0800)
src/language/stats/ctables.c

index 624331ba5d108429fcd220aca672eef66de6be65..d71754d01e88c1974dbb62abb9766a4a184422b1 100644 (file)
@@ -26,6 +26,7 @@
 #include "libpspp/assertion.h"
 #include "libpspp/hmap.h"
 #include "libpspp/message.h"
+#include "libpspp/string-array.h"
 #include "output/pivot-table.h"
 
 #include "gl/minmax.h"
@@ -1247,6 +1248,77 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
   return true;
 }
 
+struct ctables_freqtab
+  {
+    struct variable **vars;
+    size_t n_vars;
+
+    struct hmap data;           /* Contains "struct freq"s. */
+  };
+
+static struct string_array
+nest_fts (struct string_array sa0, struct string_array sa1)
+{
+  if (!sa0.n)
+    return sa1;
+  else if (!sa1.n)
+    return sa0;
+
+  struct string_array sa = STRING_ARRAY_INITIALIZER;
+  for (size_t i = 0; i < sa0.n; i++)
+    for (size_t j = 0; j < sa1.n; j++)
+      string_array_append_nocopy (&sa, xasprintf ("%s, %s",
+                                                  sa0.strings[i],
+                                                  sa1.strings[j]));
+  string_array_destroy (&sa0);
+  string_array_destroy (&sa1);
+  return sa;
+}
+
+static struct string_array
+enumerate_fts (const struct ctables_axis *a)
+{
+  struct string_array sa = STRING_ARRAY_INITIALIZER;
+  if (!a)
+    return sa;
+
+  switch (a->op)
+    {
+    case CTAO_VAR:
+      string_array_append (&sa, ctables_var_name (&a->var));
+      break;
+
+    case CTAO_STACK:
+      sa = enumerate_fts (a->subs[0]);
+      struct string_array sa2 = enumerate_fts (a->subs[1]);
+      for (size_t i = 0; i < sa2.n; i++)
+        string_array_append_nocopy (&sa, sa2.strings[i]);
+      free (sa2.strings);
+      break;
+
+    case CTAO_NEST:
+      return nest_fts (enumerate_fts (a->subs[0]),
+                       enumerate_fts (a->subs[1]));
+    }
+  return sa;
+}
+
+static void
+ctables_execute (struct ctables *ct)
+{
+  for (size_t i = 0; i < ct->n_tables; i++)
+    {
+      struct ctables_table *t = &ct->tables[i];
+      struct string_array sa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
+      sa = nest_fts (sa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
+      sa = nest_fts (sa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
+      for (size_t i = 0; i < sa.n; i++)
+        puts (sa.strings[i]);
+      putc ('\n', stdout);
+      string_array_destroy (&sa);
+    }
+}
+
 int
 cmd_ctables (struct lexer *lexer, struct dataset *ds)
 {
@@ -1860,6 +1932,8 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds)
 
     }
   while (lex_token (lexer) != T_ENDCMD);
+
+  ctables_execute (ct);
   ctables_destroy (ct);
   return CMD_SUCCESS;