Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / output / table-select.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 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_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]
101           && z1 == subtable->n[axis] - subtable->h[axis][1])
102         return subtable;
103
104       if (subtable->h[axis][0])
105         table_ref (subtable);
106       if (subtable->h[axis][1])
107         table_ref (subtable);
108     }
109   else
110     {
111       if (z0 == 0 && z1 == subtable->n[axis])
112         return subtable;
113     }
114
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];
119   rect[axis][0] = z0;
120   rect[axis][1] = z1;
121   table = table_select (subtable, rect);
122
123   if (add_headers)
124     {
125       if (subtable->h[axis][0])
126         table = table_paste (
127           table_select_slice (subtable, axis, 0, subtable->h[axis][0],
128                               false),
129           table, axis);
130
131       if (subtable->h[axis][1])
132         table = table_paste (
133           table,
134           table_select_slice (subtable, axis,
135                               subtable->n[axis] - subtable->h[axis][1],
136                               subtable->n[axis], false),
137           axis);
138     }
139
140   return table;
141 }
142
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. */
146 struct table *
147 table_select_columns (struct table *subtable, int x0, int x1,
148                       bool add_headers)
149 {
150   return table_select_slice (subtable, TABLE_HORZ, x0, x1, add_headers);
151 }
152
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. */
156 struct table *
157 table_select_rows (struct table *subtable, int y0, int y1,
158                    bool add_headers)
159 {
160   return table_select_slice (subtable, TABLE_VERT, y0, y1, add_headers);
161 }
162
163 static void
164 table_select_destroy (struct table *ti)
165 {
166   struct table_select *ts = table_select_cast (ti);
167   table_unref (ts->subtable);
168   free (ts);
169 }
170
171 static void
172 table_select_get_cell (const struct table *ti, int x, int y,
173                        struct table_cell *cell)
174 {
175   struct table_select *ts = table_select_cast (ti);
176   int axis;
177
178   table_get_cell (ts->subtable,
179                   x + ts->ofs[TABLE_HORZ],
180                   y + ts->ofs[TABLE_VERT], cell);
181
182   for (axis = 0; axis < TABLE_N_AXES; axis++)
183     {
184       int *d = cell->d[axis];
185       int ofs = ts->ofs[axis];
186
187       d[0] = MAX (d[0] - ofs, 0);
188       d[1] = MIN (d[1] - ofs, ti->n[axis]);
189     }
190 }
191
192 static int
193 table_select_get_rule (const struct table *ti,
194                        enum table_axis axis,
195                        int x, int y)
196 {
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]);
201 }
202
203 static struct table *
204 table_select_select (struct table *ti, int rect[TABLE_N_AXES][2])
205 {
206   struct table_select *ts = table_select_cast (ti);
207   int axis;
208
209   for (axis = 0; axis < TABLE_N_AXES; axis++)
210     {
211       int h1;
212
213       if (ts->table.h[axis][0] > rect[axis][0])
214         ts->table.h[axis][0] = ts->table.h[axis][0] - rect[axis][0];
215       else
216         ts->table.h[axis][0] = 0;
217
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;
221       else
222         ts->table.h[axis][1] = 0;
223
224       ts->ofs[axis] += rect[axis][0];
225       ts->table.n[axis] = rect[axis][1] - rect[axis][0];
226     }
227   return ti;
228 }
229
230 static const struct table_class table_select_class =
231   {
232     table_select_destroy,
233     table_select_get_cell,
234     table_select_get_rule,
235     NULL,                       /* paste */
236     table_select_select,
237   };