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