1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 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/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 /* Handle tables that know how to paste themselves. */
152 if (!table_is_shared (a) && !table_is_shared (b) && a != b)
154 if (a->klass->paste != NULL)
156 struct table *new = a->klass->paste (a, b, orientation);
160 if (b->klass->paste != NULL && a->klass != b->klass)
162 struct table *new = b->klass->paste (a, b, orientation);
168 /* Create new table_paste and insert A and B into it. */
169 tp = xmalloc (sizeof *tp);
170 table_init (&tp->table, &table_paste_class);
171 tower_init (&tp->subtables);
172 tp->orientation = orientation;
173 table_paste_insert_subtable (tp, a, NULL);
174 table_paste_insert_subtable (tp, b, NULL);
178 /* Shorthand for table_paste (left, right, TABLE_HORZ). */
180 table_hpaste (struct table *left, struct table *right)
182 return table_paste (left, right, TABLE_HORZ);
185 /* Shorthand for table_paste (left, right, TABLE_VERT). */
187 table_vpaste (struct table *left, struct table *right)
189 return table_paste (left, right, TABLE_VERT);
193 table_paste_destroy (struct table *t)
195 struct table_paste *tp = table_paste_cast (t);
196 struct tower_node *node, *next;
198 for (node = tower_first (&tp->subtables); node != NULL; node = next)
200 struct paste_subtable *ps = paste_subtable_cast (node);
201 table_unref (ps->table);
202 next = tower_delete (&tp->subtables, node);
209 table_paste_get_cell (const struct table *t, int x, int y,
210 struct table_cell *cell)
212 struct table_paste *tp = table_paste_cast (t);
213 struct paste_subtable *ps;
214 unsigned long int start;
219 ps = paste_subtable_lookup (tp, d[tp->orientation], &start);
220 d[tp->orientation] -= start;
221 table_get_cell (ps->table, d[TABLE_HORZ], d[TABLE_VERT], cell);
222 cell->d[tp->orientation][0] += start;
223 cell->d[tp->orientation][1] += start;
227 table_paste_get_rule (const struct table *t,
228 enum table_axis axis, int x, int y)
230 struct table_paste *tp = table_paste_cast (t);
231 int h = tp->orientation == TABLE_HORZ ? x : y;
232 int k = tp->orientation == TABLE_HORZ ? y : x;
233 struct paste_subtable *ps;
234 unsigned long int start;
236 if (tp->orientation == axis)
240 ps = paste_subtable_lookup (tp, h == 0 ? 0 : h - 1, &start);
241 if (tp->orientation == TABLE_HORZ) /* XXX */
242 r = table_get_rule (ps->table, axis, h - start, k);
244 r = table_get_rule (ps->table, axis, k, h - start);
245 if (h == start + tower_node_get_size (&ps->node))
247 struct tower_node *ps2_ = tower_next (&tp->subtables, &ps->node);
250 struct paste_subtable *ps2 = paste_subtable_cast (ps2_);
253 if (tp->orientation == TABLE_HORZ) /* XXX */
254 r2 = table_get_rule (ps2->table, axis, 0, k);
256 r2 = table_get_rule (ps2->table, axis, k, 0);
257 return table_rule_combine (r, r2);
264 ps = paste_subtable_lookup (tp, h, &start);
265 if (tp->orientation == TABLE_HORZ) /* XXX */
266 return table_get_rule (ps->table, axis, h - start, k);
268 return table_get_rule (ps->table, axis, k, h - start);
272 static struct table *
273 table_paste_paste (struct table *a, struct table *b,
274 enum table_axis orientation)
276 struct table_paste *ta, *tb;
278 ta = is_table_paste (a, orientation) ? table_paste_cast (a) : NULL;
279 tb = is_table_paste (b, orientation) ? table_paste_cast (b) : NULL;
285 /* Append all of B's subtables onto A, then destroy B. */
286 table_paste_increase_size (ta, b);
287 tower_splice (&ta->subtables, NULL,
288 &tb->subtables, tower_first (&tb->subtables), NULL);
293 /* Append B to A's stack of subtables. */
294 table_paste_insert_subtable (ta, b, NULL);
296 reassess_headers (ta);
301 /* Insert A at the beginning of B's stack of subtables. */
302 table_paste_insert_subtable (tb, a, tower_first (&tb->subtables));
303 reassess_headers (tb);
310 static const struct table_class table_paste_class =
313 table_paste_get_cell,
314 table_paste_get_rule,