1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "libpspp/assertion.h"
20 #include "libpspp/cast.h"
21 #include "output/table-provider.h"
23 #include "gl/minmax.h"
24 #include "gl/xalloc.h"
26 struct table_transpose
29 struct table *subtable;
32 static const struct table_class table_transpose_class;
34 static struct table_transpose *
35 table_transpose_cast (const struct table *table)
37 assert (table->klass == &table_transpose_class);
38 return UP_CAST (table, struct table_transpose, table);
41 /* Takes ownership of SUBTABLE and returns a new table whose contents are
42 SUBTABLE with rows and columns transposed. */
44 table_transpose (struct table *subtable)
46 if (subtable->n[TABLE_HORZ] == subtable->n[TABLE_VERT]
47 && subtable->n[TABLE_HORZ] <= 1)
49 else if (subtable->klass == &table_transpose_class)
51 struct table_transpose *tt = table_transpose_cast (subtable);
52 struct table *table = table_ref (tt->subtable);
53 table_unref (subtable);
58 struct table_transpose *tt;
61 tt = xmalloc (sizeof *tt);
62 table_init (&tt->table, &table_transpose_class);
63 tt->subtable = subtable;
65 for (axis = 0; axis < TABLE_N_AXES; axis++)
67 tt->table.n[axis] = subtable->n[!axis];
68 tt->table.h[axis][0] = subtable->h[!axis][0];
69 tt->table.h[axis][1] = subtable->h[!axis][1];
76 table_transpose_destroy (struct table *ti)
78 struct table_transpose *tt = table_transpose_cast (ti);
79 table_unref (tt->subtable);
92 table_transpose_get_cell (const struct table *ti, int x, int y,
93 struct table_cell *cell)
95 struct table_transpose *tt = table_transpose_cast (ti);
98 table_get_cell (tt->subtable, y, x, cell);
99 for (i = 0; i < 2; i++)
100 swap (&cell->d[TABLE_HORZ][i], &cell->d[TABLE_VERT][i]);
104 table_transpose_get_rule (const struct table *ti,
105 enum table_axis axis,
108 struct table_transpose *tt = table_transpose_cast (ti);
109 return table_get_rule (tt->subtable, !axis, y, x);
112 static const struct table_class table_transpose_class =
114 table_transpose_destroy,
115 table_transpose_get_cell,
116 table_transpose_get_rule,