table-paste: Add error-checking assertion to table_paste().
[pspp] / src / output / table-paste.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 2014 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/tower.h"
21 #include "output/table-provider.h"
22
23 #include "gl/minmax.h"
24 #include "gl/xalloc.h"
25
26 struct paste_subtable
27   {
28     struct tower_node node;
29     struct table *table;
30   };
31
32 static struct paste_subtable *
33 paste_subtable_cast (struct tower_node *node)
34 {
35   return tower_data (node, struct paste_subtable, node);
36 }
37
38 struct table_paste
39   {
40     struct table table;
41     struct tower subtables;
42     enum table_axis orientation;
43   };
44
45 static const struct table_class table_paste_class;
46
47 static struct table_paste *
48 table_paste_cast (const struct table *table)
49 {
50   assert (table->klass == &table_paste_class);
51   return UP_CAST (table, struct table_paste, table);
52 }
53
54 static bool
55 is_table_paste (const struct table *table, int orientation)
56 {
57   return (table->klass == &table_paste_class
58           && table_paste_cast (table)->orientation == orientation);
59 }
60
61 static struct paste_subtable *
62 paste_subtable_lookup (struct table_paste *tp, unsigned long int offset,
63                        unsigned long int *start)
64 {
65   return paste_subtable_cast (tower_lookup (&tp->subtables, offset, start));
66 }
67
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. */
70 static void
71 table_paste_increase_size (struct table_paste *tp,
72                            const struct table *table)
73 {
74   int o = tp->orientation;
75   int h0, h1;
76
77   tp->table.n[o] += table->n[o];
78   tp->table.n[!o] = MAX (tp->table.n[!o], table->n[!o]);
79
80   h0 = table->h[!o][0];
81   h1 = table->h[!o][1];
82   if (tower_is_empty (&tp->subtables))
83     {
84       tp->table.h[!o][0] = h0;
85       tp->table.h[!o][1] = h1;
86     }
87   else
88     {
89       tp->table.h[!o][0] = MIN (tp->table.h[!o][0], h0);
90
91       /* XXX this is not quite right */
92       tp->table.h[!o][1] = MIN (tp->table.h[!o][1], h1);
93     }
94 }
95
96 static void
97 reassess_headers (struct table_paste *tp)
98 {
99   int o = tp->orientation;
100   if (tower_is_empty (&tp->subtables))
101     tp->table.h[o][0] = tp->table.h[o][1] = 0;
102   else
103     {
104       struct paste_subtable *h0, *h1;
105
106       h0 = paste_subtable_cast (tower_first (&tp->subtables));
107       tp->table.h[o][0] = h0->table->h[o][0];
108
109       h1 = paste_subtable_cast (tower_last (&tp->subtables));
110       tp->table.h[o][1] = h1->table->h[o][1];
111     }
112 }
113
114 static void
115 table_paste_insert_subtable (struct table_paste *tp,
116                              struct table *table,
117                              struct tower_node *under)
118 {
119   struct paste_subtable *subtable;
120
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);
127 }
128
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.
134
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
137    typical.
138
139    If A or B is null, returns the other argument. */
140 struct table *
141 table_paste (struct table *a, struct table *b, enum table_axis orientation)
142 {
143   struct table_paste *tp;
144
145   /* Handle nulls. */
146   if (a == NULL)
147     return b;
148   if (b == NULL)
149     return a;
150
151   assert (a->n[!orientation] == b->n[!orientation]);
152
153   /* Handle tables that know how to paste themselves. */
154   if (!table_is_shared (a) && !table_is_shared (b) && a != b)
155     {
156       if (a->klass->paste != NULL)
157         {
158           struct table *new = a->klass->paste (a, b, orientation);
159           if (new != NULL)
160             return new;
161         }
162       if (b->klass->paste != NULL && a->klass != b->klass)
163         {
164           struct table *new = b->klass->paste (a, b, orientation);
165           if (new != NULL)
166             return new;
167         }
168     }
169
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);
177   return &tp->table;
178 }
179
180 /* Shorthand for table_paste (left, right, TABLE_HORZ). */
181 struct table *
182 table_hpaste (struct table *left, struct table *right)
183 {
184   return table_paste (left, right, TABLE_HORZ);
185 }
186
187 /* Shorthand for table_paste (top, bottom, TABLE_VERT). */
188 struct table *
189 table_vpaste (struct table *top, struct table *bottom)
190 {
191   return table_paste (top, bottom, TABLE_VERT);
192 }
193
194 static void
195 table_paste_destroy (struct table *t)
196 {
197   struct table_paste *tp = table_paste_cast (t);
198   struct tower_node *node, *next;
199
200   for (node = tower_first (&tp->subtables); node != NULL; node = next)
201     {
202       struct paste_subtable *ps = paste_subtable_cast (node);
203       table_unref (ps->table);
204       next = tower_delete (&tp->subtables, node);
205       free (node);
206     }
207   free (tp);
208 }
209
210 static void
211 table_paste_get_cell (const struct table *t, int x, int y,
212                       struct table_cell *cell)
213 {
214   struct table_paste *tp = table_paste_cast (t);
215   struct paste_subtable *ps;
216   unsigned long int start;
217   int d[TABLE_N_AXES];
218
219   d[TABLE_HORZ] = x;
220   d[TABLE_VERT] = y;
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;
226 }
227
228 static int
229 table_paste_get_rule (const struct table *t,
230                       enum table_axis axis, int x, int y)
231 {
232   struct table_paste *tp = table_paste_cast (t);
233   int h = tp->orientation == TABLE_HORZ ? x : y;
234   int k = tp->orientation == TABLE_HORZ ? y : x;
235   struct paste_subtable *ps;
236   unsigned long int start;
237
238   if (tp->orientation == axis)
239     {
240       int r;
241
242       ps = paste_subtable_lookup (tp, h == 0 ? 0 : h - 1, &start);
243       if (tp->orientation == TABLE_HORZ) /* XXX */
244         r = table_get_rule (ps->table, axis, h - start, k);
245       else
246         r = table_get_rule (ps->table, axis, k, h - start);
247       if (h == start + tower_node_get_size (&ps->node))
248         {
249           struct tower_node *ps2_ = tower_next (&tp->subtables, &ps->node);
250           if (ps2_ != NULL)
251             {
252               struct paste_subtable *ps2 = paste_subtable_cast (ps2_);
253               int r2;
254
255               if (tp->orientation == TABLE_HORZ) /* XXX */
256                 r2 = table_get_rule (ps2->table, axis, 0, k);
257               else
258                 r2 = table_get_rule (ps2->table, axis, k, 0);
259               return table_rule_combine (r, r2);
260             }
261         }
262       return r;
263     }
264   else
265     {
266       ps = paste_subtable_lookup (tp, h, &start);
267       if (tp->orientation == TABLE_HORZ) /* XXX */
268         return table_get_rule (ps->table, axis, h - start, k);
269       else
270         return table_get_rule (ps->table, axis, k, h - start);
271     }
272 }
273
274 static struct table *
275 table_paste_paste (struct table *a, struct table *b,
276                    enum table_axis orientation)
277 {
278   struct table_paste *ta, *tb;
279
280   ta = is_table_paste (a, orientation) ? table_paste_cast (a) : NULL;
281   tb = is_table_paste (b, orientation) ? table_paste_cast (b) : NULL;
282
283   if (ta != NULL)
284     {
285       if (tb != NULL)
286         {
287           /* Append all of B's subtables onto A, then destroy B. */
288           table_paste_increase_size (ta, b);
289           tower_splice (&ta->subtables, NULL,
290                         &tb->subtables, tower_first (&tb->subtables), NULL);
291           table_unref (b);
292         }
293       else
294         {
295           /* Append B to A's stack of subtables. */
296           table_paste_insert_subtable (ta, b, NULL);
297         }
298       reassess_headers (ta);
299       return a;
300     }
301   else if (tb != NULL)
302     {
303       /* Insert A at the beginning of B's stack of subtables. */
304       table_paste_insert_subtable (tb, a, tower_first (&tb->subtables));
305       reassess_headers (tb);
306       return b;
307     }
308   else
309     return NULL;
310 }
311
312 static const struct table_class table_paste_class =
313   {
314     table_paste_destroy,
315     table_paste_get_cell,
316     table_paste_get_rule,
317     table_paste_paste,
318     NULL,                       /* select */
319   };