annotate liboctave/oct-sort.cc @ 8710:739141cde75a ss-3-1-52

fix typo in Array-f.cc
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 09 Feb 2009 21:51:31 +0100
parents 314be237cd5b
children e9cb742df9eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1 /*
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 David Bateman
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
3 Copyright (C) 2008, 2009 Jaroslav Hajek
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
4
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
6
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6959
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6959
diff changeset
10 option) any later version.
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
11
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
15 for more details.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
16
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6959
diff changeset
18 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6959
diff changeset
19 <http://www.gnu.org/licenses/>.
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
20
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
21 Code stolen in large part from Python's, listobject.c, which itself had
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
22 no license header. However, thanks to Tim Peters for the parts of the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
23 code I ripped-off.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
24
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
25 As required in the Python license the short description of the changes
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
26 made are
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
27
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
28 * convert the sorting code in listobject.cc into a generic class,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
29 replacing PyObject* with the type of the class T.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
30
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
31 * replaced usages of malloc, free, memcpy and memmove by standard C++
8678
e2b4c19c455c redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8206
diff changeset
32 new [], delete [] and std::copy and std::copy_backward. Note that replacing
e2b4c19c455c redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8206
diff changeset
33 memmove by std::copy is possible if the destination starts before the source.
e2b4c19c455c redo changeset 4238f2600a17 with fixes to sorting
Jaroslav Hajek <highegg@gmail.com>
parents: 8206
diff changeset
34 If not, std::copy_backward needs to be used.
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
35
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
36 * templatize comparison operator in most methods, provide possible dispatch
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
37
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
38 * duplicate methods to avoid by-the-way indexed sorting
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
39
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
40
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
41 The Python license is
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
42
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
43 PSF LICENSE AGREEMENT FOR PYTHON 2.3
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
44 --------------------------------------
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
45
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
46 1. This LICENSE AGREEMENT is between the Python Software Foundation
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
47 ("PSF"), and the Individual or Organization ("Licensee") accessing and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
48 otherwise using Python 2.3 software in source or binary form and its
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
49 associated documentation.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
50
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
51 2. Subject to the terms and conditions of this License Agreement, PSF
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
52 hereby grants Licensee a nonexclusive, royalty-free, world-wide
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
53 license to reproduce, analyze, test, perform and/or display publicly,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
54 prepare derivative works, distribute, and otherwise use Python 2.3
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
55 alone or in any derivative version, provided, however, that PSF's
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
56 License Agreement and PSF's notice of copyright, i.e., "Copyright (c)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
57 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
58 retained in Python 2.3 alone or in any derivative version prepared by
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
59 Licensee.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
60
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
61 3. In the event Licensee prepares a derivative work that is based on
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
62 or incorporates Python 2.3 or any part thereof, and wants to make
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
63 the derivative work available to others as provided herein, then
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
64 Licensee hereby agrees to include in any such work a brief summary of
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
65 the changes made to Python 2.3.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
66
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
67 4. PSF is making Python 2.3 available to Licensee on an "AS IS"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
68 basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
69 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
70 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
71 FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
72 INFRINGE ANY THIRD PARTY RIGHTS.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
73
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
74 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
75 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
76 A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
77 OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
78
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
79 6. This License Agreement will automatically terminate upon a material
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
80 breach of its terms and conditions.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
81
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
82 7. Nothing in this License Agreement shall be deemed to create any
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
83 relationship of agency, partnership, or joint venture between PSF and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
84 Licensee. This License Agreement does not grant permission to use PSF
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
85 trademarks or trade name in a trademark sense to endorse or promote
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
86 products or services of Licensee, or any third party.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
87
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
88 8. By copying, installing or otherwise using Python 2.3, Licensee
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
89 agrees to be bound by the terms and conditions of this License
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
90 Agreement.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
91 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
92
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
93 #ifdef HAVE_CONFIG_H
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
94 #include <config.h>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
95 #endif
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
96
5883
1de9a198a303 [project @ 2006-07-14 18:10:30 by jwe]
jwe
parents: 5760
diff changeset
97 #include <cassert>
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
98 #include <algorithm>
7480
93826ba0d078 compilation fixes
Jason Riedy
parents: 7433
diff changeset
99 #include <cstring>
5883
1de9a198a303 [project @ 2006-07-14 18:10:30 by jwe]
jwe
parents: 5760
diff changeset
100
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
101 #include "lo-mappers.h"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
102 #include "quit.h"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
103 #include "oct-sort.h"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
104
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
105 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
106 octave_sort<T>::octave_sort (void) : compare (ascending_compare)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
107 {
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
108 merge_init ();
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
109 merge_getmem (1024);
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
110 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
111
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
112 template <class T>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
113 octave_sort<T>::octave_sort (bool (*comp) (T, T)) : compare (comp)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
114 {
4998
3f3d6eec0a2c [project @ 2004-09-15 21:00:01 by jwe]
jwe
parents: 4882
diff changeset
115 merge_init ();
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
116 merge_getmem (1024);
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
117 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
118
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
119 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
120 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
121 void
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
122 octave_sort<T>::binarysort (T *data, octave_idx_type nel,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
123 octave_idx_type start, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
124 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
125 if (start == 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
126 ++start;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
127
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
128 for (; start < nel; ++start)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
129 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
130 /* set l to where *start belongs */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
131 octave_idx_type l = 0, r = start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
132 T pivot = data[start];
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
133 /* Invariants:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
134 * pivot >= all in [lo, l).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
135 * pivot < all in [r, start).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
136 * The second is vacuously true at the start.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
137 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
138 do
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
139 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
140 octave_idx_type p = l + ((r - l) >> 1);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
141 if (comp (pivot, data[p]))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
142 r = p;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
143 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
144 l = p+1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
145 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
146 while (l < r);
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
147 /* The invariants still hold, so pivot >= all in [lo, l) and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
148 pivot < all in [l, start), so pivot belongs at l. Note
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
149 that if there are elements equal to pivot, l points to the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
150 first slot after them -- that's why this sort is stable.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
151 Slide over to make room.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
152 Caution: using memmove is much slower under MSVC 5;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
153 we're not usually moving many slots. */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
154 // NOTE: using swap and going upwards appears to be faster.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
155 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
156 std::swap (pivot, data[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
157 data[start] = pivot;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
158 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
159
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
160 return;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
161 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
162
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
163 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
164 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
165 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
166 octave_sort<T>::binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
167 octave_idx_type start, Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
168 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
169 if (start == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
170 ++start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
171
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
172 for (; start < nel; ++start)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
173 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
174 /* set l to where *start belongs */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
175 octave_idx_type l = 0, r = start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
176 T pivot = data[start];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
177 /* Invariants:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
178 * pivot >= all in [lo, l).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
179 * pivot < all in [r, start).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
180 * The second is vacuously true at the start.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
181 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
182 do
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
183 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
184 octave_idx_type p = l + ((r - l) >> 1);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
185 if (comp (pivot, data[p]))
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
186 r = p;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
187 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
188 l = p+1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
189 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
190 while (l < r);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
191 /* The invariants still hold, so pivot >= all in [lo, l) and
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
192 pivot < all in [l, start), so pivot belongs at l. Note
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
193 that if there are elements equal to pivot, l points to the
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
194 first slot after them -- that's why this sort is stable.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
195 Slide over to make room.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
196 Caution: using memmove is much slower under MSVC 5;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
197 we're not usually moving many slots. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
198 // NOTE: using swap and going upwards appears to be faster.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
199 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
200 std::swap (pivot, data[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
201 data[start] = pivot;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
202 octave_idx_type ipivot = idx[start];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
203 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
204 std::swap (ipivot, idx[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
205 idx[start] = ipivot;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
206 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
207
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
208 return;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
209 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
210
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
211 /*
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
212 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
213 is required on entry. "A run" is the longest ascending sequence, with
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
214
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
215 lo[0] <= lo[1] <= lo[2] <= ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
216
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
217 or the longest descending sequence, with
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
218
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
219 lo[0] > lo[1] > lo[2] > ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
220
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
221 DESCENDING is set to false in the former case, or to true in the latter.
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
222 For its intended use in a stable mergesort, the strictness of the defn of
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
223 "descending" is needed so that the caller can safely reverse a descending
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
224 sequence without violating stability (strict > ensures there are no equal
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
225 elements to get out of order).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
226
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
227 Returns -1 in case of error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
228 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
229 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
230 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
231 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
232 octave_sort<T>::count_run (T *lo, octave_idx_type nel, bool& descending, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
233 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
234 octave_idx_type n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
235 T *hi = lo + nel;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
236
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
237 descending = false;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
238 ++lo;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
239 if (lo == hi)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
240 return 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
241
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
242 n = 2;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
243
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
244 if (comp (*lo, *(lo-1)))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
245 {
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
246 descending = true;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
247 for (lo = lo+1; lo < hi; ++lo, ++n)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
248 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
249 if (comp (*lo, *(lo-1)))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
250 ;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
251 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
252 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
253 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
254 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
255 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
256 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
257 for (lo = lo+1; lo < hi; ++lo, ++n)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
258 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
259 if (comp (*lo, *(lo-1)))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
260 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
261 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
262 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
263
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
264 return n;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
265 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
266
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
267 /*
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
268 Locate the proper position of key in a sorted vector; if the vector contains
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
269 an element equal to key, return the position immediately to the left of
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
270 the leftmost equal element. [gallop_right() does the same except returns
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
271 the position to the right of the rightmost equal element (if any).]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
272
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
273 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
274
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
275 "hint" is an index at which to begin the search, 0 <= hint < n. The closer
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
276 hint is to the final result, the faster this runs.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
277
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
278 The return value is the int k in 0..n such that
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
279
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
280 a[k-1] < key <= a[k]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
281
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
282 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
283 key belongs at index k; or, IOW, the first k elements of a should precede
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
284 key, and the last n-k should follow key.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
285
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
286 Returns -1 on error. See listsort.txt for info on the method.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
287 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
288 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
289 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
290 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
291 octave_sort<T>::gallop_left (T key, T *a, octave_idx_type n, octave_idx_type hint,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
292 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
293 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
294 octave_idx_type ofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
295 octave_idx_type lastofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
296 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
297
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
298 a += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
299 lastofs = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
300 ofs = 1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
301 if (comp (*a, key))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
302 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
303 /* a[hint] < key -- gallop right, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
304 * a[hint + lastofs] < key <= a[hint + ofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
305 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
306 const octave_idx_type maxofs = n - hint; /* &a[n-1] is highest */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
307 while (ofs < maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
308 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
309 if (comp (a[ofs], key))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
310 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
311 lastofs = ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
312 ofs = (ofs << 1) + 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
313 if (ofs <= 0) /* int overflow */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
314 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
315 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
316 else /* key <= a[hint + ofs] */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
317 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
318 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
319 if (ofs > maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
320 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
321 /* Translate back to offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
322 lastofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
323 ofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
324 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
325 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
326 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
327 /* key <= a[hint] -- gallop left, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
328 * a[hint - ofs] < key <= a[hint - lastofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
329 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
330 const octave_idx_type maxofs = hint + 1; /* &a[0] is lowest */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
331 while (ofs < maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
332 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
333 if (comp (*(a-ofs), key))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
334 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
335 /* key <= a[hint - ofs] */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
336 lastofs = ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
337 ofs = (ofs << 1) + 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
338 if (ofs <= 0) /* int overflow */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
339 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
340 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
341 if (ofs > maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
342 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
343 /* Translate back to positive offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
344 k = lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
345 lastofs = hint - ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
346 ofs = hint - k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
347 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
348 a -= hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
349
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
350 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
351 * right of lastofs but no farther right than ofs. Do a binary
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
352 * search, with invariant a[lastofs-1] < key <= a[ofs].
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
353 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
354 ++lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
355 while (lastofs < ofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
356 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
357 octave_idx_type m = lastofs + ((ofs - lastofs) >> 1);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
358
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
359 if (comp (a[m], key))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
360 lastofs = m+1; /* a[m] < key */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
361 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
362 ofs = m; /* key <= a[m] */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
363 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
364
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
365 return ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
366 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
367
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
368 /*
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
369 Exactly like gallop_left(), except that if key already exists in a[0:n],
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
370 finds the position immediately to the right of the rightmost equal value.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
371
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
372 The return value is the int k in 0..n such that
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
373
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
374 a[k-1] <= key < a[k]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
375
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
376 or -1 if error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
377
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
378 The code duplication is massive, but this is enough different given that
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
379 we're sticking to "<" comparisons that it's much harder to follow if
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
380 written as one routine with yet another "left or right?" flag.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
381 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
382 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
383 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
384 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
385 octave_sort<T>::gallop_right (T key, T *a, octave_idx_type n, octave_idx_type hint,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
386 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
387 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
388 octave_idx_type ofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
389 octave_idx_type lastofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
390 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
391
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
392 a += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
393 lastofs = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
394 ofs = 1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
395 if (comp (key, *a))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
396 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
397 /* key < a[hint] -- gallop left, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
398 * a[hint - ofs] <= key < a[hint - lastofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
399 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
400 const octave_idx_type maxofs = hint + 1; /* &a[0] is lowest */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
401 while (ofs < maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
402 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
403 if (comp (key, *(a-ofs)))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
404 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
405 lastofs = ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
406 ofs = (ofs << 1) + 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
407 if (ofs <= 0) /* int overflow */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
408 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
409 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
410 else /* a[hint - ofs] <= key */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
411 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
412 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
413 if (ofs > maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
414 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
415 /* Translate back to positive offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
416 k = lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
417 lastofs = hint - ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
418 ofs = hint - k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
419 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
420 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
421 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
422 /* a[hint] <= key -- gallop right, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
423 * a[hint + lastofs] <= key < a[hint + ofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
424 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
425 const octave_idx_type maxofs = n - hint; /* &a[n-1] is highest */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
426 while (ofs < maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
427 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
428 if (comp (key, a[ofs]))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
429 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
430 /* a[hint + ofs] <= key */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
431 lastofs = ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
432 ofs = (ofs << 1) + 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
433 if (ofs <= 0) /* int overflow */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
434 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
435 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
436 if (ofs > maxofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
437 ofs = maxofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
438 /* Translate back to offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
439 lastofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
440 ofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
441 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
442 a -= hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
443
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
444 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
445 * right of lastofs but no farther right than ofs. Do a binary
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
446 * search, with invariant a[lastofs-1] <= key < a[ofs].
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
447 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
448 ++lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
449 while (lastofs < ofs)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
450 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
451 octave_idx_type m = lastofs + ((ofs - lastofs) >> 1);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
452
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
453 if (comp (key, a[m]))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
454 ofs = m; /* key < a[m] */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
455 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
456 lastofs = m+1; /* a[m] <= key */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
457 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
458
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
459 return ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
460 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
461
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
462 /* Conceptually a MergeState's constructor. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
463 template <class T>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
464 void
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
465 octave_sort<T>::merge_init (void)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
466 {
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
467 ms.a = 0;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
468 ms.ia = 0;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
469 ms.alloced = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
470 ms.n = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
471 ms.min_gallop = MIN_GALLOP;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
472 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
473
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
474 /* Free all the temp memory owned by the MergeState. This must be called
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
475 * when you're done with a MergeState, and may be called before then if
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
476 * you want to free the temp memory early.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
477 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
478 template <class T>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
479 void
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
480 octave_sort<T>::merge_freemem (void)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
481 {
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
482 delete [] ms.a;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
483 delete [] ms.ia;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
484 ms.alloced = 0;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
485 ms.a = 0;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
486 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
487
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
488 static inline octave_idx_type
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
489 roundupsize (octave_idx_type n)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
490 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
491 unsigned int nbits = 3;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
492 octave_idx_type n2 = static_cast<octave_idx_type> (n) >> 8;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
493
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
494 /* Round up:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
495 * If n < 256, to a multiple of 8.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
496 * If n < 2048, to a multiple of 64.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
497 * If n < 16384, to a multiple of 512.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
498 * If n < 131072, to a multiple of 4096.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
499 * If n < 1048576, to a multiple of 32768.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
500 * If n < 8388608, to a multiple of 262144.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
501 * If n < 67108864, to a multiple of 2097152.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
502 * If n < 536870912, to a multiple of 16777216.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
503 * ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
504 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
505 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
506 * This over-allocates proportional to the list size, making room
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
507 * for additional growth. The over-allocation is mild, but is
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
508 * enough to give linear-time amortized behavior over a long
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
509 * sequence of appends() in the presence of a poorly-performing
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
510 * system realloc() (which is a reality, e.g., across all flavors
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
511 * of Windows, with Win9x behavior being particularly bad -- and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
512 * we've still got address space fragmentation problems on Win9x
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
513 * even with this scheme, although it requires much longer lists to
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
514 * provoke them than it used to).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
515 */
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
516 while (n2)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
517 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
518 n2 >>= 3;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
519 nbits += 3;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
520 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
521
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
522 return ((n >> nbits) + 1) << nbits;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
523 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
524
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
525 /* Ensure enough temp memory for 'need' array slots is available.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
526 * Returns 0 on success and -1 if the memory can't be gotten.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
527 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
528 template <class T>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
529 int
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
530 octave_sort<T>::merge_getmem (octave_idx_type need)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
531 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
532 if (need <= ms.alloced)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
533 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
534
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
535 need = roundupsize (need);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
536 /* Don't realloc! That can cost cycles to copy the old data, but
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
537 * we don't care what's in the block.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
538 */
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
539 merge_freemem ();
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
540 ms.a = new T[need];
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
541 if (ms.a)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
542 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
543 ms.alloced = need;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
544 return 0;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
545 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
546 merge_freemem (); /* reset to sane state */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
547
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
548 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
549 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
550
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
551 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
552 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
553 octave_sort<T>::merge_getmemi (octave_idx_type need)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
554 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
555 if (need <= ms.alloced && ms.a && ms.ia)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
556 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
557
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
558 need = roundupsize (need);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
559 /* Don't realloc! That can cost cycles to copy the old data, but
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
560 * we don't care what's in the block.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
561 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
562 merge_freemem ();
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
563 ms.a = new T[need];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
564 ms.ia = new octave_idx_type[need];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
565 if (ms.a && ms.ia)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
566 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
567 ms.alloced = need;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
568 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
569 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
570 merge_freemem (); /* reset to sane state */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
571
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
572 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
573 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
574
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
575 /* Merge the na elements starting at pa with the nb elements starting at pb
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
576 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
577 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
578 * merge, and should have na <= nb. See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
579 * Return 0 if successful, -1 if error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
580 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
581 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
582 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
583 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
584 octave_sort<T>::merge_lo (T *pa, octave_idx_type na,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
585 T *pb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
586 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
587 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
588 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
589 T *dest;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
590 int result = -1; /* guilty until proved innocent */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
591 octave_idx_type min_gallop = ms.min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
592
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
593 if (merge_getmem (na) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
594 return -1;
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
595 std::copy (pa, pa + na, ms.a);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
596 dest = pa;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
597 pa = ms.a;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
598
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
599 *dest++ = *pb++;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
600 --nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
601 if (nb == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
602 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
603 if (na == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
604 goto CopyB;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
605
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
606 for (;;)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
607 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
608 octave_idx_type acount = 0; /* # of times A won in a row */
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
609 octave_idx_type bcount = 0; /* # of times B won in a row */
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
610
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
611 /* Do the straightforward thing until (if ever) one run
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
612 * appears to win consistently.
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
613 */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
614 for (;;)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
615 {
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
616
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
617 // FIXME: these loops are candidates for further optimizations.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
618 // Rather than testing everything in each cycle, it may be more
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
619 // efficient to do it in hunks.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
620 if (comp (*pb, *pa))
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
621 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
622 *dest++ = *pb++;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
623 ++bcount;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
624 acount = 0;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
625 --nb;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
626 if (nb == 0)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
627 goto Succeed;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
628 if (bcount >= min_gallop)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
629 break;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
630 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
631 else
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
632 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
633 *dest++ = *pa++;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
634 ++acount;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
635 bcount = 0;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
636 --na;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
637 if (na == 1)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
638 goto CopyB;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
639 if (acount >= min_gallop)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
640 break;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
641 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
642 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
643
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
644 /* One run is winning so consistently that galloping may
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
645 * be a huge win. So try that, and continue galloping until
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
646 * (if ever) neither run appears to be winning consistently
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
647 * anymore.
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
648 */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
649 ++min_gallop;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
650 do
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
651 {
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
652 min_gallop -= min_gallop > 1;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
653 ms.min_gallop = min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
654 k = gallop_right (*pb, pa, na, 0, comp);
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
655 acount = k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
656 if (k)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
657 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
658 if (k < 0)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
659 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
660 dest = std::copy (pa, pa + k, dest);
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
661 pa += k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
662 na -= k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
663 if (na == 1)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
664 goto CopyB;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
665 /* na==0 is impossible now if the comparison
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
666 * function is consistent, but we can't assume
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
667 * that it is.
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
668 */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
669 if (na == 0)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
670 goto Succeed;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
671 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
672 *dest++ = *pb++;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
673 --nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
674 if (nb == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
675 goto Succeed;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
676
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
677 k = gallop_left (*pa, pb, nb, 0, comp);
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
678 bcount = k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
679 if (k)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
680 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
681 if (k < 0)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
682 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
683 dest = std::copy (pb, pb + k, dest);
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
684 pb += k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
685 nb -= k;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
686 if (nb == 0)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
687 goto Succeed;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
688 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
689 *dest++ = *pa++;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
690 --na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
691 if (na == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
692 goto CopyB;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
693 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
694 while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
695
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
696 ++min_gallop; /* penalize it for leaving galloping mode */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
697 ms.min_gallop = min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
698 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
699
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
700 Succeed:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
701 result = 0;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
702
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
703 Fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
704 if (na)
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
705 std::copy (pa, pa + na, dest);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
706 return result;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
707
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
708 CopyB:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
709 /* The last element of pa belongs at the end of the merge. */
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
710 std::copy (pb, pb + nb, dest);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
711 dest[nb] = *pa;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
712
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
713 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
714 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
715
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
716 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
717 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
718 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
719 octave_sort<T>::merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
720 T *pb, octave_idx_type *ipb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
721 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
722 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
723 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
724 T *dest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
725 octave_idx_type *idest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
726 int result = -1; /* guilty until proved innocent */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
727 octave_idx_type min_gallop = ms.min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
728
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
729 if (merge_getmemi (na) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
730 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
731 std::copy (pa, pa + na, ms.a);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
732 std::copy (ipa, ipa + na, ms.ia);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
733 dest = pa; idest = ipa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
734 pa = ms.a; ipa = ms.ia;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
735
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
736 *dest++ = *pb++; *idest++ = *ipb++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
737 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
738 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
739 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
740 if (na == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
741 goto CopyB;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
742
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
743 for (;;)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
744 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
745 octave_idx_type acount = 0; /* # of times A won in a row */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
746 octave_idx_type bcount = 0; /* # of times B won in a row */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
747
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
748 /* Do the straightforward thing until (if ever) one run
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
749 * appears to win consistently.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
750 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
751 for (;;)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
752 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
753
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
754 if (comp (*pb, *pa))
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
755 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
756 *dest++ = *pb++; *idest++ = *ipb++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
757 ++bcount;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
758 acount = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
759 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
760 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
761 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
762 if (bcount >= min_gallop)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
763 break;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
764 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
765 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
766 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
767 *dest++ = *pa++; *idest++ = *ipa++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
768 ++acount;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
769 bcount = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
770 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
771 if (na == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
772 goto CopyB;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
773 if (acount >= min_gallop)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
774 break;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
775 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
776 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
777
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
778 /* One run is winning so consistently that galloping may
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
779 * be a huge win. So try that, and continue galloping until
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
780 * (if ever) neither run appears to be winning consistently
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
781 * anymore.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
782 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
783 ++min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
784 do
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
785 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
786 min_gallop -= min_gallop > 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
787 ms.min_gallop = min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
788 k = gallop_right (*pb, pa, na, 0, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
789 acount = k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
790 if (k)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
791 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
792 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
793 goto Fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
794 dest = std::copy (pa, pa + k, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
795 idest = std::copy (ipa, ipa + k, idest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
796 pa += k; ipa += k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
797 na -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
798 if (na == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
799 goto CopyB;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
800 /* na==0 is impossible now if the comparison
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
801 * function is consistent, but we can't assume
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
802 * that it is.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
803 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
804 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
805 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
806 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
807 *dest++ = *pb++; *idest++ = *ipb++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
808 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
809 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
810 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
811
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
812 k = gallop_left (*pa, pb, nb, 0, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
813 bcount = k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
814 if (k)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
815 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
816 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
817 goto Fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
818 dest = std::copy (pb, pb + k, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
819 idest = std::copy (ipb, ipb + k, idest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
820 pb += k; ipb += k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
821 nb -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
822 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
823 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
824 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
825 *dest++ = *pa++; *idest++ = *ipa++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
826 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
827 if (na == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
828 goto CopyB;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
829 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
830 while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
831
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
832 ++min_gallop; /* penalize it for leaving galloping mode */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
833 ms.min_gallop = min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
834 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
835
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
836 Succeed:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
837 result = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
838
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
839 Fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
840 if (na)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
841 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
842 std::copy (pa, pa + na, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
843 std::copy (ipa, ipa + na, idest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
844 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
845 return result;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
846
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
847 CopyB:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
848 /* The last element of pa belongs at the end of the merge. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
849 std::copy (pb, pb + nb, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
850 std::copy (ipb, ipb + nb, idest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
851 dest[nb] = *pa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
852 idest[nb] = *ipa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
853
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
854 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
855 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
856
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
857 /* Merge the na elements starting at pa with the nb elements starting at pb
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
858 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
859 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
860 * merge, and should have na >= nb. See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
861 * Return 0 if successful, -1 if error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
862 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
863 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
864 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
865 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
866 octave_sort<T>::merge_hi (T *pa, octave_idx_type na,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
867 T *pb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
868 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
869 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
870 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
871 T *dest;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
872 int result = -1; /* guilty until proved innocent */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
873 T *basea, *baseb;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
874 octave_idx_type min_gallop = ms.min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
875
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
876 if (merge_getmem (nb) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
877 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
878 dest = pb + nb - 1;
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
879 std::copy (pb, pb + nb, ms.a);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
880 basea = pa;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
881 baseb = ms.a;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
882 pb = ms.a + nb - 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
883 pa += na - 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
884
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
885 *dest-- = *pa--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
886 --na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
887 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
888 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
889 if (nb == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
890 goto CopyA;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
891
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
892 for (;;)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
893 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
894 octave_idx_type acount = 0; /* # of times A won in a row */
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
895 octave_idx_type bcount = 0; /* # of times B won in a row */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
896
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
897 /* Do the straightforward thing until (if ever) one run
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
898 * appears to win consistently.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
899 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
900 for (;;)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
901 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
902 if (comp (*pb, *pa))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
903 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
904 *dest-- = *pa--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
905 ++acount;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
906 bcount = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
907 --na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
908 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
909 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
910 if (acount >= min_gallop)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
911 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
912 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
913 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
914 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
915 *dest-- = *pb--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
916 ++bcount;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
917 acount = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
918 --nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
919 if (nb == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
920 goto CopyA;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
921 if (bcount >= min_gallop)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
922 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
923 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
924 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
925
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
926 /* One run is winning so consistently that galloping may
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
927 * be a huge win. So try that, and continue galloping until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
928 * (if ever) neither run appears to be winning consistently
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
929 * anymore.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
930 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
931 ++min_gallop;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
932 do
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
933 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
934 min_gallop -= min_gallop > 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
935 ms.min_gallop = min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
936 k = gallop_right (*pb, basea, na, na-1, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
937 if (k < 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
938 goto Fail;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
939 k = na - k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
940 acount = k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
941 if (k)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
942 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
943 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
944 pa -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
945 na -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
946 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
947 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
948 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
949 *dest-- = *pb--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
950 --nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
951 if (nb == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
952 goto CopyA;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
953
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
954 k = gallop_left (*pa, baseb, nb, nb-1, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
955 if (k < 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
956 goto Fail;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
957 k = nb - k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
958 bcount = k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
959 if (k)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
960 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
961 dest -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
962 pb -= k;
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
963 std::copy (pb+1, pb+1 + k, dest+1);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
964 nb -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
965 if (nb == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
966 goto CopyA;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
967 /* nb==0 is impossible now if the comparison
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
968 * function is consistent, but we can't assume
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
969 * that it is.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
970 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
971 if (nb == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
972 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
973 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
974 *dest-- = *pa--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
975 --na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
976 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
977 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
978 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
979 ++min_gallop; /* penalize it for leaving galloping mode */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
980 ms.min_gallop = min_gallop;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
981 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
982
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
983 Succeed:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
984 result = 0;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
985
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
986 Fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
987 if (nb)
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
988 std::copy (baseb, baseb + nb, dest-(nb-1));
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
989 return result;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
990
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
991 CopyA:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
992 /* The first element of pb belongs at the front of the merge. */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
993 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
994 pa -= na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
995 *dest = *pb;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
996
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
997 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
998 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
999
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1000 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1001 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1002 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1003 octave_sort<T>::merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1004 T *pb, octave_idx_type *ipb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1005 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1006 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1007 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1008 T *dest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1009 octave_idx_type *idest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1010 int result = -1; /* guilty until proved innocent */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1011 T *basea, *baseb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1012 octave_idx_type *ibasea, *ibaseb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1013 octave_idx_type min_gallop = ms.min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1014
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1015 if (merge_getmemi (nb) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1016 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1017 dest = pb + nb - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1018 idest = ipb + nb - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1019 std::copy (pb, pb + nb, ms.a);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1020 std::copy (ipb, ipb + nb, ms.ia);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1021 basea = pa; ibasea = ipa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1022 baseb = ms.a; ibaseb = ms.ia;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1023 pb = ms.a + nb - 1; ipb = ms.ia + nb - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1024 pa += na - 1; ipa += na - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1025
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1026 *dest-- = *pa--; *idest-- = *ipa--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1027 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1028 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1029 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1030 if (nb == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1031 goto CopyA;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1032
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1033 for (;;)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1034 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1035 octave_idx_type acount = 0; /* # of times A won in a row */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1036 octave_idx_type bcount = 0; /* # of times B won in a row */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1037
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1038 /* Do the straightforward thing until (if ever) one run
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1039 * appears to win consistently.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1040 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1041 for (;;)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1042 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1043 if (comp (*pb, *pa))
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1044 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1045 *dest-- = *pa--; *idest-- = *ipa--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1046 ++acount;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1047 bcount = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1048 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1049 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1050 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1051 if (acount >= min_gallop)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1052 break;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1053 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1054 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1055 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1056 *dest-- = *pb--; *idest-- = *ipb--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1057 ++bcount;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1058 acount = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1059 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1060 if (nb == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1061 goto CopyA;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1062 if (bcount >= min_gallop)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1063 break;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1064 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1065 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1066
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1067 /* One run is winning so consistently that galloping may
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1068 * be a huge win. So try that, and continue galloping until
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1069 * (if ever) neither run appears to be winning consistently
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1070 * anymore.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1071 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1072 ++min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1073 do
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1074 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1075 min_gallop -= min_gallop > 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1076 ms.min_gallop = min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1077 k = gallop_right (*pb, basea, na, na-1, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1078 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1079 goto Fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1080 k = na - k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1081 acount = k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1082 if (k)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1083 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1084 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1085 idest = std::copy_backward (ipa+1 - k, ipa+1, idest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1086 pa -= k; ipa -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1087 na -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1088 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1089 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1090 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1091 *dest-- = *pb--; *idest-- = *ipb--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1092 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1093 if (nb == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1094 goto CopyA;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1095
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1096 k = gallop_left (*pa, baseb, nb, nb-1, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1097 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1098 goto Fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1099 k = nb - k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1100 bcount = k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1101 if (k)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1102 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1103 dest -= k; idest -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1104 pb -= k; ipb -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1105 std::copy (pb+1, pb+1 + k, dest+1);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1106 std::copy (ipb+1, ipb+1 + k, idest+1);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1107 nb -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1108 if (nb == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1109 goto CopyA;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1110 /* nb==0 is impossible now if the comparison
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1111 * function is consistent, but we can't assume
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1112 * that it is.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1113 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1114 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1115 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1116 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1117 *dest-- = *pa--; *idest-- = *ipa--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1118 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1119 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1120 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1121 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1122 ++min_gallop; /* penalize it for leaving galloping mode */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1123 ms.min_gallop = min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1124 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1125
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1126 Succeed:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1127 result = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1128
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1129 Fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1130 if (nb)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1131 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1132 std::copy (baseb, baseb + nb, dest-(nb-1));
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1133 std::copy (ibaseb, ibaseb + nb, idest-(nb-1));
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1134 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1135 return result;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1136
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1137 CopyA:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1138 /* The first element of pb belongs at the front of the merge. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1139 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1140 idest = std::copy_backward (ipa+1 - na, ipa+1, idest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1141 pa -= na; ipa -= na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1142 *dest = *pb; *idest = *ipb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1143
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1144 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1145 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1146
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1147 /* Merge the two runs at stack indices i and i+1.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1148 * Returns 0 on success, -1 on error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1149 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1150 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1151 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1152 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1153 octave_sort<T>::merge_at (octave_idx_type i, T *data,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1154 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1155 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1156 T *pa, *pb;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1157 octave_idx_type na, nb;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1158 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1159
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1160 pa = data + ms.pending[i].base;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1161 na = ms.pending[i].len;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1162 pb = data + ms.pending[i+1].base;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1163 nb = ms.pending[i+1].len;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1164
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1165 /* Record the length of the combined runs; if i is the 3rd-last
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1166 * run now, also slide over the last run (which isn't involved
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1167 * in this merge). The current run i+1 goes away in any case.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1168 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1169 ms.pending[i].len = na + nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1170 if (i == ms.n - 3)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1171 ms.pending[i+1] = ms.pending[i+2];
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1172 --ms.n;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1173
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1174 /* Where does b start in a? Elements in a before that can be
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1175 * ignored (already in place).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1176 */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1177 k = gallop_right (*pb, pa, na, 0, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1178 if (k < 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1179 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1180 pa += k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1181 na -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1182 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1183 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1184
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1185 /* Where does a end in b? Elements in b after that can be
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1186 * ignored (already in place).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1187 */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1188 nb = gallop_left (pa[na-1], pb, nb, nb-1, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1189 if (nb <= 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1190 return nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1191
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1192 /* Merge what remains of the runs, using a temp array with
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1193 * min(na, nb) elements.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1194 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1195 if (na <= nb)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1196 return merge_lo (pa, na, pb, nb, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1197 else
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1198 return merge_hi (pa, na, pb, nb, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1199 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1200
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1201 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1202 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1203 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1204 octave_sort<T>::merge_at (octave_idx_type i, T *data, octave_idx_type *idx,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1205 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1206 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1207 T *pa, *pb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1208 octave_idx_type *ipa, *ipb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1209 octave_idx_type na, nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1210 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1211
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1212 pa = data + ms.pending[i].base;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1213 ipa = idx + ms.pending[i].base;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1214 na = ms.pending[i].len;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1215 pb = data + ms.pending[i+1].base;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1216 ipb = idx + ms.pending[i+1].base;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1217 nb = ms.pending[i+1].len;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1218
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1219 /* Record the length of the combined runs; if i is the 3rd-last
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1220 * run now, also slide over the last run (which isn't involved
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1221 * in this merge). The current run i+1 goes away in any case.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1222 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1223 ms.pending[i].len = na + nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1224 if (i == ms.n - 3)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1225 ms.pending[i+1] = ms.pending[i+2];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1226 --ms.n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1227
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1228 /* Where does b start in a? Elements in a before that can be
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1229 * ignored (already in place).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1230 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1231 k = gallop_right (*pb, pa, na, 0, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1232 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1233 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1234 pa += k; ipa += k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1235 na -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1236 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1237 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1238
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1239 /* Where does a end in b? Elements in b after that can be
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1240 * ignored (already in place).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1241 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1242 nb = gallop_left (pa[na-1], pb, nb, nb-1, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1243 if (nb <= 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1244 return nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1245
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1246 /* Merge what remains of the runs, using a temp array with
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1247 * min(na, nb) elements.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1248 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1249 if (na <= nb)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1250 return merge_lo (pa, ipa, na, pb, ipb, nb, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1251 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1252 return merge_hi (pa, ipa, na, pb, ipb, nb, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1253 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1254
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1255 /* Examine the stack of runs waiting to be merged, merging adjacent runs
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1256 * until the stack invariants are re-established:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1257 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1258 * 1. len[-3] > len[-2] + len[-1]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1259 * 2. len[-2] > len[-1]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1260 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1261 * See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1262 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1263 * Returns 0 on success, -1 on error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1264 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1265 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1266 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1267 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1268 octave_sort<T>::merge_collapse (T *data, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1269 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1270 struct s_slice *p = ms.pending;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1271
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1272 while (ms.n > 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1273 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1274 octave_idx_type n = ms.n - 2;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1275 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1276 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1277 if (p[n-1].len < p[n+1].len)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1278 --n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1279 if (merge_at (n, data, comp) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1280 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1281 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1282 else if (p[n].len <= p[n+1].len)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1283 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1284 if (merge_at (n, data, comp) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1285 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1286 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1287 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1288 break;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1289 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1290
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1291 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1292 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1293
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1294 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1295 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1296 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1297 octave_sort<T>::merge_collapse (T *data, octave_idx_type *idx, Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1298 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1299 struct s_slice *p = ms.pending;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1300
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1301 while (ms.n > 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1302 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1303 octave_idx_type n = ms.n - 2;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1304 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1305 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1306 if (p[n-1].len < p[n+1].len)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1307 --n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1308 if (merge_at (n, data, idx, comp) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1309 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1310 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1311 else if (p[n].len <= p[n+1].len)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1312 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1313 if (merge_at (n, data, idx, comp) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1314 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1315 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1316 else
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1317 break;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1318 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1319
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1320 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1321 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1322
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1323 /* Regardless of invariants, merge all runs on the stack until only one
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1324 * remains. This is used at the end of the mergesort.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1325 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1326 * Returns 0 on success, -1 on error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1327 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1328 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1329 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1330 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1331 octave_sort<T>::merge_force_collapse (T *data, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1332 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1333 struct s_slice *p = ms.pending;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1334
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1335 while (ms.n > 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1336 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1337 octave_idx_type n = ms.n - 2;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1338 if (n > 0 && p[n-1].len < p[n+1].len)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1339 --n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1340 if (merge_at (n, data, comp) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1341 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1342 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1343
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1344 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1345 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1346
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1347 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1348 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1349 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1350 octave_sort<T>::merge_force_collapse (T *data, octave_idx_type *idx, Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1351 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1352 struct s_slice *p = ms.pending;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1353
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1354 while (ms.n > 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1355 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1356 octave_idx_type n = ms.n - 2;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1357 if (n > 0 && p[n-1].len < p[n+1].len)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1358 --n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1359 if (merge_at (n, data, idx, comp) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1360 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1361 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1362
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1363 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1364 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1365
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1366 /* Compute a good value for the minimum run length; natural runs shorter
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1367 * than this are boosted artificially via binary insertion.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1368 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1369 * If n < 64, return n (it's too small to bother with fancy stuff).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1370 * Else if n is an exact power of 2, return 32.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1371 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1372 * strictly less than, an exact power of 2.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1373 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1374 * See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1375 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1376 template <class T>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1377 octave_idx_type
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1378 octave_sort<T>::merge_compute_minrun (octave_idx_type n)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1379 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1380 octave_idx_type r = 0; /* becomes 1 if any 1 bits are shifted off */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1381
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1382 while (n >= 64)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1383 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1384 r |= n & 1;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1385 n >>= 1;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1386 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1387
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1388 return n + r;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1389 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1390
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1391 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1392 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1393 void
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1394 octave_sort<T>::sort (T *data, octave_idx_type nel, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1395 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1396 /* Re-initialize the Mergestate as this might be the second time called */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1397 ms.n = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1398 ms.min_gallop = MIN_GALLOP;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1399
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1400 if (nel > 1)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1401 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1402 octave_idx_type nremaining = nel;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1403 octave_idx_type lo = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1404
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1405 /* March over the array once, left to right, finding natural runs,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1406 * and extending short natural runs to minrun elements.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1407 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1408 octave_idx_type minrun = merge_compute_minrun (nremaining);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1409 do
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1410 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1411 bool descending;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1412 octave_idx_type n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1413
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1414 /* Identify next run. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1415 n = count_run (data + lo, nremaining, descending, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1416 if (n < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1417 goto fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1418 if (descending)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1419 std::reverse (data + lo, data + lo + n);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1420 /* If short, extend to min(minrun, nremaining). */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1421 if (n < minrun)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1422 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1423 const octave_idx_type force = nremaining <= minrun ? nremaining : minrun;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1424 binarysort (data + lo, force, n, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1425 n = force;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1426 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1427 /* Push run onto pending-runs stack, and maybe merge. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1428 assert (ms.n < MAX_MERGE_PENDING);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1429 ms.pending[ms.n].base = lo;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1430 ms.pending[ms.n].len = n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1431 ++ms.n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1432 if (merge_collapse (data, comp) < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1433 goto fail;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1434 /* Advance to find next run. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1435 lo += n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1436 nremaining -= n;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1437 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1438 while (nremaining);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1439
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1440 merge_force_collapse (data, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1441 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1442
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1443 fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1444 return;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1445 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1446
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1447 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1448 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1449 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1450 octave_sort<T>::sort (T *data, octave_idx_type *idx, octave_idx_type nel,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1451 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1452 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1453 /* Re-initialize the Mergestate as this might be the second time called */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1454 ms.n = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1455 ms.min_gallop = MIN_GALLOP;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1456
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1457 if (nel > 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1458 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1459 octave_idx_type nremaining = nel;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1460 octave_idx_type lo = 0;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1461
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1462 /* March over the array once, left to right, finding natural runs,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1463 * and extending short natural runs to minrun elements.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1464 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1465 octave_idx_type minrun = merge_compute_minrun (nremaining);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1466 do
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1467 {
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
1468 bool descending;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1469 octave_idx_type n;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1470
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1471 /* Identify next run. */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1472 n = count_run (data + lo, nremaining, descending, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1473 if (n < 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1474 goto fail;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1475 if (descending)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1476 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1477 std::reverse (data + lo, data + lo + n);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1478 std::reverse (idx + lo, idx + lo + n);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1479 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1480 /* If short, extend to min(minrun, nremaining). */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1481 if (n < minrun)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1482 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1483 const octave_idx_type force = nremaining <= minrun ? nremaining : minrun;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1484 binarysort (data + lo, idx + lo, force, n, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1485 n = force;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1486 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1487 /* Push run onto pending-runs stack, and maybe merge. */
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1488 assert (ms.n < MAX_MERGE_PENDING);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1489 ms.pending[ms.n].base = lo;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1490 ms.pending[ms.n].len = n;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1491 ++ms.n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1492 if (merge_collapse (data, idx, comp) < 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1493 goto fail;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1494 /* Advance to find next run. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1495 lo += n;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1496 nremaining -= n;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1497 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1498 while (nremaining);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1499
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1500 merge_force_collapse (data, idx, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1501 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1502
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1503 fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1504 return;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1505 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1506
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1507 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1508 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1509 octave_sort<T>::sort (T *data, octave_idx_type nel)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1510 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1511 #ifdef INLINE_ASCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1512 if (compare == ascending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1513 sort (data, nel, std::less<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1514 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1515 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1516 #ifdef INLINE_DESCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1517 if (compare == descending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1518 sort (data, nel, std::greater<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1519 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1520 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1521 if (compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1522 sort (data, nel, compare);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1523 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1524
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1525 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1526 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1527 octave_sort<T>::sort (T *data, octave_idx_type *idx, octave_idx_type nel)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1528 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1529 #ifdef INLINE_ASCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1530 if (compare == ascending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1531 sort (data, idx, nel, std::less<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1532 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1533 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1534 #ifdef INLINE_DESCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1535 if (compare == descending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1536 sort (data, idx, nel, std::greater<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1537 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1538 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1539 if (compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1540 sort (data, idx, nel, compare);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1541 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1542
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1543 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1544 bool
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1545 octave_sort<T>::ascending_compare (T x, T y)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1546 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1547 return x < y;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1548 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1549
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1550 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1551 bool
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1552 octave_sort<T>::descending_compare (T x, T y)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1553 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1554 return x > y;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1555 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1556
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1557 /*
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1558 ;;; Local Variables: ***
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1559 ;;; mode: C++ ***
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1560 ;;; End: ***
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1561 */