4851
|
1 /* |
7017
|
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 David Bateman |
4851
|
3 |
|
4 This file is part of Octave. |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
7016
|
8 Free Software Foundation; either version 3 of the License, or (at your |
|
9 option) any later version. |
4851
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
7016
|
17 along with Octave; see the file COPYING. If not, see |
|
18 <http://www.gnu.org/licenses/>. |
4851
|
19 |
|
20 Code stolen in large part from Python's, listobject.c, which itself had |
|
21 no license header. However, thanks to Tim Peters for the parts of the |
|
22 code I ripped-off. |
|
23 |
|
24 As required in the Python license the short description of the changes |
|
25 made are |
|
26 |
|
27 * convert the sorting code in listobject.cc into a generic class, |
|
28 replacing PyObject* with the type of the class T. |
|
29 |
|
30 The Python license is |
|
31 |
|
32 PSF LICENSE AGREEMENT FOR PYTHON 2.3 |
|
33 -------------------------------------- |
|
34 |
|
35 1. This LICENSE AGREEMENT is between the Python Software Foundation |
|
36 ("PSF"), and the Individual or Organization ("Licensee") accessing and |
|
37 otherwise using Python 2.3 software in source or binary form and its |
|
38 associated documentation. |
|
39 |
|
40 2. Subject to the terms and conditions of this License Agreement, PSF |
|
41 hereby grants Licensee a nonexclusive, royalty-free, world-wide |
|
42 license to reproduce, analyze, test, perform and/or display publicly, |
|
43 prepare derivative works, distribute, and otherwise use Python 2.3 |
|
44 alone or in any derivative version, provided, however, that PSF's |
|
45 License Agreement and PSF's notice of copyright, i.e., "Copyright (c) |
|
46 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are |
|
47 retained in Python 2.3 alone or in any derivative version prepared by |
|
48 Licensee. |
|
49 |
|
50 3. In the event Licensee prepares a derivative work that is based on |
|
51 or incorporates Python 2.3 or any part thereof, and wants to make |
|
52 the derivative work available to others as provided herein, then |
|
53 Licensee hereby agrees to include in any such work a brief summary of |
|
54 the changes made to Python 2.3. |
|
55 |
|
56 4. PSF is making Python 2.3 available to Licensee on an "AS IS" |
|
57 basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR |
|
58 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND |
|
59 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS |
|
60 FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT |
|
61 INFRINGE ANY THIRD PARTY RIGHTS. |
|
62 |
|
63 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON |
|
64 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS |
|
65 A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3, |
|
66 OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. |
|
67 |
|
68 6. This License Agreement will automatically terminate upon a material |
|
69 breach of its terms and conditions. |
|
70 |
|
71 7. Nothing in this License Agreement shall be deemed to create any |
|
72 relationship of agency, partnership, or joint venture between PSF and |
|
73 Licensee. This License Agreement does not grant permission to use PSF |
|
74 trademarks or trade name in a trademark sense to endorse or promote |
|
75 products or services of Licensee, or any third party. |
|
76 |
|
77 8. By copying, installing or otherwise using Python 2.3, Licensee |
|
78 agrees to be bound by the terms and conditions of this License |
|
79 Agreement. |
|
80 */ |
|
81 |
|
82 #ifdef HAVE_CONFIG_H |
|
83 #include <config.h> |
|
84 #endif |
|
85 |
5883
|
86 #include <cassert> |
6482
|
87 #include <cstdlib> |
5883
|
88 |
4851
|
89 #include "lo-mappers.h" |
|
90 #include "quit.h" |
|
91 #include "oct-sort.h" |
|
92 |
|
93 #define IFLT(a,b) if (compare == NULL ? ((a) < (b)) : compare ((a), (b))) |
|
94 |
|
95 template <class T> |
|
96 octave_sort<T>::octave_sort (void) : compare (NULL) |
|
97 { |
4998
|
98 merge_init (); |
4851
|
99 merge_getmem (1024); |
|
100 } |
|
101 |
|
102 template <class T> |
|
103 octave_sort<T>::octave_sort (bool (*comp) (T, T)) : compare (comp) |
|
104 { |
4998
|
105 merge_init (); |
4851
|
106 merge_getmem (1024); |
|
107 } |
|
108 |
|
109 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ |
|
110 template <class T> |
|
111 void |
|
112 octave_sort<T>::reverse_slice(T *lo, T *hi) |
|
113 { |
|
114 --hi; |
|
115 while (lo < hi) |
|
116 { |
|
117 T t = *lo; |
|
118 *lo = *hi; |
|
119 *hi = t; |
|
120 ++lo; |
|
121 --hi; |
|
122 } |
|
123 } |
|
124 |
|
125 template <class T> |
|
126 void |
|
127 octave_sort<T>::binarysort (T *lo, T *hi, T *start) |
|
128 { |
6959
|
129 T *l, *p, *r; |
|
130 T pivot; |
4851
|
131 |
|
132 if (lo == start) |
|
133 ++start; |
|
134 |
|
135 for (; start < hi; ++start) |
|
136 { |
|
137 /* set l to where *start belongs */ |
|
138 l = lo; |
|
139 r = start; |
|
140 pivot = *r; |
|
141 /* Invariants: |
|
142 * pivot >= all in [lo, l). |
|
143 * pivot < all in [r, start). |
|
144 * The second is vacuously true at the start. |
|
145 */ |
|
146 do |
|
147 { |
|
148 p = l + ((r - l) >> 1); |
|
149 IFLT (pivot, *p) |
|
150 r = p; |
|
151 else |
|
152 l = p+1; |
|
153 } |
|
154 while (l < r); |
|
155 /* The invariants still hold, so pivot >= all in [lo, l) and |
|
156 pivot < all in [l, start), so pivot belongs at l. Note |
|
157 that if there are elements equal to pivot, l points to the |
|
158 first slot after them -- that's why this sort is stable. |
|
159 Slide over to make room. |
|
160 Caution: using memmove is much slower under MSVC 5; |
|
161 we're not usually moving many slots. */ |
|
162 for (p = start; p > l; --p) |
|
163 *p = *(p-1); |
|
164 *l = pivot; |
|
165 } |
|
166 |
|
167 return; |
|
168 } |
|
169 |
|
170 /* |
|
171 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi |
|
172 is required on entry. "A run" is the longest ascending sequence, with |
|
173 |
|
174 lo[0] <= lo[1] <= lo[2] <= ... |
|
175 |
|
176 or the longest descending sequence, with |
|
177 |
|
178 lo[0] > lo[1] > lo[2] > ... |
|
179 |
|
180 Boolean *descending is set to 0 in the former case, or to 1 in the latter. |
|
181 For its intended use in a stable mergesort, the strictness of the defn of |
|
182 "descending" is needed so that the caller can safely reverse a descending |
|
183 sequence without violating stability (strict > ensures there are no equal |
|
184 elements to get out of order). |
|
185 |
|
186 Returns -1 in case of error. |
|
187 */ |
|
188 template <class T> |
|
189 int |
|
190 octave_sort<T>::count_run(T *lo, T *hi, int *descending) |
|
191 { |
|
192 int n; |
|
193 |
|
194 *descending = 0; |
|
195 ++lo; |
|
196 if (lo == hi) |
|
197 return 1; |
|
198 |
|
199 n = 2; |
|
200 |
|
201 IFLT (*lo, *(lo-1)) |
|
202 { |
|
203 *descending = 1; |
|
204 for (lo = lo+1; lo < hi; ++lo, ++n) |
|
205 { |
|
206 IFLT (*lo, *(lo-1)) |
|
207 ; |
|
208 else |
|
209 break; |
|
210 } |
|
211 } |
|
212 else |
|
213 { |
|
214 for (lo = lo+1; lo < hi; ++lo, ++n) |
|
215 { |
|
216 IFLT (*lo, *(lo-1)) |
|
217 break; |
|
218 } |
|
219 } |
|
220 |
|
221 return n; |
|
222 } |
|
223 |
|
224 /* |
|
225 Locate the proper position of key in a sorted vector; if the vector contains |
|
226 an element equal to key, return the position immediately to the left of |
|
227 the leftmost equal element. [gallop_right() does the same except returns |
|
228 the position to the right of the rightmost equal element (if any).] |
|
229 |
|
230 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0. |
|
231 |
|
232 "hint" is an index at which to begin the search, 0 <= hint < n. The closer |
|
233 hint is to the final result, the faster this runs. |
|
234 |
|
235 The return value is the int k in 0..n such that |
|
236 |
|
237 a[k-1] < key <= a[k] |
|
238 |
|
239 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW, |
|
240 key belongs at index k; or, IOW, the first k elements of a should precede |
|
241 key, and the last n-k should follow key. |
|
242 |
|
243 Returns -1 on error. See listsort.txt for info on the method. |
|
244 */ |
|
245 template <class T> |
|
246 int |
|
247 octave_sort<T>::gallop_left(T key, T *a, int n, int hint) |
|
248 { |
|
249 int ofs; |
|
250 int lastofs; |
|
251 int k; |
|
252 |
|
253 a += hint; |
|
254 lastofs = 0; |
|
255 ofs = 1; |
|
256 IFLT (*a, key) |
|
257 { |
|
258 /* a[hint] < key -- gallop right, until |
|
259 * a[hint + lastofs] < key <= a[hint + ofs] |
|
260 */ |
|
261 const int maxofs = n - hint; /* &a[n-1] is highest */ |
|
262 while (ofs < maxofs) |
|
263 { |
|
264 IFLT (a[ofs], key) |
|
265 { |
|
266 lastofs = ofs; |
|
267 ofs = (ofs << 1) + 1; |
|
268 if (ofs <= 0) /* int overflow */ |
|
269 ofs = maxofs; |
|
270 } |
|
271 else /* key <= a[hint + ofs] */ |
|
272 break; |
|
273 } |
|
274 if (ofs > maxofs) |
|
275 ofs = maxofs; |
|
276 /* Translate back to offsets relative to &a[0]. */ |
|
277 lastofs += hint; |
|
278 ofs += hint; |
|
279 } |
|
280 else |
|
281 { |
|
282 /* key <= a[hint] -- gallop left, until |
|
283 * a[hint - ofs] < key <= a[hint - lastofs] |
|
284 */ |
|
285 const int maxofs = hint + 1; /* &a[0] is lowest */ |
|
286 while (ofs < maxofs) |
|
287 { |
|
288 IFLT (*(a-ofs), key) |
|
289 break; |
|
290 /* key <= a[hint - ofs] */ |
|
291 lastofs = ofs; |
|
292 ofs = (ofs << 1) + 1; |
|
293 if (ofs <= 0) /* int overflow */ |
|
294 ofs = maxofs; |
|
295 } |
|
296 if (ofs > maxofs) |
|
297 ofs = maxofs; |
|
298 /* Translate back to positive offsets relative to &a[0]. */ |
|
299 k = lastofs; |
|
300 lastofs = hint - ofs; |
|
301 ofs = hint - k; |
|
302 } |
|
303 a -= hint; |
|
304 |
|
305 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the |
|
306 * right of lastofs but no farther right than ofs. Do a binary |
|
307 * search, with invariant a[lastofs-1] < key <= a[ofs]. |
|
308 */ |
|
309 ++lastofs; |
|
310 while (lastofs < ofs) |
|
311 { |
|
312 int m = lastofs + ((ofs - lastofs) >> 1); |
|
313 |
|
314 IFLT (a[m], key) |
|
315 lastofs = m+1; /* a[m] < key */ |
|
316 else |
|
317 ofs = m; /* key <= a[m] */ |
|
318 } |
|
319 return ofs; |
|
320 } |
|
321 |
|
322 /* |
|
323 Exactly like gallop_left(), except that if key already exists in a[0:n], |
|
324 finds the position immediately to the right of the rightmost equal value. |
|
325 |
|
326 The return value is the int k in 0..n such that |
|
327 |
|
328 a[k-1] <= key < a[k] |
|
329 |
|
330 or -1 if error. |
|
331 |
|
332 The code duplication is massive, but this is enough different given that |
|
333 we're sticking to "<" comparisons that it's much harder to follow if |
|
334 written as one routine with yet another "left or right?" flag. |
|
335 */ |
|
336 template <class T> |
|
337 int |
|
338 octave_sort<T>::gallop_right(T key, T *a, int n, int hint) |
|
339 { |
|
340 int ofs; |
|
341 int lastofs; |
|
342 int k; |
|
343 |
|
344 a += hint; |
|
345 lastofs = 0; |
|
346 ofs = 1; |
|
347 IFLT (key, *a) |
|
348 { |
|
349 /* key < a[hint] -- gallop left, until |
|
350 * a[hint - ofs] <= key < a[hint - lastofs] |
|
351 */ |
|
352 const int maxofs = hint + 1; /* &a[0] is lowest */ |
|
353 while (ofs < maxofs) |
|
354 { |
|
355 IFLT (key, *(a-ofs)) |
|
356 { |
|
357 lastofs = ofs; |
|
358 ofs = (ofs << 1) + 1; |
|
359 if (ofs <= 0) /* int overflow */ |
|
360 ofs = maxofs; |
|
361 } |
|
362 else /* a[hint - ofs] <= key */ |
|
363 break; |
|
364 } |
|
365 if (ofs > maxofs) |
|
366 ofs = maxofs; |
|
367 /* Translate back to positive offsets relative to &a[0]. */ |
|
368 k = lastofs; |
|
369 lastofs = hint - ofs; |
|
370 ofs = hint - k; |
|
371 } |
|
372 else |
|
373 { |
|
374 /* a[hint] <= key -- gallop right, until |
|
375 * a[hint + lastofs] <= key < a[hint + ofs] |
|
376 */ |
|
377 const int maxofs = n - hint; /* &a[n-1] is highest */ |
|
378 while (ofs < maxofs) |
|
379 { |
|
380 IFLT (key, a[ofs]) |
|
381 break; |
|
382 /* a[hint + ofs] <= key */ |
|
383 lastofs = ofs; |
|
384 ofs = (ofs << 1) + 1; |
|
385 if (ofs <= 0) /* int overflow */ |
|
386 ofs = maxofs; |
|
387 } |
|
388 if (ofs > maxofs) |
|
389 ofs = maxofs; |
|
390 /* Translate back to offsets relative to &a[0]. */ |
|
391 lastofs += hint; |
|
392 ofs += hint; |
|
393 } |
|
394 a -= hint; |
|
395 |
|
396 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the |
|
397 * right of lastofs but no farther right than ofs. Do a binary |
|
398 * search, with invariant a[lastofs-1] <= key < a[ofs]. |
|
399 */ |
|
400 ++lastofs; |
|
401 while (lastofs < ofs) |
|
402 { |
|
403 int m = lastofs + ((ofs - lastofs) >> 1); |
|
404 |
|
405 IFLT (key, a[m]) |
|
406 ofs = m; /* key < a[m] */ |
|
407 else |
|
408 lastofs = m+1; /* a[m] <= key */ |
|
409 } |
|
410 return ofs; |
|
411 } |
|
412 |
|
413 /* Conceptually a MergeState's constructor. */ |
|
414 template <class T> |
|
415 void |
|
416 octave_sort<T>::merge_init(void) |
|
417 { |
|
418 ms.a = NULL; |
|
419 ms.alloced = 0; |
|
420 ms.n = 0; |
|
421 ms.min_gallop = MIN_GALLOP; |
|
422 } |
|
423 |
|
424 /* Free all the temp memory owned by the MergeState. This must be called |
|
425 * when you're done with a MergeState, and may be called before then if |
|
426 * you want to free the temp memory early. |
|
427 */ |
|
428 template <class T> |
|
429 void |
|
430 octave_sort<T>::merge_freemem(void) |
|
431 { |
|
432 if (ms.a) |
|
433 free (ms.a); |
|
434 ms.alloced = 0; |
|
435 ms.a = NULL; |
|
436 } |
|
437 |
|
438 static inline int |
|
439 roundupsize(int n) |
|
440 { |
|
441 unsigned int nbits = 3; |
5760
|
442 unsigned int n2 = static_cast<unsigned int> (n) >> 8; |
4851
|
443 |
|
444 /* Round up: |
|
445 * If n < 256, to a multiple of 8. |
|
446 * If n < 2048, to a multiple of 64. |
|
447 * If n < 16384, to a multiple of 512. |
|
448 * If n < 131072, to a multiple of 4096. |
|
449 * If n < 1048576, to a multiple of 32768. |
|
450 * If n < 8388608, to a multiple of 262144. |
|
451 * If n < 67108864, to a multiple of 2097152. |
|
452 * If n < 536870912, to a multiple of 16777216. |
|
453 * ... |
|
454 * If n < 2**(5+3*i), to a multiple of 2**(3*i). |
|
455 * |
|
456 * This over-allocates proportional to the list size, making room |
|
457 * for additional growth. The over-allocation is mild, but is |
|
458 * enough to give linear-time amortized behavior over a long |
|
459 * sequence of appends() in the presence of a poorly-performing |
|
460 * system realloc() (which is a reality, e.g., across all flavors |
|
461 * of Windows, with Win9x behavior being particularly bad -- and |
|
462 * we've still got address space fragmentation problems on Win9x |
|
463 * even with this scheme, although it requires much longer lists to |
|
464 * provoke them than it used to). |
|
465 */ |
|
466 while (n2) { |
|
467 n2 >>= 3; |
|
468 nbits += 3; |
|
469 } |
|
470 return ((n >> nbits) + 1) << nbits; |
|
471 } |
|
472 |
|
473 /* Ensure enough temp memory for 'need' array slots is available. |
|
474 * Returns 0 on success and -1 if the memory can't be gotten. |
|
475 */ |
|
476 template <class T> |
|
477 int |
|
478 octave_sort<T>::merge_getmem(int need) |
|
479 { |
|
480 if (need <= ms.alloced) |
|
481 return 0; |
|
482 |
|
483 need = roundupsize(need); |
|
484 /* Don't realloc! That can cost cycles to copy the old data, but |
|
485 * we don't care what's in the block. |
|
486 */ |
|
487 merge_freemem( ); |
5760
|
488 ms.a = static_cast <T *> (malloc (need * sizeof (T))); |
4851
|
489 if (ms.a) { |
|
490 ms.alloced = need; |
|
491 return 0; |
|
492 } |
|
493 merge_freemem( ); /* reset to sane state */ |
|
494 return -1; |
|
495 } |
|
496 |
|
497 #define MERGE_GETMEM(NEED) ((NEED) <= ms.alloced ? 0 : \ |
|
498 merge_getmem(NEED)) |
|
499 |
|
500 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
501 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
502 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
503 * merge, and should have na <= nb. See listsort.txt for more info. |
|
504 * Return 0 if successful, -1 if error. |
|
505 */ |
|
506 template <class T> |
|
507 int |
|
508 octave_sort<T>::merge_lo(T *pa, int na, T *pb, int nb) |
|
509 { |
|
510 int k; |
|
511 T *dest; |
|
512 int result = -1; /* guilty until proved innocent */ |
|
513 int min_gallop = ms.min_gallop; |
|
514 |
|
515 if (MERGE_GETMEM(na) < 0) |
|
516 return -1; |
|
517 memcpy(ms.a, pa, na * sizeof(T)); |
|
518 dest = pa; |
|
519 pa = ms.a; |
|
520 |
|
521 *dest++ = *pb++; |
|
522 --nb; |
|
523 if (nb == 0) |
|
524 goto Succeed; |
|
525 if (na == 1) |
|
526 goto CopyB; |
|
527 |
|
528 for (;;) { |
|
529 int acount = 0; /* # of times A won in a row */ |
|
530 int bcount = 0; /* # of times B won in a row */ |
|
531 |
|
532 /* Do the straightforward thing until (if ever) one run |
|
533 * appears to win consistently. |
|
534 */ |
|
535 for (;;) { |
|
536 |
|
537 IFLT (*pb, *pa) |
|
538 { |
|
539 *dest++ = *pb++; |
|
540 ++bcount; |
|
541 acount = 0; |
|
542 --nb; |
|
543 if (nb == 0) |
|
544 goto Succeed; |
|
545 if (bcount >= min_gallop) |
|
546 break; |
|
547 } |
|
548 else |
|
549 { |
|
550 *dest++ = *pa++; |
|
551 ++acount; |
|
552 bcount = 0; |
|
553 --na; |
|
554 if (na == 1) |
|
555 goto CopyB; |
|
556 if (acount >= min_gallop) |
|
557 break; |
|
558 } |
|
559 } |
|
560 |
|
561 /* One run is winning so consistently that galloping may |
|
562 * be a huge win. So try that, and continue galloping until |
|
563 * (if ever) neither run appears to be winning consistently |
|
564 * anymore. |
|
565 */ |
|
566 ++min_gallop; |
|
567 do |
|
568 { |
|
569 min_gallop -= min_gallop > 1; |
|
570 ms.min_gallop = min_gallop; |
|
571 k = gallop_right(*pb, pa, na, 0); |
|
572 acount = k; |
|
573 if (k) |
|
574 { |
|
575 if (k < 0) |
|
576 goto Fail; |
|
577 memcpy(dest, pa, k * sizeof(T)); |
|
578 dest += k; |
|
579 pa += k; |
|
580 na -= k; |
|
581 if (na == 1) |
|
582 goto CopyB; |
|
583 /* na==0 is impossible now if the comparison |
|
584 * function is consistent, but we can't assume |
|
585 * that it is. |
|
586 */ |
|
587 if (na == 0) |
|
588 goto Succeed; |
|
589 } |
|
590 *dest++ = *pb++; |
|
591 --nb; |
|
592 if (nb == 0) |
|
593 goto Succeed; |
|
594 |
|
595 k = gallop_left(*pa, pb, nb, 0); |
|
596 bcount = k; |
|
597 if (k) { |
|
598 if (k < 0) |
|
599 goto Fail; |
|
600 memmove(dest, pb, k * sizeof(T)); |
|
601 dest += k; |
|
602 pb += k; |
|
603 nb -= k; |
|
604 if (nb == 0) |
|
605 goto Succeed; |
|
606 } |
|
607 *dest++ = *pa++; |
|
608 --na; |
|
609 if (na == 1) |
|
610 goto CopyB; |
|
611 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
612 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
613 ms.min_gallop = min_gallop; |
|
614 } |
|
615 Succeed: |
|
616 result = 0; |
|
617 Fail: |
|
618 if (na) |
|
619 memcpy(dest, pa, na * sizeof(T)); |
|
620 return result; |
|
621 CopyB: |
|
622 /* The last element of pa belongs at the end of the merge. */ |
|
623 memmove(dest, pb, nb * sizeof(T)); |
|
624 dest[nb] = *pa; |
|
625 return 0; |
|
626 } |
|
627 |
|
628 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
629 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
630 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
631 * merge, and should have na >= nb. See listsort.txt for more info. |
|
632 * Return 0 if successful, -1 if error. |
|
633 */ |
|
634 template <class T> |
|
635 int |
|
636 octave_sort<T>::merge_hi(T *pa, int na, T *pb, int nb) |
|
637 { |
|
638 int k; |
|
639 T *dest; |
|
640 int result = -1; /* guilty until proved innocent */ |
|
641 T *basea; |
|
642 T *baseb; |
|
643 int min_gallop = ms.min_gallop; |
|
644 |
|
645 if (MERGE_GETMEM(nb) < 0) |
|
646 return -1; |
|
647 dest = pb + nb - 1; |
|
648 memcpy(ms.a, pb, nb * sizeof(T)); |
|
649 basea = pa; |
|
650 baseb = ms.a; |
|
651 pb = ms.a + nb - 1; |
|
652 pa += na - 1; |
|
653 |
|
654 *dest-- = *pa--; |
|
655 --na; |
|
656 if (na == 0) |
|
657 goto Succeed; |
|
658 if (nb == 1) |
|
659 goto CopyA; |
|
660 |
|
661 for (;;) |
|
662 { |
|
663 int acount = 0; /* # of times A won in a row */ |
|
664 int bcount = 0; /* # of times B won in a row */ |
|
665 |
|
666 /* Do the straightforward thing until (if ever) one run |
|
667 * appears to win consistently. |
|
668 */ |
|
669 for (;;) |
|
670 { |
|
671 IFLT (*pb, *pa) |
|
672 { |
|
673 *dest-- = *pa--; |
|
674 ++acount; |
|
675 bcount = 0; |
|
676 --na; |
|
677 if (na == 0) |
|
678 goto Succeed; |
|
679 if (acount >= min_gallop) |
|
680 break; |
|
681 } |
|
682 else |
|
683 { |
|
684 *dest-- = *pb--; |
|
685 ++bcount; |
|
686 acount = 0; |
|
687 --nb; |
|
688 if (nb == 1) |
|
689 goto CopyA; |
|
690 if (bcount >= min_gallop) |
|
691 break; |
|
692 } |
|
693 } |
|
694 |
|
695 /* One run is winning so consistently that galloping may |
|
696 * be a huge win. So try that, and continue galloping until |
|
697 * (if ever) neither run appears to be winning consistently |
|
698 * anymore. |
|
699 */ |
|
700 ++min_gallop; |
|
701 do |
|
702 { |
|
703 min_gallop -= min_gallop > 1; |
|
704 ms.min_gallop = min_gallop; |
|
705 k = gallop_right(*pb, basea, na, na-1); |
|
706 if (k < 0) |
|
707 goto Fail; |
|
708 k = na - k; |
|
709 acount = k; |
|
710 if (k) |
|
711 { |
|
712 dest -= k; |
|
713 pa -= k; |
|
714 memmove(dest+1, pa+1, k * sizeof(T )); |
|
715 na -= k; |
|
716 if (na == 0) |
|
717 goto Succeed; |
|
718 } |
|
719 *dest-- = *pb--; |
|
720 --nb; |
|
721 if (nb == 1) |
|
722 goto CopyA; |
|
723 |
|
724 k = gallop_left(*pa, baseb, nb, nb-1); |
|
725 if (k < 0) |
|
726 goto Fail; |
|
727 k = nb - k; |
|
728 bcount = k; |
|
729 if (k) |
|
730 { |
|
731 dest -= k; |
|
732 pb -= k; |
|
733 memcpy(dest+1, pb+1, k * sizeof(T)); |
|
734 nb -= k; |
|
735 if (nb == 1) |
|
736 goto CopyA; |
|
737 /* nb==0 is impossible now if the comparison |
|
738 * function is consistent, but we can't assume |
|
739 * that it is. |
|
740 */ |
|
741 if (nb == 0) |
|
742 goto Succeed; |
|
743 } |
|
744 *dest-- = *pa--; |
|
745 --na; |
|
746 if (na == 0) |
|
747 goto Succeed; |
|
748 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
749 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
750 ms.min_gallop = min_gallop; |
|
751 } |
|
752 Succeed: |
|
753 result = 0; |
|
754 Fail: |
|
755 if (nb) |
|
756 memcpy(dest-(nb-1), baseb, nb * sizeof(T)); |
|
757 return result; |
|
758 CopyA: |
|
759 /* The first element of pb belongs at the front of the merge. */ |
|
760 dest -= na; |
|
761 pa -= na; |
|
762 memmove(dest+1, pa+1, na * sizeof(T)); |
|
763 *dest = *pb; |
|
764 return 0; |
|
765 } |
|
766 |
|
767 /* Merge the two runs at stack indices i and i+1. |
|
768 * Returns 0 on success, -1 on error. |
|
769 */ |
|
770 template <class T> |
|
771 int |
|
772 octave_sort<T>::merge_at(int i) |
|
773 { |
|
774 T *pa, *pb; |
|
775 int na, nb; |
|
776 int k; |
|
777 |
|
778 pa = ms.pending[i].base; |
|
779 na = ms.pending[i].len; |
|
780 pb = ms.pending[i+1].base; |
|
781 nb = ms.pending[i+1].len; |
|
782 |
|
783 /* Record the length of the combined runs; if i is the 3rd-last |
|
784 * run now, also slide over the last run (which isn't involved |
|
785 * in this merge). The current run i+1 goes away in any case. |
|
786 */ |
|
787 ms.pending[i].len = na + nb; |
|
788 if (i == ms.n - 3) |
|
789 ms.pending[i+1] = ms.pending[i+2]; |
|
790 --ms.n; |
|
791 |
|
792 /* Where does b start in a? Elements in a before that can be |
|
793 * ignored (already in place). |
|
794 */ |
|
795 k = gallop_right(*pb, pa, na, 0); |
|
796 if (k < 0) |
|
797 return -1; |
|
798 pa += k; |
|
799 na -= k; |
|
800 if (na == 0) |
|
801 return 0; |
|
802 |
|
803 /* Where does a end in b? Elements in b after that can be |
|
804 * ignored (already in place). |
|
805 */ |
|
806 nb = gallop_left(pa[na-1], pb, nb, nb-1); |
|
807 if (nb <= 0) |
|
808 return nb; |
|
809 |
|
810 /* Merge what remains of the runs, using a temp array with |
|
811 * min(na, nb) elements. |
|
812 */ |
|
813 if (na <= nb) |
|
814 return merge_lo(pa, na, pb, nb); |
|
815 else |
|
816 return merge_hi(pa, na, pb, nb); |
|
817 } |
|
818 |
|
819 /* Examine the stack of runs waiting to be merged, merging adjacent runs |
|
820 * until the stack invariants are re-established: |
|
821 * |
|
822 * 1. len[-3] > len[-2] + len[-1] |
|
823 * 2. len[-2] > len[-1] |
|
824 * |
|
825 * See listsort.txt for more info. |
|
826 * |
|
827 * Returns 0 on success, -1 on error. |
|
828 */ |
|
829 template <class T> |
|
830 int |
|
831 octave_sort<T>::merge_collapse(void) |
|
832 { |
|
833 struct s_slice *p = ms.pending; |
|
834 |
|
835 while (ms.n > 1) |
|
836 { |
|
837 int n = ms.n - 2; |
|
838 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) |
|
839 { |
|
840 if (p[n-1].len < p[n+1].len) |
|
841 --n; |
|
842 if (merge_at(n) < 0) |
|
843 return -1; |
|
844 } |
|
845 else if (p[n].len <= p[n+1].len) |
|
846 { |
|
847 if (merge_at(n) < 0) |
|
848 return -1; |
|
849 } |
|
850 else |
|
851 break; |
|
852 } |
|
853 return 0; |
|
854 } |
|
855 |
|
856 /* Regardless of invariants, merge all runs on the stack until only one |
|
857 * remains. This is used at the end of the mergesort. |
|
858 * |
|
859 * Returns 0 on success, -1 on error. |
|
860 */ |
|
861 template <class T> |
|
862 int |
|
863 octave_sort<T>::merge_force_collapse(void) |
|
864 { |
|
865 struct s_slice *p = ms.pending; |
|
866 |
|
867 while (ms.n > 1) |
|
868 { |
|
869 int n = ms.n - 2; |
|
870 if (n > 0 && p[n-1].len < p[n+1].len) |
|
871 --n; |
|
872 if (merge_at(n) < 0) |
|
873 return -1; |
|
874 } |
|
875 return 0; |
|
876 } |
|
877 |
|
878 /* Compute a good value for the minimum run length; natural runs shorter |
|
879 * than this are boosted artificially via binary insertion. |
|
880 * |
|
881 * If n < 64, return n (it's too small to bother with fancy stuff). |
|
882 * Else if n is an exact power of 2, return 32. |
|
883 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but |
|
884 * strictly less than, an exact power of 2. |
|
885 * |
|
886 * See listsort.txt for more info. |
|
887 */ |
|
888 template <class T> |
|
889 int |
|
890 octave_sort<T>::merge_compute_minrun(int n) |
|
891 { |
|
892 int r = 0; /* becomes 1 if any 1 bits are shifted off */ |
|
893 |
|
894 while (n >= 64) { |
|
895 r |= n & 1; |
|
896 n >>= 1; |
|
897 } |
|
898 return n + r; |
|
899 } |
|
900 |
|
901 template <class T> |
|
902 void |
|
903 octave_sort<T>::sort (T *v, int elements) |
|
904 { |
|
905 /* Re-initialize the Mergestate as this might be the second time called */ |
|
906 ms.n = 0; |
|
907 ms.min_gallop = MIN_GALLOP; |
|
908 |
|
909 if (elements > 1) |
|
910 { |
|
911 int nremaining = elements; |
|
912 T *lo = v; |
|
913 T *hi = v + elements; |
|
914 |
|
915 /* March over the array once, left to right, finding natural runs, |
|
916 * and extending short natural runs to minrun elements. |
|
917 */ |
|
918 int minrun = merge_compute_minrun(nremaining); |
|
919 do |
|
920 { |
|
921 int descending; |
|
922 int n; |
|
923 |
|
924 /* Identify next run. */ |
|
925 n = count_run(lo, hi, &descending); |
|
926 if (n < 0) |
|
927 goto fail; |
|
928 if (descending) |
|
929 reverse_slice(lo, lo + n); |
|
930 /* If short, extend to min(minrun, nremaining). */ |
|
931 if (n < minrun) |
|
932 { |
|
933 const int force = nremaining <= minrun ? nremaining : minrun; |
|
934 binarysort(lo, lo + force, lo + n); |
|
935 n = force; |
|
936 } |
|
937 /* Push run onto pending-runs stack, and maybe merge. */ |
|
938 assert(ms.n < MAX_MERGE_PENDING); |
|
939 ms.pending[ms.n].base = lo; |
|
940 ms.pending[ms.n].len = n; |
|
941 ++ms.n; |
|
942 if (merge_collapse( ) < 0) |
|
943 goto fail; |
|
944 /* Advance to find next run. */ |
|
945 lo += n; |
|
946 nremaining -= n; |
|
947 } while (nremaining); |
|
948 |
|
949 merge_force_collapse( ); |
|
950 } |
|
951 |
|
952 fail: |
|
953 return; |
|
954 } |
|
955 |
|
956 /* |
|
957 ;;; Local Variables: *** |
|
958 ;;; mode: C++ *** |
|
959 ;;; End: *** |
|
960 */ |