1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 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/tower.h"
21 #include "output/table-provider.h"
23 #include "gl/minmax.h"
24 #include "gl/xalloc.h"
28 struct tower_node node;
32 static struct paste_subtable *
33 paste_subtable_cast (struct tower_node *node)
35 return tower_data (node, struct paste_subtable, node);
41 struct tower subtables;
42 enum table_axis orientation;
45 static const struct table_class table_paste_class;
47 static struct table_paste *
48 table_paste_cast (const struct table *table)
50 assert (table->klass == &table_paste_class);
51 return UP_CAST (table, struct table_paste, table);
55 is_table_paste (const struct table *table, int orientation)
57 return (table->klass == &table_paste_class
58 && table_paste_cast (table)->orientation == orientation);
61 static struct paste_subtable *
62 paste_subtable_lookup (struct table_paste *tp, unsigned long int offset,
63 unsigned long int *start)
65 return paste_subtable_cast (tower_lookup (&tp->subtables, offset, start));
68 /* This must be called *before* adding TABLE to TP, otherwise the test for
69 whether TP is empty will not have the correct effect. */
71 table_paste_increase_size (struct table_paste *tp,
72 const struct table *table)
74 int o = tp->orientation;
77 tp->table.n[o] += table->n[o];
78 tp->table.n[!o] = MAX (tp->table.n[!o], table->n[!o]);
82 if (tower_is_empty (&tp->subtables))
84 tp->table.h[!o][0] = h0;
85 tp->table.h[!o][1] = h1;
89 tp->table.h[!o][0] = MIN (tp->table.h[!o][0], h0);
91 /* XXX this is not quite right */
92 tp->table.h[!o][1] = MIN (tp->table.h[!o][1], h1);
97 reassess_headers (struct table_paste *tp)
99 int o = tp->orientation;
100 if (tower_is_empty (&tp->subtables))
101 tp->table.h[o][0] = tp->table.h[o][1] = 0;
104 struct paste_subtable *h0, *h1;
106 h0 = paste_subtable_cast (tower_first (&tp->subtables));
107 tp->table.h[o][0] = h0->table->h[o][0];
109 h1 = paste_subtable_cast (tower_last (&tp->subtables));
110 tp->table.h[o][1] = h1->table->h[o][1];
115 table_paste_insert_subtable (struct table_paste *tp,
117 struct tower_node *under)
119 struct paste_subtable *subtable;
121 subtable = xmalloc (sizeof *subtable);
122 table_paste_increase_size (tp, table);
123 tower_insert (&tp->subtables, table->n[tp->orientation],
124 &subtable->node, under);
125 subtable->table = table;
126 reassess_headers (tp);
129 /* Takes ownership of A and B and returns a table that consists of tables A and
130 B "pasted together", that is, a table whose size is the sum of the sizes of
131 A and B along the axis specified by ORIENTATION. A and B should have the
132 same size along the axis opposite ORIENTATION; the handling of tables that
133 have different sizes along that axis may vary.
135 The rules at the seam between A and B are combined. The exact way in which
136 they are combined is unspecified, but the method of table_rule_combine() is
139 If A or B is null, returns the other argument. */
141 table_paste (struct table *a, struct table *b, enum table_axis orientation)
143 struct table_paste *tp;
151 assert (a->n[!orientation] == b->n[!orientation]);
153 /* Handle tables that know how to paste themselves. */
154 if (!table_is_shared (a) && !table_is_shared (b) && a != b)
156 if (a->klass->paste != NULL)
158 struct table *new = a->klass->paste (a, b, orientation);
162 if (b->klass->paste != NULL && a->klass != b->klass)
164 struct table *new = b->klass->paste (a, b, orientation);
170 /* Create new table_paste and insert A and B into it. */
171 tp = xmalloc (sizeof *tp);
172 table_init (&tp->table, &table_paste_class);
173 tower_init (&tp->subtables);
174 tp->orientation = orientation;
175 table_paste_insert_subtable (tp, a, NULL);
176 table_paste_insert_subtable (tp, b, NULL);
180 /* Shorthand for table_paste (left, right, TABLE_HORZ). */
182 table_hpaste (struct table *left, struct table *right)
184 return table_paste (left, right, TABLE_HORZ);
187 /* Shorthand for table_paste (top, bottom, TABLE_VERT). */
189 table_vpaste (struct table *top, struct table *bottom)
191 return table_paste (top, bottom, TABLE_VERT);
195 table_paste_destroy (struct table *t)
197 struct table_paste *tp = table_paste_cast (t);
198 struct tower_node *node, *next;
200 for (node = tower_first (&tp->subtables); node != NULL; node = next)
202 struct paste_subtable *ps = paste_subtable_cast (node);
203 table_unref (ps->table);
204 next = tower_delete (&tp->subtables, node);
211 table_paste_get_cell (const struct table *t, int x, int y,
212 struct table_cell *cell)
214 struct table_paste *tp = table_paste_cast (t);
215 struct paste_subtable *ps;
216 unsigned long int start;
221 ps = paste_subtable_lookup (tp, d[tp->orientation], &start);
222 d[tp->orientation] -= start;
223 table_get_cell (ps->table, d[TABLE_HORZ], d[TABLE_VERT], cell);
224 cell->d[tp->orientation][0] += start;
225 cell->d[tp->orientation][1] += start;
229 table_paste_get_rule (const struct table *t,
230 enum table_axis axis, int x, int y,
231 struct cell_color *color)
233 struct table_paste *tp = table_paste_cast (t);
234 int h = tp->orientation == TABLE_HORZ ? x : y;
235 int k = tp->orientation == TABLE_HORZ ? y : x;
236 struct paste_subtable *ps;
237 unsigned long int start;
239 if (tp->orientation == axis)
243 ps = paste_subtable_lookup (tp, h == 0 ? 0 : h - 1, &start);
244 if (tp->orientation == TABLE_HORZ) /* XXX */
245 r = table_get_rule (ps->table, axis, h - start, k, color);
247 r = table_get_rule (ps->table, axis, k, h - start, color);
248 if (h == start + tower_node_get_size (&ps->node))
250 struct tower_node *ps2_ = tower_next (&tp->subtables, &ps->node);
253 struct paste_subtable *ps2 = paste_subtable_cast (ps2_);
256 if (tp->orientation == TABLE_HORZ) /* XXX */
257 r2 = table_get_rule (ps2->table, axis, 0, k, color);
259 r2 = table_get_rule (ps2->table, axis, k, 0, color);
260 return table_rule_combine (r, r2);
267 ps = paste_subtable_lookup (tp, h, &start);
268 if (tp->orientation == TABLE_HORZ) /* XXX */
269 return table_get_rule (ps->table, axis, h - start, k, color);
271 return table_get_rule (ps->table, axis, k, h - start, color);
275 static struct table *
276 table_paste_paste (struct table *a, struct table *b,
277 enum table_axis orientation)
279 struct table_paste *ta, *tb;
281 ta = is_table_paste (a, orientation) ? table_paste_cast (a) : NULL;
282 tb = is_table_paste (b, orientation) ? table_paste_cast (b) : NULL;
288 /* Append all of B's subtables onto A, then destroy B. */
289 table_paste_increase_size (ta, b);
290 tower_splice (&ta->subtables, NULL,
291 &tb->subtables, tower_first (&tb->subtables), NULL);
296 /* Append B to A's stack of subtables. */
297 table_paste_insert_subtable (ta, b, NULL);
299 reassess_headers (ta);
304 /* Insert A at the beginning of B's stack of subtables. */
305 table_paste_insert_subtable (tb, a, tower_first (&tb->subtables));
306 reassess_headers (tb);
313 static const struct table_class table_paste_class =
316 table_paste_get_cell,
317 table_paste_get_rule,