102b51404d3da130d29f3176404eb94317964e53
[pspp-builds.git] / src / output / table-paste.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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->class == &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->class == &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   /* Handle tables that know how to paste themselves. */
152   if (!table_is_shared (a) && !table_is_shared (b) && a != b)
153     {
154       if (a->class->paste != NULL)
155         {
156           struct table *new = a->class->paste (a, b, orientation);
157           if (new != NULL)
158             return new;
159         }
160       if (b->class->paste != NULL && a->class != b->class)
161         {
162           struct table *new = b->class->paste (a, b, orientation);
163           if (new != NULL)
164             return new;
165         }
166     }
167
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);
175   return &tp->table;
176 }
177
178 /* Shorthand for table_paste (left, right, TABLE_HORZ). */
179 struct table *
180 table_hpaste (struct table *left, struct table *right)
181 {
182   return table_paste (left, right, TABLE_HORZ);
183 }
184
185 /* Shorthand for table_paste (left, right, TABLE_VERT). */
186 struct table *
187 table_vpaste (struct table *left, struct table *right)
188 {
189   return table_paste (left, right, TABLE_VERT);
190 }
191
192 static void
193 table_paste_destroy (struct table *t)
194 {
195   struct table_paste *tp = table_paste_cast (t);
196   struct tower_node *node, *next;
197
198   for (node = tower_first (&tp->subtables); node != NULL; node = next)
199     {
200       struct paste_subtable *ps = paste_subtable_cast (node);
201       table_unref (ps->table);
202       next = tower_delete (&tp->subtables, node);
203       free (node);
204     }
205   free (tp);
206 }
207
208 static void
209 table_paste_get_cell (const struct table *t, int x, int y,
210                       struct table_cell *cell)
211 {
212   struct table_paste *tp = table_paste_cast (t);
213   struct paste_subtable *ps;
214   unsigned long int start;
215   int d[TABLE_N_AXES];
216
217   d[TABLE_HORZ] = x;
218   d[TABLE_VERT] = y;
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;
224 }
225
226 static int
227 table_paste_get_rule (const struct table *t,
228                       enum table_axis axis, int x, int y)
229 {
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;
235
236   if (tp->orientation == axis)
237     {
238       int r;
239
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);
243       else
244         r = table_get_rule (ps->table, axis, k, h - start);
245       if (h == start + tower_node_get_size (&ps->node))
246         {
247           struct tower_node *ps2_ = tower_next (&tp->subtables, &ps->node);
248           if (ps2_ != NULL)
249             {
250               struct paste_subtable *ps2 = paste_subtable_cast (ps2_);
251               int r2;
252
253               if (tp->orientation == TABLE_HORZ) /* XXX */
254                 r2 = table_get_rule (ps2->table, axis, 0, k);
255               else
256                 r2 = table_get_rule (ps2->table, axis, k, 0);
257               return table_rule_combine (r, r2);
258             }
259         }
260       return r;
261     }
262   else
263     {
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);
267       else
268         return table_get_rule (ps->table, axis, k, h - start);
269     }
270 }
271
272 static struct table *
273 table_paste_paste (struct table *a, struct table *b,
274                    enum table_axis orientation)
275 {
276   struct table_paste *ta, *tb;
277
278   ta = is_table_paste (a, orientation) ? table_paste_cast (a) : NULL;
279   tb = is_table_paste (b, orientation) ? table_paste_cast (b) : NULL;
280
281   if (ta != NULL)
282     {
283       if (tb != NULL)
284         {
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);
289           table_unref (b);
290         }
291       else
292         {
293           /* Append B to A's stack of subtables. */
294           table_paste_insert_subtable (ta, b, NULL);
295         }
296       reassess_headers (ta);
297       return a;
298     }
299   else if (tb != NULL)
300     {
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);
304       return b;
305     }
306   else
307     return NULL;
308 }
309
310 static const struct table_class table_paste_class =
311   {
312     table_paste_destroy,
313     table_paste_get_cell,
314     table_paste_get_rule,
315     table_paste_paste,
316     NULL,                       /* select */
317   };