work on pivot table and FREQUENCIES usage
[pspp] / src / output / pivot-table.c
diff --git a/src/output/pivot-table.c b/src/output/pivot-table.c
new file mode 100644 (file)
index 0000000..54af3f9
--- /dev/null
@@ -0,0 +1,166 @@
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2010 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "output/pivot-table.h"
+
+#include "data/case.h"
+#include "data/casereader.h"
+#include "math/sort.h"
+
+#include "gl/xalloc.h"
+
+void
+pivot_cell_init (struct pivot_cell *cell, struct pivot_value *value)
+{
+  cell->base = value;
+  cell->cmp = PIVOT_RAW;
+  cell->cmp_args[0] = cell->cmp_args[1] = cell->cmp_args[2] = NULL;
+  cell->label = NULL;
+  cell->format = F_8_0;
+}
+
+struct pivot_value *
+pivot_value_create (const struct variable *var,
+                    enum pivot_operator operator,
+                    int n_vars_horz,
+                    int n_vars_vert,
+                    enum pivot_value_include include)
+{
+  struct pivot_value *value = xmalloc (sizeof *value);
+  value->var = var;
+  value->operator = operator;
+  value->n_agg_vars[TABLE_HORZ] = n_vars_horz;
+  value->n_agg_vars[TABLE_VERT] = n_vars_vert;
+  value->include = include;
+  return value;
+}
+
+static struct ccase *
+keep_one (struct ccase *a, struct ccase *b, void *aux UNUSED)
+{
+  case_unref (b);
+  return a;
+}
+
+static void
+dump_projection (struct casereader *data, const struct subcase *split,
+                 const struct subcase *vars)
+{
+  struct subcase sc;
+  struct ccase *c;
+
+  subcase_clone (&sc, split);
+  subcase_concat_always (&sc, vars);
+
+  data = casereader_project (data, &sc);
+  data = sort_distinct_execute (data, &sc, keep_one, NULL, NULL);
+
+#if 0
+  for (; (c = casereader_read (data)) != NULL; case_unref (c))
+    {
+      const struct caseproto *proto = case_get_proto (c);
+      size_t i;
+
+      for (i = 0; i < caseproto_get_n_widths (proto); i++)
+        {
+          int width = caseproto_get_width (proto, i);
+
+          if (i > 0)
+            putchar (' ');
+
+          if (width == 0)
+            printf ("%8.2g", case_num_idx (c, i));
+          else
+            printf ("\"%.*s\"", width, case_str_idx (c, i));
+        }
+      printf ("\n");
+    }
+#endif
+
+  subcase_destroy (&sc);
+}
+
+static void
+dump_data (struct pivot_table *pt)
+{
+  struct casereader *data;
+  struct subcase sc;
+  struct ccase *c;
+
+  subcase_clone (&sc, &pt->split);
+  subcase_concat_always (&sc, &pt->dimensions[TABLE_HORZ]);
+  subcase_concat_always (&sc, &pt->dimensions[TABLE_VERT]);
+
+  data = sort_distinct_execute (casereader_clone (pt->data), &sc, keep_one, NULL, NULL);
+  for (; (c = casereader_read (data)) != NULL; case_unref (c))
+    {
+      const struct caseproto *proto = case_get_proto (c);
+      size_t i;
+
+      for (i = 0; i < caseproto_get_n_widths (proto); i++)
+        {
+          int width = caseproto_get_width (proto, i);
+
+          if (i > 0)
+            putchar (' ');
+
+          if (width == 0)
+            printf ("%8.2f", case_num_idx (c, i));
+          else
+            printf ("\"%.*s\"", width, case_str_idx (c, i));
+        }
+      printf ("\n");
+    }
+
+}
+
+void
+pivot_table_dump (struct pivot_table *pt)
+{
+  /* Strategy:
+
+     Determine row and column labels:
+
+       Project onto (splits, rows) and sort, discarding duplicates.
+
+       Project onto (splits, columns) and sort, discarding duplicates.
+
+     Sort data on (splits, rows, columns).
+
+     For each split group:
+
+       For each row value:
+
+         For each column value:
+
+           For each value in the pane:
+
+             Evaluate and print value.
+
+  */
+
+  dump_projection (casereader_clone (pt->data),
+                   &pt->split, &pt->dimensions[TABLE_HORZ]);
+  dump_projection (casereader_clone (pt->data),
+                   &pt->split, &pt->dimensions[TABLE_VERT]);
+
+  dump_data (pt);
+  
+
+  casereader_destroy (pt->data);
+}