Fixed bug reporting the significance of paired value t-test.
[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/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 case_ordering *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 case_ordering *, 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 (struct case_ordering *ordering, size_t value_cnt)
67 {
68   struct sort_writer *sort;
69
70   sort = xmalloc (sizeof *sort);
71   sort->value_cnt = value_cnt;
72   sort->ordering = case_ordering_clone (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   case_ordering_destroy (ordering);
80
81   return casewriter_create (value_cnt, &sort_casewriter_class, sort);
82 }
83
84 static void
85 sort_casewriter_write (struct casewriter *writer UNUSED, void *sort_,
86                        struct ccase *c)
87 {
88   struct sort_writer *sort = sort_;
89   bool next_run;
90
91   if (pqueue_is_full (sort->pqueue))
92     output_record (sort);
93
94   next_run = (case_is_null (&sort->run_end)
95               || case_ordering_compare_cases (c, &sort->run_end,
96                                               sort->ordering) < 0);
97   pqueue_push (sort->pqueue, c, sort->run_id + (next_run ? 1 : 0));
98 }
99
100 static void
101 sort_casewriter_destroy (struct casewriter *writer UNUSED, void *sort_)
102 {
103   struct sort_writer *sort = sort_;
104
105   case_ordering_destroy (sort->ordering);
106   merge_destroy (sort->merge);
107   pqueue_destroy (sort->pqueue);
108   casewriter_destroy (sort->run);
109   case_destroy (&sort->run_end);
110   free (sort);
111 }
112
113 static struct casereader *
114 sort_casewriter_convert_to_reader (struct casewriter *writer, void *sort_)
115 {
116   struct sort_writer *sort = sort_;
117   struct casereader *output;
118
119   if (sort->run == NULL && sort->run_id == 0)
120     {
121       /* In-core sort. */
122       sort->run = mem_writer_create (casewriter_get_value_cnt (writer));
123       sort->run_id = 1;
124     }
125   while (!pqueue_is_empty (sort->pqueue))
126     output_record (sort);
127
128   merge_append (sort->merge, casewriter_make_reader (sort->run));
129   sort->run = NULL;
130
131   output = merge_make_reader (sort->merge);
132   sort_casewriter_destroy (writer, sort);
133   return output;
134 }
135
136 static void
137 output_record (struct sort_writer *sort)
138 {
139   struct ccase min_case;
140   casenumber min_run_id;
141
142   pqueue_pop (sort->pqueue, &min_case, &min_run_id);
143 #if 0
144   printf ("\toutput: %f to run %d\n", case_num_idx (&min_case, 0), min_run_id);
145 #endif
146
147   if (sort->run_id != min_run_id && sort->run != NULL)
148     {
149       merge_append (sort->merge, casewriter_make_reader (sort->run));
150       sort->run = NULL;
151     }
152   if (sort->run == NULL)
153     {
154       sort->run = tmpfile_writer_create (sort->value_cnt);
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 =
179     sort_create_writer (ordering, casereader_get_value_cnt (input));
180   casereader_transfer (input, output);
181   return casewriter_make_reader (output);
182 }
183 \f
184 struct pqueue
185   {
186     struct case_ordering *ordering;
187     struct pqueue_record *records;
188     size_t record_cnt;
189     size_t record_cap;
190     casenumber idx;
191   };
192
193 struct pqueue_record
194   {
195     casenumber id;
196     struct ccase c;
197     casenumber idx;
198   };
199
200 static int compare_pqueue_records_minheap (const void *a, const void *b,
201                                            const void *pq_);
202
203 static struct pqueue *
204 pqueue_create (const struct case_ordering *ordering, size_t value_cnt)
205 {
206   struct pqueue *pq;
207
208   pq = xmalloc (sizeof *pq);
209   pq->ordering = case_ordering_clone (ordering);
210   pq->record_cap
211     = settings_get_workspace_cases (value_cnt);
212   if (pq->record_cap > max_buffers)
213     pq->record_cap = max_buffers;
214   else if (pq->record_cap < min_buffers)
215     pq->record_cap = min_buffers;
216   pq->record_cnt = 0;
217   pq->records = xnmalloc (pq->record_cap, sizeof *pq->records);
218   pq->idx = 0;
219
220   return pq;
221 }
222
223 static void
224 pqueue_destroy (struct pqueue *pq)
225 {
226   if (pq != NULL)
227     {
228       while (!pqueue_is_empty (pq))
229         {
230           struct ccase c;
231           casenumber id;
232           pqueue_pop (pq, &c, &id);
233           case_destroy (&c);
234         }
235       case_ordering_destroy (pq->ordering);
236       free (pq->records);
237       free (pq);
238     }
239 }
240
241 static bool
242 pqueue_is_full (const struct pqueue *pq)
243 {
244   return pq->record_cnt >= pq->record_cap;
245 }
246
247 static bool
248 pqueue_is_empty (const struct pqueue *pq)
249 {
250   return pq->record_cnt == 0;
251 }
252
253 static void
254 pqueue_push (struct pqueue *pq, struct ccase *c, casenumber id)
255 {
256   struct pqueue_record *r;
257
258   assert (!pqueue_is_full (pq));
259
260   r = &pq->records[pq->record_cnt++];
261   r->id = id;
262   case_move (&r->c, c);
263   r->idx = pq->idx++;
264
265   push_heap (pq->records, pq->record_cnt, sizeof *pq->records,
266              compare_pqueue_records_minheap, pq);
267 }
268
269 static void
270 pqueue_pop (struct pqueue *pq, struct ccase *c, casenumber *id)
271 {
272   struct pqueue_record *r;
273
274   assert (!pqueue_is_empty (pq));
275
276   pop_heap (pq->records, pq->record_cnt--, sizeof *pq->records,
277             compare_pqueue_records_minheap, pq);
278
279   r = &pq->records[pq->record_cnt];
280   *id = r->id;
281   case_move (c, &r->c);
282 }
283
284 /* Compares record-run tuples A and B on id, then on case data,
285    then on insertion order, in descending order. */
286 static int
287 compare_pqueue_records_minheap (const void *a_, const void *b_,
288                                 const void *pq_)
289 {
290   const struct pqueue_record *a = a_;
291   const struct pqueue_record *b = b_;
292   const struct pqueue *pq = pq_;
293   int result = a->id < b->id ? -1 : a->id > b->id;
294   if (result == 0)
295     result = case_ordering_compare_cases (&a->c, &b->c, pq->ordering);
296   if (result == 0)
297     result = a->idx < b->idx ? -1 : a->idx > b->idx;
298   return -result;
299 }