Make casewriters keep track of the number of `union value's in each
[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-ordering.h>
24 #include <data/case.h>
25 #include <data/casereader.h>
26 #include <data/casewriter.h>
27 #include <data/casewriter-provider.h>
28 #include <data/settings.h>
29 #include <libpspp/alloc.h>
30 #include <libpspp/array.h>
31 #include <libpspp/assertion.h>
32 #include <math/merge.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* These should only be changed for testing purposes. */
38 int min_buffers = 64;
39 int max_buffers = INT_MAX;
40
41 struct sort_writer
42   {
43     struct case_ordering *ordering;
44     struct merge *merge;
45     struct pqueue *pqueue;
46
47     struct casewriter *run;
48     casenumber run_id;
49     struct ccase run_end;
50   };
51
52 static struct casewriter_class sort_casewriter_class;
53
54 static struct pqueue *pqueue_create (const struct case_ordering *);
55 static void pqueue_destroy (struct pqueue *);
56 static bool pqueue_is_full (const struct pqueue *);
57 static bool pqueue_is_empty (const struct pqueue *);
58 static void pqueue_push (struct pqueue *, struct ccase *, casenumber);
59 static void pqueue_pop (struct pqueue *, struct ccase *, casenumber *);
60
61 static void output_record (struct sort_writer *);
62
63 struct casewriter *
64 sort_create_writer (struct case_ordering *ordering)
65 {
66   size_t value_cnt = case_ordering_get_value_cnt (ordering);
67   struct sort_writer *sort;
68
69   sort = xmalloc (sizeof *sort);
70   sort->ordering = case_ordering_clone (ordering);
71   sort->merge = merge_create (ordering);
72   sort->pqueue = pqueue_create (ordering);
73   sort->run = NULL;
74   sort->run_id = 0;
75   case_nullify (&sort->run_end);
76
77   case_ordering_destroy (ordering);
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               || case_ordering_compare_cases (c, &sort->run_end,
94                                               sort->ordering) < 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   case_ordering_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 (case_ordering_get_value_cnt (
121                                        sort->ordering));
122       sort->run_id = 1;
123     }
124   while (!pqueue_is_empty (sort->pqueue))
125     output_record (sort);
126
127   merge_append (sort->merge, casewriter_make_reader (sort->run));
128   sort->run = NULL;
129
130   output = merge_make_reader (sort->merge);
131   sort_casewriter_destroy (writer, sort);
132   return output;
133 }
134
135 static void
136 output_record (struct sort_writer *sort)
137 {
138   struct ccase min_case;
139   casenumber min_run_id;
140
141   pqueue_pop (sort->pqueue, &min_case, &min_run_id);
142 #if 0
143   printf ("\toutput: %f to run %d\n", case_num_idx (&min_case, 0), min_run_id);
144 #endif
145
146   if (sort->run_id != min_run_id && sort->run != NULL)
147     {
148       merge_append (sort->merge, casewriter_make_reader (sort->run));
149       sort->run = NULL;
150     }
151   if (sort->run == NULL)
152     {
153       sort->run = tmpfile_writer_create (case_ordering_get_value_cnt (
154                                            sort->ordering));
155       sort->run_id = min_run_id;
156     }
157
158   case_destroy (&sort->run_end);
159   case_clone (&sort->run_end, &min_case);
160
161   casewriter_write (sort->run, &min_case);
162 }
163
164 static struct casewriter_class sort_casewriter_class =
165   {
166     sort_casewriter_write,
167     sort_casewriter_destroy,
168     sort_casewriter_convert_to_reader,
169   };
170 \f
171 /* Reads all the cases from INPUT.  Sorts the cases according to
172    ORDERING.  Returns the sorted cases in a new casereader, or a
173    null pointer if an I/O error occurs.  Both INPUT and ORDERING
174    are destroyed upon return, regardless of success. */
175 struct casereader *
176 sort_execute (struct casereader *input, struct case_ordering *ordering)
177 {
178   struct casewriter *output = sort_create_writer (ordering);
179   casereader_transfer (input, output);
180   return casewriter_make_reader (output);
181 }
182 \f
183 struct pqueue
184   {
185     struct case_ordering *ordering;
186     struct pqueue_record *records;
187     size_t record_cnt;
188     size_t record_cap;
189     casenumber idx;
190   };
191
192 struct pqueue_record
193   {
194     casenumber id;
195     struct ccase c;
196     casenumber idx;
197   };
198
199 static int compare_pqueue_records_minheap (const void *a, const void *b,
200                                            const void *pq_);
201
202 static struct pqueue *
203 pqueue_create (const struct case_ordering *ordering)
204 {
205   struct pqueue *pq;
206
207   pq = xmalloc (sizeof *pq);
208   pq->ordering = case_ordering_clone (ordering);
209   pq->record_cap
210     = get_workspace_cases (case_ordering_get_value_cnt (ordering));
211   if (pq->record_cap > max_buffers)
212     pq->record_cap = max_buffers;
213   else if (pq->record_cap < min_buffers)
214     pq->record_cap = min_buffers;
215   pq->record_cnt = 0;
216   pq->records = xnmalloc (pq->record_cap, sizeof *pq->records);
217   pq->idx = 0;
218
219   return pq;
220 }
221
222 static void
223 pqueue_destroy (struct pqueue *pq)
224 {
225   if (pq != NULL)
226     {
227       while (!pqueue_is_empty (pq))
228         {
229           struct ccase c;
230           casenumber id;
231           pqueue_pop (pq, &c, &id);
232           case_destroy (&c);
233         }
234       case_ordering_destroy (pq->ordering);
235       free (pq->records);
236       free (pq);
237     }
238 }
239
240 static bool
241 pqueue_is_full (const struct pqueue *pq)
242 {
243   return pq->record_cnt >= pq->record_cap;
244 }
245
246 static bool
247 pqueue_is_empty (const struct pqueue *pq)
248 {
249   return pq->record_cnt == 0;
250 }
251
252 static void
253 pqueue_push (struct pqueue *pq, struct ccase *c, casenumber id)
254 {
255   struct pqueue_record *r;
256
257   assert (!pqueue_is_full (pq));
258
259   r = &pq->records[pq->record_cnt++];
260   r->id = id;
261   case_move (&r->c, c);
262   r->idx = pq->idx++;
263
264   push_heap (pq->records, pq->record_cnt, sizeof *pq->records,
265              compare_pqueue_records_minheap, pq);
266 }
267
268 static void
269 pqueue_pop (struct pqueue *pq, struct ccase *c, casenumber *id)
270 {
271   struct pqueue_record *r;
272
273   assert (!pqueue_is_empty (pq));
274
275   pop_heap (pq->records, pq->record_cnt--, sizeof *pq->records,
276             compare_pqueue_records_minheap, pq);
277
278   r = &pq->records[pq->record_cnt];
279   *id = r->id;
280   case_move (c, &r->c);
281 }
282
283 /* Compares record-run tuples A and B on id, then on case data,
284    then on insertion order, in descending order. */
285 static int
286 compare_pqueue_records_minheap (const void *a_, const void *b_,
287                                 const void *pq_)
288 {
289   const struct pqueue_record *a = a_;
290   const struct pqueue_record *b = b_;
291   const struct pqueue *pq = pq_;
292   int result = a->id < b->id ? -1 : a->id > b->id;
293   if (result == 0)
294     result = case_ordering_compare_cases (&a->c, &b->c, pq->ordering);
295   if (result == 0)
296     result = a->idx < b->idx ? -1 : a->idx > b->idx;
297   return -result;
298 }