1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2011, 2014 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];
99 h0 = add_headers && subtable->h[axis][0] > 0;
100 if (h0 && z0 == subtable->h[axis][0])
106 h1 = add_headers && subtable->h[axis][1] > 0;
107 if (h1 && z1 == subtable->n[axis] - subtable->h[axis][1])
109 z1 = subtable->n[axis];
113 if (z0 == 0 && z1 == subtable->n[axis])
117 table_ref (subtable);
119 table_ref (subtable);
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];
127 table = table_select (subtable, rect);
130 table = table_paste (
131 table_select_slice (subtable, axis, 0, subtable->h[axis][0], false),
135 table = table_paste (
137 table_select_slice (subtable, axis,
138 subtable->n[axis] - subtable->h[axis][1],
139 subtable->n[axis], false),
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. */
149 table_select_columns (struct table *subtable, int x0, int x1,
152 return table_select_slice (subtable, TABLE_HORZ, x0, x1, add_headers);
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. */
159 table_select_rows (struct table *subtable, int y0, int y1,
162 return table_select_slice (subtable, TABLE_VERT, y0, y1, add_headers);
166 table_select_destroy (struct table *ti)
168 struct table_select *ts = table_select_cast (ti);
169 table_unref (ts->subtable);
174 table_select_get_cell (const struct table *ti, int x, int y,
175 struct table_cell *cell)
177 struct table_select *ts = table_select_cast (ti);
180 table_get_cell (ts->subtable,
181 x + ts->ofs[TABLE_HORZ],
182 y + ts->ofs[TABLE_VERT], cell);
184 for (axis = 0; axis < TABLE_N_AXES; axis++)
186 int *d = cell->d[axis];
187 int ofs = ts->ofs[axis];
189 d[0] = MAX (d[0] - ofs, 0);
190 d[1] = MIN (d[1] - ofs, ti->n[axis]);
195 table_select_get_rule (const struct table *ti,
196 enum table_axis axis,
197 int x, int y, struct cell_color *color)
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], color);
205 static struct table *
206 table_select_select (struct table *ti, int rect[TABLE_N_AXES][2])
208 struct table_select *ts = table_select_cast (ti);
211 for (axis = 0; axis < TABLE_N_AXES; axis++)
215 if (ts->table.h[axis][0] > rect[axis][0])
216 ts->table.h[axis][0] = ts->table.h[axis][0] - rect[axis][0];
218 ts->table.h[axis][0] = 0;
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;
224 ts->table.h[axis][1] = 0;
226 ts->ofs[axis] += rect[axis][0];
227 ts->table.n[axis] = rect[axis][1] - rect[axis][0];
232 static const struct table_class table_select_class =
234 table_select_destroy,
235 table_select_get_cell,
236 table_select_get_rule,