9e860c4657a83029fc923e835f30579b39d2ea1c
[pspp-builds.git] / src / math / sort.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006 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 "sort.h"
20
21 #include <stdio.h>
22
23 #include <data/case.h>
24 #include <data/casereader.h>
25 #include <data/casewriter.h>
26 #include <data/casewriter-provider.h>
27 #include <data/settings.h>
28 #include <data/subcase.h>
29 #include <libpspp/array.h>
30 #include <libpspp/assertion.h>
31 #include <math/merge.h>
32
33 #include "xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* These should only be changed for testing purposes. */
39 int min_buffers = 64;
40 int max_buffers = INT_MAX;
41
42 struct sort_writer
43   {
44     size_t value_cnt;
45     struct subcase ordering;
46     struct merge *merge;
47     struct pqueue *pqueue;
48
49     struct casewriter *run;
50     casenumber run_id;
51     struct ccase run_end;
52   };
53
54 static struct casewriter_class sort_casewriter_class;
55
56 static struct pqueue *pqueue_create (const struct subcase *, size_t);
57 static void pqueue_destroy (struct pqueue *);
58 static bool pqueue_is_full (const struct pqueue *);
59 static bool pqueue_is_empty (const struct pqueue *);
60 static void pqueue_push (struct pqueue *, struct ccase *, casenumber);
61 static void pqueue_pop (struct pqueue *, struct ccase *, casenumber *);
62
63 static void output_record (struct sort_writer *);
64
65 struct casewriter *
66 sort_create_writer (const struct subcase *ordering, size_t value_cnt)
67 {
68   struct sort_writer *sort;
69
70   sort = xmalloc (sizeof *sort);
71   sort->value_cnt = value_cnt;
72   subcase_clone (&sort->ordering, ordering);
73   sort->merge = merge_create (ordering, value_cnt);
74   sort->pqueue = pqueue_create (ordering, value_cnt);
75   sort->run = NULL;
76   sort->run_id = 0;
77   case_nullify (&sort->run_end);
78
79   return casewriter_create (value_cnt, &sort_casewriter_class, sort);
80 }
81
82 static void
83 sort_casewriter_write (struct casewriter *writer UNUSED, void *sort_,
84                        struct ccase *c)
85 {
86   struct sort_writer *sort = sort_;
87   bool next_run;
88
89   if (pqueue_is_full (sort->pqueue))
90     output_record (sort);
91
92   next_run = (case_is_null (&sort->run_end)
93               || subcase_compare_3way (&sort->ordering, c,
94                                        &sort->ordering, &sort->run_end) < 0);
95   pqueue_push (sort->pqueue, c, sort->run_id + (next_run ? 1 : 0));
96 }
97
98 static void
99 sort_casewriter_destroy (struct casewriter *writer UNUSED, void *sort_)
100 {
101   struct sort_writer *sort = sort_;
102
103   subcase_destroy (&sort->ordering);
104   merge_destroy (sort->merge);
105   pqueue_destroy (sort->pqueue);
106   casewriter_destroy (sort->run);
107   case_destroy (&sort->run_end);
108   free (sort);
109 }
110
111 static struct casereader *
112 sort_casewriter_convert_to_reader (struct casewriter *writer, void *sort_)
113 {
114   struct sort_writer *sort = sort_;
115   struct casereader *output;
116
117   if (sort->run == NULL && sort->run_id == 0)
118     {
119       /* In-core sort. */
120       sort->run = mem_writer_create (casewriter_get_value_cnt (writer));
121       sort->run_id = 1;
122     }
123   while (!pqueue_is_empty (sort->pqueue))
124     output_record (sort);
125
126   merge_append (sort->merge, casewriter_make_reader (sort->run));
127   sort->run = NULL;
128
129   output = merge_make_reader (sort->merge);
130   sort_casewriter_destroy (writer, sort);
131   return output;
132 }
133
134 static void
135 output_record (struct sort_writer *sort)
136 {
137   struct ccase min_case;
138   casenumber min_run_id;
139
140   pqueue_pop (sort->pqueue, &min_case, &min_run_id);
141 #if 0
142   printf ("\toutput: %f to run %d\n", case_num_idx (&min_case, 0), min_run_id);
143 #endif
144
145   if (sort->run_id != min_run_id && sort->run != NULL)
146     {
147       merge_append (sort->merge, casewriter_make_reader (sort->run));
148       sort->run = NULL;
149     }
150   if (sort->run == NULL)
151     {
152       sort->run = tmpfile_writer_create (sort->value_cnt);
153       sort->run_id = min_run_id;
154     }
155
156   case_destroy (&sort->run_end);
157   case_clone (&sort->run_end, &min_case);
158
159   casewriter_write (sort->run, &min_case);
160 }
161
162 static struct casewriter_class sort_casewriter_class =
163   {
164     sort_casewriter_write,
165     sort_casewriter_destroy,
166     sort_casewriter_convert_to_reader,
167   };
168 \f
169 /* Reads all the cases from INPUT.  Sorts the cases according to
170    ORDERING.  Returns the sorted cases in a new casereader. */
171 struct casereader *
172 sort_execute (struct casereader *input, const struct subcase *ordering)
173 {
174   struct casewriter *output =
175     sort_create_writer (ordering, casereader_get_value_cnt (input));
176   casereader_transfer (input, output);
177   return casewriter_make_reader (output);
178 }
179
180 /* Reads all the cases from INPUT.  Sorts the cases in ascending
181    order according to VARIABLE.  Returns the sorted cases in a
182    new casereader. */
183 struct casereader *
184 sort_execute_1var (struct casereader *input, const struct variable *var)
185 {
186   struct subcase sc;
187   struct casereader *reader;
188
189   subcase_init_var (&sc, var, SC_ASCEND);
190   reader = sort_execute (input, &sc);
191   subcase_destroy (&sc);
192   return reader;
193 }
194 \f
195 struct pqueue
196   {
197     struct subcase ordering;
198     struct pqueue_record *records;
199     size_t record_cnt;
200     size_t record_cap;
201     casenumber idx;
202   };
203
204 struct pqueue_record
205   {
206     casenumber id;
207     struct ccase c;
208     casenumber idx;
209   };
210
211 static int compare_pqueue_records_minheap (const void *a, const void *b,
212                                            const void *pq_);
213
214 static struct pqueue *
215 pqueue_create (const struct subcase *ordering, size_t value_cnt)
216 {
217   struct pqueue *pq;
218
219   pq = xmalloc (sizeof *pq);
220   subcase_clone (&pq->ordering, ordering);
221   pq->record_cap
222     = settings_get_workspace_cases (value_cnt);
223   if (pq->record_cap > max_buffers)
224     pq->record_cap = max_buffers;
225   else if (pq->record_cap < min_buffers)
226     pq->record_cap = min_buffers;
227   pq->record_cnt = 0;
228   pq->records = xnmalloc (pq->record_cap, sizeof *pq->records);
229   pq->idx = 0;
230
231   return pq;
232 }
233
234 static void
235 pqueue_destroy (struct pqueue *pq)
236 {
237   if (pq != NULL)
238     {
239       while (!pqueue_is_empty (pq))
240         {
241           struct ccase c;
242           casenumber id;
243           pqueue_pop (pq, &c, &id);
244           case_destroy (&c);
245         }
246       subcase_destroy (&pq->ordering);
247       free (pq->records);
248       free (pq);
249     }
250 }
251
252 static bool
253 pqueue_is_full (const struct pqueue *pq)
254 {
255   return pq->record_cnt >= pq->record_cap;
256 }
257
258 static bool
259 pqueue_is_empty (const struct pqueue *pq)
260 {
261   return pq->record_cnt == 0;
262 }
263
264 static void
265 pqueue_push (struct pqueue *pq, struct ccase *c, casenumber id)
266 {
267   struct pqueue_record *r;
268
269   assert (!pqueue_is_full (pq));
270
271   r = &pq->records[pq->record_cnt++];
272   r->id = id;
273   case_move (&r->c, c);
274   r->idx = pq->idx++;
275
276   push_heap (pq->records, pq->record_cnt, sizeof *pq->records,
277              compare_pqueue_records_minheap, pq);
278 }
279
280 static void
281 pqueue_pop (struct pqueue *pq, struct ccase *c, casenumber *id)
282 {
283   struct pqueue_record *r;
284
285   assert (!pqueue_is_empty (pq));
286
287   pop_heap (pq->records, pq->record_cnt--, sizeof *pq->records,
288             compare_pqueue_records_minheap, pq);
289
290   r = &pq->records[pq->record_cnt];
291   *id = r->id;
292   case_move (c, &r->c);
293 }
294
295 /* Compares record-run tuples A and B on id, then on case data,
296    then on insertion order, in descending order. */
297 static int
298 compare_pqueue_records_minheap (const void *a_, const void *b_,
299                                 const void *pq_)
300 {
301   const struct pqueue_record *a = a_;
302   const struct pqueue_record *b = b_;
303   const struct pqueue *pq = pq_;
304   int result = a->id < b->id ? -1 : a->id > b->id;
305   if (result == 0)
306     result = subcase_compare_3way (&pq->ordering, &a->c,
307                                    &pq->ordering, &b->c);
308   if (result == 0)
309     result = a->idx < b->idx ? -1 : a->idx > b->idx;
310   return -result;
311 }