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 |
7234
|
93 #define IFLT(a,b) if (compare ? compare ((a), (b)) : ((a) < (b))) |
4851
|
94 |
|
95 template <class T> |
7234
|
96 octave_sort<T>::octave_sort (void) : compare (0) |
4851
|
97 { |
7234
|
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 |
7234
|
112 octave_sort<T>::reverse_slice (T *lo, T *hi) |
4851
|
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 |
7234
|
190 octave_sort<T>::count_run (T *lo, T *hi, int *descending) |
4851
|
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 |
7234
|
247 octave_sort<T>::gallop_left (T key, T *a, int n, int hint) |
4851
|
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 } |
7234
|
319 |
4851
|
320 return ofs; |
|
321 } |
|
322 |
|
323 /* |
|
324 Exactly like gallop_left(), except that if key already exists in a[0:n], |
|
325 finds the position immediately to the right of the rightmost equal value. |
|
326 |
|
327 The return value is the int k in 0..n such that |
|
328 |
|
329 a[k-1] <= key < a[k] |
|
330 |
|
331 or -1 if error. |
|
332 |
|
333 The code duplication is massive, but this is enough different given that |
|
334 we're sticking to "<" comparisons that it's much harder to follow if |
|
335 written as one routine with yet another "left or right?" flag. |
|
336 */ |
|
337 template <class T> |
|
338 int |
7234
|
339 octave_sort<T>::gallop_right (T key, T *a, int n, int hint) |
4851
|
340 { |
|
341 int ofs; |
|
342 int lastofs; |
|
343 int k; |
|
344 |
|
345 a += hint; |
|
346 lastofs = 0; |
|
347 ofs = 1; |
|
348 IFLT (key, *a) |
|
349 { |
|
350 /* key < a[hint] -- gallop left, until |
|
351 * a[hint - ofs] <= key < a[hint - lastofs] |
|
352 */ |
|
353 const int maxofs = hint + 1; /* &a[0] is lowest */ |
|
354 while (ofs < maxofs) |
|
355 { |
|
356 IFLT (key, *(a-ofs)) |
|
357 { |
|
358 lastofs = ofs; |
|
359 ofs = (ofs << 1) + 1; |
|
360 if (ofs <= 0) /* int overflow */ |
|
361 ofs = maxofs; |
|
362 } |
|
363 else /* a[hint - ofs] <= key */ |
|
364 break; |
|
365 } |
|
366 if (ofs > maxofs) |
|
367 ofs = maxofs; |
|
368 /* Translate back to positive offsets relative to &a[0]. */ |
|
369 k = lastofs; |
|
370 lastofs = hint - ofs; |
|
371 ofs = hint - k; |
|
372 } |
|
373 else |
|
374 { |
|
375 /* a[hint] <= key -- gallop right, until |
|
376 * a[hint + lastofs] <= key < a[hint + ofs] |
|
377 */ |
|
378 const int maxofs = n - hint; /* &a[n-1] is highest */ |
|
379 while (ofs < maxofs) |
|
380 { |
|
381 IFLT (key, a[ofs]) |
|
382 break; |
|
383 /* a[hint + ofs] <= key */ |
|
384 lastofs = ofs; |
|
385 ofs = (ofs << 1) + 1; |
|
386 if (ofs <= 0) /* int overflow */ |
|
387 ofs = maxofs; |
|
388 } |
|
389 if (ofs > maxofs) |
|
390 ofs = maxofs; |
|
391 /* Translate back to offsets relative to &a[0]. */ |
|
392 lastofs += hint; |
|
393 ofs += hint; |
|
394 } |
|
395 a -= hint; |
|
396 |
|
397 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the |
|
398 * right of lastofs but no farther right than ofs. Do a binary |
|
399 * search, with invariant a[lastofs-1] <= key < a[ofs]. |
|
400 */ |
|
401 ++lastofs; |
|
402 while (lastofs < ofs) |
|
403 { |
|
404 int m = lastofs + ((ofs - lastofs) >> 1); |
|
405 |
|
406 IFLT (key, a[m]) |
|
407 ofs = m; /* key < a[m] */ |
|
408 else |
|
409 lastofs = m+1; /* a[m] <= key */ |
|
410 } |
7234
|
411 |
4851
|
412 return ofs; |
|
413 } |
|
414 |
|
415 /* Conceptually a MergeState's constructor. */ |
|
416 template <class T> |
|
417 void |
7234
|
418 octave_sort<T>::merge_init (void) |
4851
|
419 { |
7234
|
420 ms.a = 0; |
4851
|
421 ms.alloced = 0; |
|
422 ms.n = 0; |
|
423 ms.min_gallop = MIN_GALLOP; |
|
424 } |
|
425 |
|
426 /* Free all the temp memory owned by the MergeState. This must be called |
|
427 * when you're done with a MergeState, and may be called before then if |
|
428 * you want to free the temp memory early. |
|
429 */ |
|
430 template <class T> |
|
431 void |
7234
|
432 octave_sort<T>::merge_freemem (void) |
4851
|
433 { |
|
434 if (ms.a) |
|
435 free (ms.a); |
|
436 ms.alloced = 0; |
7234
|
437 ms.a = 0; |
4851
|
438 } |
|
439 |
|
440 static inline int |
7234
|
441 roundupsize (int n) |
4851
|
442 { |
|
443 unsigned int nbits = 3; |
5760
|
444 unsigned int n2 = static_cast<unsigned int> (n) >> 8; |
4851
|
445 |
|
446 /* Round up: |
|
447 * If n < 256, to a multiple of 8. |
|
448 * If n < 2048, to a multiple of 64. |
|
449 * If n < 16384, to a multiple of 512. |
|
450 * If n < 131072, to a multiple of 4096. |
|
451 * If n < 1048576, to a multiple of 32768. |
|
452 * If n < 8388608, to a multiple of 262144. |
|
453 * If n < 67108864, to a multiple of 2097152. |
|
454 * If n < 536870912, to a multiple of 16777216. |
|
455 * ... |
|
456 * If n < 2**(5+3*i), to a multiple of 2**(3*i). |
|
457 * |
|
458 * This over-allocates proportional to the list size, making room |
|
459 * for additional growth. The over-allocation is mild, but is |
|
460 * enough to give linear-time amortized behavior over a long |
|
461 * sequence of appends() in the presence of a poorly-performing |
|
462 * system realloc() (which is a reality, e.g., across all flavors |
|
463 * of Windows, with Win9x behavior being particularly bad -- and |
|
464 * we've still got address space fragmentation problems on Win9x |
|
465 * even with this scheme, although it requires much longer lists to |
|
466 * provoke them than it used to). |
|
467 */ |
7234
|
468 while (n2) |
|
469 { |
|
470 n2 >>= 3; |
|
471 nbits += 3; |
|
472 } |
|
473 |
4851
|
474 return ((n >> nbits) + 1) << nbits; |
|
475 } |
|
476 |
|
477 /* Ensure enough temp memory for 'need' array slots is available. |
|
478 * Returns 0 on success and -1 if the memory can't be gotten. |
|
479 */ |
|
480 template <class T> |
|
481 int |
7234
|
482 octave_sort<T>::merge_getmem (int need) |
4851
|
483 { |
|
484 if (need <= ms.alloced) |
|
485 return 0; |
|
486 |
7234
|
487 need = roundupsize (need); |
4851
|
488 /* Don't realloc! That can cost cycles to copy the old data, but |
|
489 * we don't care what's in the block. |
|
490 */ |
7234
|
491 merge_freemem (); |
5760
|
492 ms.a = static_cast <T *> (malloc (need * sizeof (T))); |
7234
|
493 if (ms.a) |
|
494 { |
|
495 ms.alloced = need; |
|
496 return 0; |
|
497 } |
|
498 merge_freemem (); /* reset to sane state */ |
|
499 |
4851
|
500 return -1; |
|
501 } |
|
502 |
7234
|
503 #define MERGE_GETMEM(NEED) ((NEED) <= ms.alloced ? 0 : merge_getmem (NEED)) |
4851
|
504 |
|
505 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
506 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
507 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
508 * merge, and should have na <= nb. See listsort.txt for more info. |
|
509 * Return 0 if successful, -1 if error. |
|
510 */ |
|
511 template <class T> |
|
512 int |
7234
|
513 octave_sort<T>::merge_lo (T *pa, int na, T *pb, int nb) |
4851
|
514 { |
|
515 int k; |
|
516 T *dest; |
|
517 int result = -1; /* guilty until proved innocent */ |
|
518 int min_gallop = ms.min_gallop; |
|
519 |
7234
|
520 if (MERGE_GETMEM (na) < 0) |
4851
|
521 return -1; |
7234
|
522 memcpy (ms.a, pa, na * sizeof (T)); |
4851
|
523 dest = pa; |
|
524 pa = ms.a; |
|
525 |
|
526 *dest++ = *pb++; |
|
527 --nb; |
|
528 if (nb == 0) |
|
529 goto Succeed; |
|
530 if (na == 1) |
|
531 goto CopyB; |
|
532 |
7234
|
533 for (;;) |
|
534 { |
|
535 int acount = 0; /* # of times A won in a row */ |
|
536 int bcount = 0; /* # of times B won in a row */ |
|
537 |
|
538 /* Do the straightforward thing until (if ever) one run |
|
539 * appears to win consistently. |
|
540 */ |
|
541 for (;;) |
|
542 { |
4851
|
543 |
7234
|
544 IFLT (*pb, *pa) |
|
545 { |
|
546 *dest++ = *pb++; |
|
547 ++bcount; |
|
548 acount = 0; |
|
549 --nb; |
|
550 if (nb == 0) |
|
551 goto Succeed; |
|
552 if (bcount >= min_gallop) |
|
553 break; |
|
554 } |
|
555 else |
|
556 { |
|
557 *dest++ = *pa++; |
|
558 ++acount; |
|
559 bcount = 0; |
|
560 --na; |
|
561 if (na == 1) |
|
562 goto CopyB; |
|
563 if (acount >= min_gallop) |
|
564 break; |
|
565 } |
|
566 } |
4851
|
567 |
7234
|
568 /* One run is winning so consistently that galloping may |
|
569 * be a huge win. So try that, and continue galloping until |
|
570 * (if ever) neither run appears to be winning consistently |
|
571 * anymore. |
|
572 */ |
|
573 ++min_gallop; |
|
574 do |
4851
|
575 { |
7234
|
576 min_gallop -= min_gallop > 1; |
|
577 ms.min_gallop = min_gallop; |
|
578 k = gallop_right (*pb, pa, na, 0); |
|
579 acount = k; |
|
580 if (k) |
|
581 { |
|
582 if (k < 0) |
|
583 goto Fail; |
|
584 memcpy (dest, pa, k * sizeof (T)); |
|
585 dest += k; |
|
586 pa += k; |
|
587 na -= k; |
|
588 if (na == 1) |
|
589 goto CopyB; |
|
590 /* na==0 is impossible now if the comparison |
|
591 * function is consistent, but we can't assume |
|
592 * that it is. |
|
593 */ |
|
594 if (na == 0) |
|
595 goto Succeed; |
|
596 } |
4851
|
597 *dest++ = *pb++; |
|
598 --nb; |
|
599 if (nb == 0) |
|
600 goto Succeed; |
7234
|
601 |
|
602 k = gallop_left (*pa, pb, nb, 0); |
|
603 bcount = k; |
|
604 if (k) |
|
605 { |
|
606 if (k < 0) |
|
607 goto Fail; |
|
608 memmove (dest, pb, k * sizeof (T)); |
|
609 dest += k; |
|
610 pb += k; |
|
611 nb -= k; |
|
612 if (nb == 0) |
|
613 goto Succeed; |
|
614 } |
4851
|
615 *dest++ = *pa++; |
|
616 --na; |
|
617 if (na == 1) |
|
618 goto CopyB; |
|
619 } |
7234
|
620 while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
621 |
|
622 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
623 ms.min_gallop = min_gallop; |
4851
|
624 } |
|
625 |
|
626 Succeed: |
|
627 result = 0; |
7234
|
628 |
4851
|
629 Fail: |
|
630 if (na) |
7234
|
631 memcpy (dest, pa, na * sizeof (T)); |
4851
|
632 return result; |
7234
|
633 |
4851
|
634 CopyB: |
|
635 /* The last element of pa belongs at the end of the merge. */ |
7234
|
636 memmove (dest, pb, nb * sizeof (T)); |
4851
|
637 dest[nb] = *pa; |
7234
|
638 |
4851
|
639 return 0; |
|
640 } |
|
641 |
|
642 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
643 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
644 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
645 * merge, and should have na >= nb. See listsort.txt for more info. |
|
646 * Return 0 if successful, -1 if error. |
|
647 */ |
|
648 template <class T> |
|
649 int |
7234
|
650 octave_sort<T>::merge_hi (T *pa, int na, T *pb, int nb) |
4851
|
651 { |
|
652 int k; |
|
653 T *dest; |
|
654 int result = -1; /* guilty until proved innocent */ |
|
655 T *basea; |
|
656 T *baseb; |
|
657 int min_gallop = ms.min_gallop; |
|
658 |
7234
|
659 if (MERGE_GETMEM (nb) < 0) |
4851
|
660 return -1; |
|
661 dest = pb + nb - 1; |
7234
|
662 memcpy (ms.a, pb, nb * sizeof (T)); |
4851
|
663 basea = pa; |
|
664 baseb = ms.a; |
|
665 pb = ms.a + nb - 1; |
|
666 pa += na - 1; |
|
667 |
|
668 *dest-- = *pa--; |
|
669 --na; |
|
670 if (na == 0) |
|
671 goto Succeed; |
|
672 if (nb == 1) |
|
673 goto CopyA; |
|
674 |
|
675 for (;;) |
|
676 { |
|
677 int acount = 0; /* # of times A won in a row */ |
|
678 int bcount = 0; /* # of times B won in a row */ |
|
679 |
|
680 /* Do the straightforward thing until (if ever) one run |
|
681 * appears to win consistently. |
|
682 */ |
|
683 for (;;) |
|
684 { |
|
685 IFLT (*pb, *pa) |
|
686 { |
|
687 *dest-- = *pa--; |
|
688 ++acount; |
|
689 bcount = 0; |
|
690 --na; |
|
691 if (na == 0) |
|
692 goto Succeed; |
|
693 if (acount >= min_gallop) |
|
694 break; |
|
695 } |
|
696 else |
|
697 { |
|
698 *dest-- = *pb--; |
|
699 ++bcount; |
|
700 acount = 0; |
|
701 --nb; |
|
702 if (nb == 1) |
|
703 goto CopyA; |
|
704 if (bcount >= min_gallop) |
|
705 break; |
|
706 } |
|
707 } |
|
708 |
|
709 /* One run is winning so consistently that galloping may |
|
710 * be a huge win. So try that, and continue galloping until |
|
711 * (if ever) neither run appears to be winning consistently |
|
712 * anymore. |
|
713 */ |
|
714 ++min_gallop; |
|
715 do |
|
716 { |
|
717 min_gallop -= min_gallop > 1; |
|
718 ms.min_gallop = min_gallop; |
7234
|
719 k = gallop_right (*pb, basea, na, na-1); |
4851
|
720 if (k < 0) |
|
721 goto Fail; |
|
722 k = na - k; |
|
723 acount = k; |
|
724 if (k) |
|
725 { |
|
726 dest -= k; |
|
727 pa -= k; |
7234
|
728 memmove (dest+1, pa+1, k * sizeof (T)); |
4851
|
729 na -= k; |
|
730 if (na == 0) |
|
731 goto Succeed; |
|
732 } |
|
733 *dest-- = *pb--; |
|
734 --nb; |
|
735 if (nb == 1) |
|
736 goto CopyA; |
|
737 |
7234
|
738 k = gallop_left (*pa, baseb, nb, nb-1); |
4851
|
739 if (k < 0) |
|
740 goto Fail; |
|
741 k = nb - k; |
|
742 bcount = k; |
|
743 if (k) |
|
744 { |
|
745 dest -= k; |
|
746 pb -= k; |
7234
|
747 memcpy (dest+1, pb+1, k * sizeof (T)); |
4851
|
748 nb -= k; |
|
749 if (nb == 1) |
|
750 goto CopyA; |
|
751 /* nb==0 is impossible now if the comparison |
|
752 * function is consistent, but we can't assume |
|
753 * that it is. |
|
754 */ |
|
755 if (nb == 0) |
|
756 goto Succeed; |
|
757 } |
|
758 *dest-- = *pa--; |
|
759 --na; |
|
760 if (na == 0) |
|
761 goto Succeed; |
|
762 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
763 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
764 ms.min_gallop = min_gallop; |
|
765 } |
7234
|
766 |
4851
|
767 Succeed: |
|
768 result = 0; |
7234
|
769 |
4851
|
770 Fail: |
|
771 if (nb) |
7234
|
772 memcpy (dest-(nb-1), baseb, nb * sizeof (T)); |
4851
|
773 return result; |
7234
|
774 |
4851
|
775 CopyA: |
|
776 /* The first element of pb belongs at the front of the merge. */ |
|
777 dest -= na; |
|
778 pa -= na; |
7234
|
779 memmove (dest+1, pa+1, na * sizeof (T)); |
4851
|
780 *dest = *pb; |
7234
|
781 |
4851
|
782 return 0; |
|
783 } |
|
784 |
|
785 /* Merge the two runs at stack indices i and i+1. |
|
786 * Returns 0 on success, -1 on error. |
|
787 */ |
|
788 template <class T> |
|
789 int |
7234
|
790 octave_sort<T>::merge_at (int i) |
4851
|
791 { |
|
792 T *pa, *pb; |
|
793 int na, nb; |
|
794 int k; |
|
795 |
|
796 pa = ms.pending[i].base; |
|
797 na = ms.pending[i].len; |
|
798 pb = ms.pending[i+1].base; |
|
799 nb = ms.pending[i+1].len; |
|
800 |
|
801 /* Record the length of the combined runs; if i is the 3rd-last |
|
802 * run now, also slide over the last run (which isn't involved |
|
803 * in this merge). The current run i+1 goes away in any case. |
|
804 */ |
|
805 ms.pending[i].len = na + nb; |
|
806 if (i == ms.n - 3) |
|
807 ms.pending[i+1] = ms.pending[i+2]; |
|
808 --ms.n; |
|
809 |
|
810 /* Where does b start in a? Elements in a before that can be |
|
811 * ignored (already in place). |
|
812 */ |
7234
|
813 k = gallop_right (*pb, pa, na, 0); |
4851
|
814 if (k < 0) |
|
815 return -1; |
|
816 pa += k; |
|
817 na -= k; |
|
818 if (na == 0) |
|
819 return 0; |
|
820 |
|
821 /* Where does a end in b? Elements in b after that can be |
|
822 * ignored (already in place). |
|
823 */ |
7234
|
824 nb = gallop_left (pa[na-1], pb, nb, nb-1); |
4851
|
825 if (nb <= 0) |
|
826 return nb; |
|
827 |
|
828 /* Merge what remains of the runs, using a temp array with |
|
829 * min(na, nb) elements. |
|
830 */ |
|
831 if (na <= nb) |
7234
|
832 return merge_lo (pa, na, pb, nb); |
4851
|
833 else |
7234
|
834 return merge_hi (pa, na, pb, nb); |
4851
|
835 } |
|
836 |
|
837 /* Examine the stack of runs waiting to be merged, merging adjacent runs |
|
838 * until the stack invariants are re-established: |
|
839 * |
|
840 * 1. len[-3] > len[-2] + len[-1] |
|
841 * 2. len[-2] > len[-1] |
|
842 * |
|
843 * See listsort.txt for more info. |
|
844 * |
|
845 * Returns 0 on success, -1 on error. |
|
846 */ |
|
847 template <class T> |
|
848 int |
7234
|
849 octave_sort<T>::merge_collapse (void) |
4851
|
850 { |
|
851 struct s_slice *p = ms.pending; |
|
852 |
|
853 while (ms.n > 1) |
|
854 { |
|
855 int n = ms.n - 2; |
|
856 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) |
|
857 { |
|
858 if (p[n-1].len < p[n+1].len) |
|
859 --n; |
7234
|
860 if (merge_at (n) < 0) |
4851
|
861 return -1; |
|
862 } |
|
863 else if (p[n].len <= p[n+1].len) |
|
864 { |
7234
|
865 if (merge_at (n) < 0) |
4851
|
866 return -1; |
|
867 } |
|
868 else |
|
869 break; |
|
870 } |
7234
|
871 |
4851
|
872 return 0; |
|
873 } |
|
874 |
|
875 /* Regardless of invariants, merge all runs on the stack until only one |
|
876 * remains. This is used at the end of the mergesort. |
|
877 * |
|
878 * Returns 0 on success, -1 on error. |
|
879 */ |
|
880 template <class T> |
|
881 int |
7234
|
882 octave_sort<T>::merge_force_collapse (void) |
4851
|
883 { |
|
884 struct s_slice *p = ms.pending; |
|
885 |
|
886 while (ms.n > 1) |
|
887 { |
|
888 int n = ms.n - 2; |
|
889 if (n > 0 && p[n-1].len < p[n+1].len) |
|
890 --n; |
7234
|
891 if (merge_at (n) < 0) |
4851
|
892 return -1; |
|
893 } |
7234
|
894 |
4851
|
895 return 0; |
|
896 } |
|
897 |
|
898 /* Compute a good value for the minimum run length; natural runs shorter |
|
899 * than this are boosted artificially via binary insertion. |
|
900 * |
|
901 * If n < 64, return n (it's too small to bother with fancy stuff). |
|
902 * Else if n is an exact power of 2, return 32. |
|
903 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but |
|
904 * strictly less than, an exact power of 2. |
|
905 * |
|
906 * See listsort.txt for more info. |
|
907 */ |
|
908 template <class T> |
|
909 int |
7234
|
910 octave_sort<T>::merge_compute_minrun (int n) |
4851
|
911 { |
|
912 int r = 0; /* becomes 1 if any 1 bits are shifted off */ |
|
913 |
7234
|
914 while (n >= 64) |
|
915 { |
|
916 r |= n & 1; |
|
917 n >>= 1; |
|
918 } |
|
919 |
4851
|
920 return n + r; |
|
921 } |
|
922 |
|
923 template <class T> |
|
924 void |
|
925 octave_sort<T>::sort (T *v, int elements) |
|
926 { |
|
927 /* Re-initialize the Mergestate as this might be the second time called */ |
|
928 ms.n = 0; |
|
929 ms.min_gallop = MIN_GALLOP; |
|
930 |
|
931 if (elements > 1) |
|
932 { |
|
933 int nremaining = elements; |
|
934 T *lo = v; |
|
935 T *hi = v + elements; |
|
936 |
|
937 /* March over the array once, left to right, finding natural runs, |
|
938 * and extending short natural runs to minrun elements. |
|
939 */ |
7234
|
940 int minrun = merge_compute_minrun (nremaining); |
4851
|
941 do |
|
942 { |
|
943 int descending; |
|
944 int n; |
|
945 |
|
946 /* Identify next run. */ |
7234
|
947 n = count_run (lo, hi, &descending); |
4851
|
948 if (n < 0) |
|
949 goto fail; |
|
950 if (descending) |
7234
|
951 reverse_slice (lo, lo + n); |
4851
|
952 /* If short, extend to min(minrun, nremaining). */ |
|
953 if (n < minrun) |
|
954 { |
|
955 const int force = nremaining <= minrun ? nremaining : minrun; |
7234
|
956 binarysort (lo, lo + force, lo + n); |
4851
|
957 n = force; |
|
958 } |
|
959 /* Push run onto pending-runs stack, and maybe merge. */ |
7234
|
960 assert (ms.n < MAX_MERGE_PENDING); |
4851
|
961 ms.pending[ms.n].base = lo; |
|
962 ms.pending[ms.n].len = n; |
|
963 ++ms.n; |
7234
|
964 if (merge_collapse () < 0) |
4851
|
965 goto fail; |
|
966 /* Advance to find next run. */ |
|
967 lo += n; |
|
968 nremaining -= n; |
7234
|
969 } |
|
970 while (nremaining); |
4851
|
971 |
7234
|
972 merge_force_collapse (); |
4851
|
973 } |
|
974 |
|
975 fail: |
|
976 return; |
|
977 } |
|
978 |
|
979 /* |
|
980 ;;; Local Variables: *** |
|
981 ;;; mode: C++ *** |
|
982 ;;; End: *** |
|
983 */ |