Rewrite PSPP output engine.
[pspp-builds.git] / src / output / table-select.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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_select
27   {
28     struct table table;
29     struct table *subtable;
30     int ofs[2];
31   };
32
33 static const struct table_class table_select_class;
34
35 static struct table_select *
36 table_select_cast (const struct table *table)
37 {
38   assert (table->class == &table_select_class);
39   return UP_CAST (table, struct table_select, table);
40 }
41
42 /* Takes ownership of SUBTABLE and returns a new table whose contents are the
43    rectangular subregion of SUBTABLE that contains rows RECT[TABLE_VERT][0]
44    through RECT[TABLE_VERT][1], exclusive, and columns RECT[TABLE_HORZ][0]
45    through RECT[TABLE_HORZ][1]. */
46 struct table *
47 table_select (struct table *subtable, int rect[TABLE_N_AXES][2])
48 {
49   struct table_select *ts;
50   int axis;
51
52   if (rect[TABLE_HORZ][0] == 0
53       && rect[TABLE_HORZ][1] == subtable->n[TABLE_HORZ]
54       && rect[TABLE_VERT][0] == 0
55       && rect[TABLE_VERT][1] == subtable->n[TABLE_VERT])
56     return subtable;
57
58   if (!table_is_shared (subtable) && subtable->class->select != NULL)
59     {
60       struct table *selected = subtable->class->select (subtable, rect);
61       if (selected != NULL)
62         return selected;
63     }
64
65   ts = xmalloc (sizeof *ts);
66   table_init (&ts->table, &table_select_class);
67   ts->subtable = subtable;
68   for (axis = 0; axis < TABLE_N_AXES; axis++)
69     {
70       int h1;
71       ts->ofs[axis] = rect[axis][0];
72       ts->table.n[axis] = rect[axis][1] - rect[axis][0];
73       if (subtable->h[axis][0] > rect[axis][0])
74         ts->table.h[axis][0] = subtable->h[axis][0] - rect[axis][0];
75       h1 = subtable->n[axis] - subtable->h[axis][1];
76       if (h1 < rect[axis][1])
77         ts->table.h[axis][1] = rect[axis][1] - h1;
78     }
79   return &ts->table;
80 }
81
82 /* Takes ownership of TABLE and returns a new table whose contents are:
83
84         - If AXIS is TABLE_HORZ, columns Z0 through Z1 (exclusive) of SUBTABLE.
85           If ADD_HEADERS is true, the returned table also includes any header
86           columns in SUBTABLE.
87
88         - If AXIS is TABLE_VERT, rows Z0 through Z1 (exclusive) of SUBTABLE.
89           If ADD_HEADERS is true, the returned table also includes any header
90           rows in SUBTABLE. */
91 struct table *
92 table_select_slice (struct table *subtable, enum table_axis axis,
93                     int z0, int z1, bool add_headers)
94 {
95   struct table *table;
96   int rect[TABLE_N_AXES][2];
97
98   if (add_headers)
99     {
100       if (z0 == subtable->h[axis][0] && z1 == subtable->h[axis][1])
101         return subtable;
102
103       if (subtable->h[axis][0])
104         table_ref (subtable);
105       if (subtable->h[axis][1])
106         table_ref (subtable);
107     }
108   else
109     {
110       if (z0 == 0 && z1 == subtable->n[axis])
111         return subtable;
112     }
113
114   rect[TABLE_HORZ][0] = 0;
115   rect[TABLE_VERT][0] = 0;
116   rect[TABLE_HORZ][1] = subtable->n[TABLE_HORZ];
117   rect[TABLE_VERT][1] = subtable->n[TABLE_VERT];
118   rect[axis][0] = z0;
119   rect[axis][1] = z1;
120   table = table_select (subtable, rect);
121
122   if (add_headers)
123     {
124       if (subtable->h[axis][0])
125         table = table_paste (
126           table_select_slice (subtable, axis, 0, subtable->h[axis][0],
127                               false),
128           table, axis);
129
130       if (subtable->h[axis][1])
131         table = table_paste (
132           table,
133           table_select_slice (subtable, axis,
134                               subtable->n[axis] - subtable->h[axis][1],
135                               subtable->n[axis], false),
136           axis);
137     }
138
139   return table;
140 }
141
142 /* Takes ownership of TABLE and returns a new table whose contents are columns
143    X0 through X1 (exclusive) of SUBTABLE.  If ADD_HEADERS is true, the
144    returned table also includes any header columns in SUBTABLE. */
145 struct table *
146 table_select_columns (struct table *subtable, int x0, int x1,
147                       bool add_headers)
148 {
149   return table_select_slice (subtable, TABLE_HORZ, x0, x1, add_headers);
150 }
151
152 /* Takes ownership of TABLE and returns a new table whose contents are rows Y0
153    through Y1 (exclusive) of SUBTABLE.  If ADD_HEADERS is true, the returned
154    table also includes any header rows in SUBTABLE. */
155 struct table *
156 table_select_rows (struct table *subtable, int y0, int y1,
157                    bool add_headers)
158 {
159   return table_select_slice (subtable, TABLE_VERT, y0, y1, add_headers);
160 }
161
162 static void
163 table_select_destroy (struct table *ti)
164 {
165   struct table_select *ts = table_select_cast (ti);
166   table_unref (ts->subtable);
167   free (ts);
168 }
169
170 static void
171 table_select_get_cell (const struct table *ti, int x, int y,
172                        struct table_cell *cell)
173 {
174   struct table_select *ts = table_select_cast (ti);
175   int axis;
176
177   table_get_cell (ts->subtable,
178                   x + ts->ofs[TABLE_HORZ],
179                   y + ts->ofs[TABLE_VERT], cell);
180
181   for (axis = 0; axis < TABLE_N_AXES; axis++)
182     {
183       int *d = cell->d[axis];
184       int ofs = ts->ofs[axis];
185
186       d[0] = MAX (d[0] - ofs, 0);
187       d[1] = MIN (d[1] - ofs, ti->n[axis]);
188     }
189 }
190
191 static int
192 table_select_get_rule (const struct table *ti,
193                        enum table_axis axis,
194                        int x, int y)
195 {
196   struct table_select *ts = table_select_cast (ti);
197   return table_get_rule (ts->subtable, axis,
198                          x + ts->ofs[TABLE_HORZ],
199                          y + ts->ofs[TABLE_VERT]);
200 }
201
202 static struct table *
203 table_select_select (struct table *ti, int rect[TABLE_N_AXES][2])
204 {
205   struct table_select *ts = table_select_cast (ti);
206   int axis;
207
208   for (axis = 0; axis < TABLE_N_AXES; axis++)
209     {
210       int h1;
211
212       if (ts->table.h[axis][0] > rect[axis][0])
213         ts->table.h[axis][0] = ts->table.h[axis][0] - rect[axis][0];
214       else
215         ts->table.h[axis][0] = 0;
216
217       h1 = ts->table.n[axis] - ts->table.h[axis][1];
218       if (h1 < rect[axis][1])
219         ts->table.h[axis][1] = rect[axis][1] - h1;
220       else
221         ts->table.h[axis][1] = 0;
222
223       ts->ofs[axis] += rect[axis][0];
224       ts->table.n[axis] = rect[axis][1] - rect[axis][0];
225     }
226   return ti;
227 }
228
229 static const struct table_class table_select_class =
230   {
231     table_select_destroy,
232     table_select_get_cell,
233     table_select_get_rule,
234     NULL,                       /* paste */
235     table_select_select,
236   };