1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 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/>. */
17 /* FIXME: error checking. */
18 /* FIXME: merge pattern should be improved, this one causes a
19 performance regression. */
22 #include <math/merge.h>
24 #include <data/case-ordering.h>
25 #include <data/case.h>
26 #include <data/casereader.h>
27 #include <data/casewriter.h>
28 #include <libpspp/array.h>
29 #include <libpspp/assertion.h>
30 #include <libpspp/taint.h>
34 #define MAX_MERGE_ORDER 7
38 struct casereader *reader;
44 struct case_ordering *ordering;
45 struct merge_input inputs[MAX_MERGE_ORDER];
50 static void do_merge (struct merge *m);
53 merge_create (const struct case_ordering *ordering, size_t value_cnt)
55 struct merge *m = xmalloc (sizeof *m);
56 m->ordering = case_ordering_clone (ordering);
58 m->value_cnt = value_cnt;
63 merge_destroy (struct merge *m)
69 case_ordering_destroy (m->ordering);
70 for (i = 0; i < m->input_cnt; i++)
71 casereader_destroy (m->inputs[i].reader);
77 merge_append (struct merge *m, struct casereader *r)
79 r = casereader_rename (r);
80 m->inputs[m->input_cnt++].reader = r;
81 if (m->input_cnt >= MAX_MERGE_ORDER)
86 merge_make_reader (struct merge *m)
93 if (m->input_cnt == 1)
95 r = m->inputs[0].reader;
98 else if (m->input_cnt == 0)
100 struct casewriter *writer = mem_writer_create (m->value_cnt);
101 r = casewriter_make_reader (writer);
110 read_input_case (struct merge *m, size_t idx)
112 struct merge_input *i = &m->inputs[idx];
114 if (casereader_read (i->reader, &i->c))
118 casereader_destroy (i->reader);
119 remove_element (m->inputs, m->input_cnt, sizeof *m->inputs, idx);
126 do_merge (struct merge *m)
128 struct casewriter *w;
131 assert (m->input_cnt > 1);
133 w = tmpfile_writer_create (m->value_cnt);
134 for (i = 0; i < m->input_cnt; i++)
135 taint_propagate (casereader_get_taint (m->inputs[i].reader),
136 casewriter_get_taint (w));
138 for (i = 0; i < m->input_cnt; )
139 if (read_input_case (m, i))
141 while (m->input_cnt > 0)
146 for (i = 1; i < m->input_cnt; i++)
147 if (case_ordering_compare_cases (&m->inputs[i].c, &m->inputs[min].c,
151 casewriter_write (w, &m->inputs[min].c);
152 read_input_case (m, min);
156 m->inputs[0].reader = casewriter_make_reader (w);