1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 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"
29 struct table *subtable;
33 static const struct table_class table_select_class;
35 static struct table_select *
36 table_select_cast (const struct table *table)
38 assert (table->klass == &table_select_class);
39 return UP_CAST (table, struct table_select, table);
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]. */
47 table_select (struct table *subtable, int rect[TABLE_N_AXES][2])
49 struct table_select *ts;
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])
58 if (!table_is_shared (subtable) && subtable->klass->select != NULL)
60 struct table *selected = subtable->klass->select (subtable, rect);
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++)
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;
82 /* Takes ownership of TABLE and returns a new table whose contents are:
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
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
92 table_select_slice (struct table *subtable, enum table_axis axis,
93 int z0, int z1, bool add_headers)
96 int rect[TABLE_N_AXES][2];
100 if (z0 == subtable->h[axis][0]
101 && z1 == subtable->n[axis] - subtable->h[axis][1])
104 if (subtable->h[axis][0])
105 table_ref (subtable);
106 if (subtable->h[axis][1])
107 table_ref (subtable);
111 if (z0 == 0 && z1 == subtable->n[axis])
115 rect[TABLE_HORZ][0] = 0;
116 rect[TABLE_VERT][0] = 0;
117 rect[TABLE_HORZ][1] = subtable->n[TABLE_HORZ];
118 rect[TABLE_VERT][1] = subtable->n[TABLE_VERT];
121 table = table_select (subtable, rect);
125 if (subtable->h[axis][0])
126 table = table_paste (
127 table_select_slice (subtable, axis, 0, subtable->h[axis][0],
131 if (subtable->h[axis][1])
132 table = table_paste (
134 table_select_slice (subtable, axis,
135 subtable->n[axis] - subtable->h[axis][1],
136 subtable->n[axis], false),
143 /* Takes ownership of TABLE and returns a new table whose contents are columns
144 X0 through X1 (exclusive) of SUBTABLE. If ADD_HEADERS is true, the
145 returned table also includes any header columns in SUBTABLE. */
147 table_select_columns (struct table *subtable, int x0, int x1,
150 return table_select_slice (subtable, TABLE_HORZ, x0, x1, add_headers);
153 /* Takes ownership of TABLE and returns a new table whose contents are rows Y0
154 through Y1 (exclusive) of SUBTABLE. If ADD_HEADERS is true, the returned
155 table also includes any header rows in SUBTABLE. */
157 table_select_rows (struct table *subtable, int y0, int y1,
160 return table_select_slice (subtable, TABLE_VERT, y0, y1, add_headers);
164 table_select_destroy (struct table *ti)
166 struct table_select *ts = table_select_cast (ti);
167 table_unref (ts->subtable);
172 table_select_get_cell (const struct table *ti, int x, int y,
173 struct table_cell *cell)
175 struct table_select *ts = table_select_cast (ti);
178 table_get_cell (ts->subtable,
179 x + ts->ofs[TABLE_HORZ],
180 y + ts->ofs[TABLE_VERT], cell);
182 for (axis = 0; axis < TABLE_N_AXES; axis++)
184 int *d = cell->d[axis];
185 int ofs = ts->ofs[axis];
187 d[0] = MAX (d[0] - ofs, 0);
188 d[1] = MIN (d[1] - ofs, ti->n[axis]);
193 table_select_get_rule (const struct table *ti,
194 enum table_axis axis,
197 struct table_select *ts = table_select_cast (ti);
198 return table_get_rule (ts->subtable, axis,
199 x + ts->ofs[TABLE_HORZ],
200 y + ts->ofs[TABLE_VERT]);
203 static struct table *
204 table_select_select (struct table *ti, int rect[TABLE_N_AXES][2])
206 struct table_select *ts = table_select_cast (ti);
209 for (axis = 0; axis < TABLE_N_AXES; axis++)
213 if (ts->table.h[axis][0] > rect[axis][0])
214 ts->table.h[axis][0] = ts->table.h[axis][0] - rect[axis][0];
216 ts->table.h[axis][0] = 0;
218 h1 = ts->table.n[axis] - ts->table.h[axis][1];
219 if (h1 < rect[axis][1])
220 ts->table.h[axis][1] = rect[axis][1] - h1;
222 ts->table.h[axis][1] = 0;
224 ts->ofs[axis] += rect[axis][0];
225 ts->table.n[axis] = rect[axis][1] - rect[axis][0];
230 static const struct table_class table_select_class =
232 table_select_destroy,
233 table_select_get_cell,
234 table_select_get_rule,