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