Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / output / table-transpose.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "libpspp/assertion.h"
20 #include "libpspp/cast.h"
21 #include "output/table-provider.h"
22
23 #include "gl/minmax.h"
24 #include "gl/xalloc.h"
25
26 struct table_transpose
27   {
28     struct table table;
29     struct table *subtable;
30   };
31
32 static const struct table_class table_transpose_class;
33
34 static struct table_transpose *
35 table_transpose_cast (const struct table *table)
36 {
37   assert (table->class == &table_transpose_class);
38   return UP_CAST (table, struct table_transpose, table);
39 }
40
41 /* Takes ownership of SUBTABLE and returns a new table whose contents are
42    SUBTABLE with rows and columns transposed. */
43 struct table *
44 table_transpose (struct table *subtable)
45 {
46   if (subtable->n[TABLE_HORZ] == subtable->n[TABLE_VERT]
47       && subtable->n[TABLE_HORZ] <= 1)
48     return subtable;
49   else if (subtable->class == &table_transpose_class)
50     {
51       struct table_transpose *tt = table_transpose_cast (subtable);
52       struct table *table = table_ref (tt->subtable);
53       table_unref (subtable);
54       return table;
55     }
56   else
57     {
58       struct table_transpose *tt;
59       int axis;
60
61       tt = xmalloc (sizeof *tt);
62       table_init (&tt->table, &table_transpose_class);
63       tt->subtable = subtable;
64
65       for (axis = 0; axis < TABLE_N_AXES; axis++)
66         {
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];
70         }
71       return &tt->table;
72     }
73 }
74
75 static void
76 table_transpose_destroy (struct table *ti)
77 {
78   struct table_transpose *tt = table_transpose_cast (ti);
79   table_unref (tt->subtable);
80   free (tt);
81 }
82
83 static void
84 swap (int *x, int *y)
85 {
86   int t = *x;
87   *x = *y;
88   *y = t;
89 }
90
91 static void
92 table_transpose_get_cell (const struct table *ti, int x, int y,
93                           struct table_cell *cell)
94 {
95   struct table_transpose *tt = table_transpose_cast (ti);
96   int i;
97
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]);
101 }
102
103 static int
104 table_transpose_get_rule (const struct table *ti,
105                           enum table_axis axis,
106                           int x, int y)
107 {
108   struct table_transpose *tt = table_transpose_cast (ti);
109   return table_get_rule (tt->subtable, !axis, y, x);
110 }
111
112 static const struct table_class table_transpose_class =
113   {
114     table_transpose_destroy,
115     table_transpose_get_cell,
116     table_transpose_get_rule,
117     NULL,                       /* paste */
118     NULL,                       /* select */
119   };