table-select: Only cut headers into two if needed in table_select_slice().
[pspp] / src / output / table-select.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011, 2014 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->klass == &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->klass->select != NULL)
59     {
60       struct table *selected = subtable->klass->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   bool h0, h1;
98
99   h0 = add_headers && subtable->h[axis][0] > 0;
100   if (h0 && z0 == subtable->h[axis][0])
101     {
102       z0 = 0;
103       h0 = false;
104     }
105
106   h1 = add_headers && subtable->h[axis][1] > 0;
107   if (h1 && z1 == subtable->n[axis] - subtable->h[axis][1])
108     {
109       z1 = subtable->n[axis];
110       h1 = false;
111     }
112
113   if (z0 == 0 && z1 == subtable->n[axis])
114     return subtable;
115
116   if (h0)
117     table_ref (subtable);
118   if (h1)
119     table_ref (subtable);
120
121   rect[TABLE_HORZ][0] = 0;
122   rect[TABLE_VERT][0] = 0;
123   rect[TABLE_HORZ][1] = subtable->n[TABLE_HORZ];
124   rect[TABLE_VERT][1] = subtable->n[TABLE_VERT];
125   rect[axis][0] = z0;
126   rect[axis][1] = z1;
127   table = table_select (subtable, rect);
128
129   if (h0)
130     table = table_paste (
131       table_select_slice (subtable, axis, 0, subtable->h[axis][0], false),
132       table, axis);
133
134   if (h1)
135     table = table_paste (
136       table,
137       table_select_slice (subtable, axis,
138                           subtable->n[axis] - subtable->h[axis][1],
139                           subtable->n[axis], false),
140       axis);
141
142   return table;
143 }
144
145 /* Takes ownership of TABLE and returns a new table whose contents are columns
146    X0 through X1 (exclusive) of SUBTABLE.  If ADD_HEADERS is true, the
147    returned table also includes any header columns in SUBTABLE. */
148 struct table *
149 table_select_columns (struct table *subtable, int x0, int x1,
150                       bool add_headers)
151 {
152   return table_select_slice (subtable, TABLE_HORZ, x0, x1, add_headers);
153 }
154
155 /* Takes ownership of TABLE and returns a new table whose contents are rows Y0
156    through Y1 (exclusive) of SUBTABLE.  If ADD_HEADERS is true, the returned
157    table also includes any header rows in SUBTABLE. */
158 struct table *
159 table_select_rows (struct table *subtable, int y0, int y1,
160                    bool add_headers)
161 {
162   return table_select_slice (subtable, TABLE_VERT, y0, y1, add_headers);
163 }
164
165 static void
166 table_select_destroy (struct table *ti)
167 {
168   struct table_select *ts = table_select_cast (ti);
169   table_unref (ts->subtable);
170   free (ts);
171 }
172
173 static void
174 table_select_get_cell (const struct table *ti, int x, int y,
175                        struct table_cell *cell)
176 {
177   struct table_select *ts = table_select_cast (ti);
178   int axis;
179
180   table_get_cell (ts->subtable,
181                   x + ts->ofs[TABLE_HORZ],
182                   y + ts->ofs[TABLE_VERT], cell);
183
184   for (axis = 0; axis < TABLE_N_AXES; axis++)
185     {
186       int *d = cell->d[axis];
187       int ofs = ts->ofs[axis];
188
189       d[0] = MAX (d[0] - ofs, 0);
190       d[1] = MIN (d[1] - ofs, ti->n[axis]);
191     }
192 }
193
194 static int
195 table_select_get_rule (const struct table *ti,
196                        enum table_axis axis,
197                        int x, int y)
198 {
199   struct table_select *ts = table_select_cast (ti);
200   return table_get_rule (ts->subtable, axis,
201                          x + ts->ofs[TABLE_HORZ],
202                          y + ts->ofs[TABLE_VERT]);
203 }
204
205 static struct table *
206 table_select_select (struct table *ti, int rect[TABLE_N_AXES][2])
207 {
208   struct table_select *ts = table_select_cast (ti);
209   int axis;
210
211   for (axis = 0; axis < TABLE_N_AXES; axis++)
212     {
213       int h1;
214
215       if (ts->table.h[axis][0] > rect[axis][0])
216         ts->table.h[axis][0] = ts->table.h[axis][0] - rect[axis][0];
217       else
218         ts->table.h[axis][0] = 0;
219
220       h1 = ts->table.n[axis] - ts->table.h[axis][1];
221       if (h1 < rect[axis][1])
222         ts->table.h[axis][1] = rect[axis][1] - h1;
223       else
224         ts->table.h[axis][1] = 0;
225
226       ts->ofs[axis] += rect[axis][0];
227       ts->table.n[axis] = rect[axis][1] - rect[axis][0];
228     }
229   return ti;
230 }
231
232 static const struct table_class table_select_class =
233   {
234     table_select_destroy,
235     table_select_get_cell,
236     table_select_get_rule,
237     NULL,                       /* paste */
238     table_select_select,
239   };