1 /* PSPP - computes sample statistics.
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 /* FIXME: error checking. */
20 /* FIXME: merge pattern should be improved, this one causes a
21 performance regression. */
24 #include <math/merge.h>
26 #include <data/case-ordering.h>
27 #include <data/case.h>
28 #include <data/casereader.h>
29 #include <data/casewriter.h>
30 #include <libpspp/array.h>
31 #include <libpspp/assertion.h>
32 #include <libpspp/taint.h>
36 #define MAX_MERGE_ORDER 7
40 struct casereader *reader;
46 struct case_ordering *ordering;
47 struct merge_input inputs[MAX_MERGE_ORDER];
51 static void do_merge (struct merge *m);
54 merge_create (const struct case_ordering *ordering)
56 struct merge *m = xmalloc (sizeof *m);
57 m->ordering = case_ordering_clone (ordering);
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 size_t value_cnt = case_ordering_get_value_cnt (m->ordering);
101 struct casewriter *writer = mem_writer_create (value_cnt);
102 r = casewriter_make_reader (writer);
111 read_input_case (struct merge *m, size_t idx)
113 struct merge_input *i = &m->inputs[idx];
115 if (casereader_read (i->reader, &i->c))
119 casereader_destroy (i->reader);
120 remove_element (m->inputs, m->input_cnt, sizeof *m->inputs, idx);
127 do_merge (struct merge *m)
129 struct casewriter *w;
132 assert (m->input_cnt > 1);
134 w = tmpfile_writer_create (case_ordering_get_value_cnt (m->ordering));
135 for (i = 0; i < m->input_cnt; i++)
136 taint_propagate (casereader_get_taint (m->inputs[i].reader),
137 casewriter_get_taint (w));
139 for (i = 0; i < m->input_cnt; )
140 if (read_input_case (m, i))
142 while (m->input_cnt > 0)
147 for (i = 1; i < m->input_cnt; i++)
148 if (case_ordering_compare_cases (&m->inputs[i].c, &m->inputs[min].c,
152 casewriter_write (w, &m->inputs[min].c);
153 read_input_case (m, min);
157 m->inputs[0].reader = casewriter_make_reader (w);