annotate liboctave/oct-sort.cc @ 14685:4460c4fb20e6 stable rc-3-6-2-2

3.6.2-rc2 release candidate * configure.ac (AC_INIT): Version is now 3.6.2-rc2.
author John W. Eaton <jwe@octave.org>
date Thu, 24 May 2012 15:35:50 -0400
parents 72c96de7a403
children 3d8ace26c5b4
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 /*
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10521
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13218
diff changeset
3 Copyright (C) 2003-2012 David Bateman
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10521
diff changeset
4 Copyright (C) 2008-2009 Jaroslav Hajek
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10521
diff changeset
5 Copyright (C) 2009-2010 VZLU Prague
4851
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 This file is part of Octave.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
8
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
9 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
10 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
11 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
12 option) any later version.
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
13
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
14 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
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
16 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
17 for more details.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
18
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
19 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
20 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
21 <http://www.gnu.org/licenses/>.
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
22
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
23 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
24 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
25 code I ripped-off.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
26
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
27 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
28 made are
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
29
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
30 * convert the sorting code in listobject.cc into a generic class,
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
31 replacing PyObject* with the type of the class T.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
32
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
33 * 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
34 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
35 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
36 If not, std::copy_backward needs to be used.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
37
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
38 * templatize comparison operator in most methods, provide possible dispatch
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
39
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
40 * duplicate methods to avoid by-the-way indexed sorting
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
41
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
42 * add methods for verifying sortedness of array
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
43
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
44 * row sorting via breadth-first tree subsorting
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
45
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
46 * binary lookup and sequential binary lookup optimized for dense downsampling.
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
47
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
48 * NOTE: the memory management routines rely on the fact that delete [] silently
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
49 ignores null pointers. Don't gripe about the missing checks - they're there.
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
50
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
51
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
52 The Python license is
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
53
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
54 PSF LICENSE AGREEMENT FOR PYTHON 2.3
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
55 --------------------------------------
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
56
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
57 1. This LICENSE AGREEMENT is between the Python Software Foundation
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
58 ("PSF"), and the Individual or Organization ("Licensee") accessing and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
59 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
60 associated documentation.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
61
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
62 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
63 hereby grants Licensee a nonexclusive, royalty-free, world-wide
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
64 license to reproduce, analyze, test, perform and/or display publicly,
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
65 prepare derivative works, distribute, and otherwise use Python 2.3
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
66 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
67 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
68 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
69 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
70 Licensee.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
71
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
72 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
73 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
74 the derivative work available to others as provided herein, then
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
75 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
76 the changes made to Python 2.3.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
77
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
78 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
79 basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
80 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
81 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
82 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
83 INFRINGE ANY THIRD PARTY RIGHTS.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
84
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
85 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
86 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
87 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
88 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
89
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
90 6. This License Agreement will automatically terminate upon a material
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
91 breach of its terms and conditions.
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 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
94 relationship of agency, partnership, or joint venture between PSF and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
95 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
96 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
97 products or services of Licensee, or any third party.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
98
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
99 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
100 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
101 Agreement.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
102 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
103
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
104 #ifdef HAVE_CONFIG_H
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
105 #include <config.h>
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
106 #endif
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
107
5883
1de9a198a303 [project @ 2006-07-14 18:10:30 by jwe]
jwe
parents: 5760
diff changeset
108 #include <cassert>
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
109 #include <algorithm>
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
110 #include <functional>
7480
93826ba0d078 compilation fixes
Jason Riedy
parents: 7433
diff changeset
111 #include <cstring>
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
112 #include <stack>
5883
1de9a198a303 [project @ 2006-07-14 18:10:30 by jwe]
jwe
parents: 5760
diff changeset
113
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
114 #include "lo-mappers.h"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
115 #include "quit.h"
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
116 #include "oct-sort.h"
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
117 #include "oct-locbuf.h"
4851
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>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
120 octave_sort<T>::octave_sort (void) :
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
121 compare (ascending_compare), ms (0)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
122 {
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
123 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
124
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
125 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
126 octave_sort<T>::octave_sort (compare_fcn_type comp)
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
127 : compare (comp), ms (0)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
128 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
129 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
130
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
131 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
132 octave_sort<T>::~octave_sort ()
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
133 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
134 delete ms;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
135 }
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
136
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
137 template <class T>
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
138 void
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
139 octave_sort<T>::set_compare (sortmode mode)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
140 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
141 if (mode == ASCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
142 compare = ascending_compare;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
143 else if (mode == DESCENDING)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
144 compare = descending_compare;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
145 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
146 compare = 0;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
147 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
148
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
149 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
150 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
151 void
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
152 octave_sort<T>::binarysort (T *data, octave_idx_type nel,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
153 octave_idx_type start, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
154 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
155 if (start == 0)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
156 ++start;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
157
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
158 for (; start < nel; ++start)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
159 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
160 /* set l to where *start belongs */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
161 octave_idx_type l = 0, r = start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
162 T pivot = data[start];
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
163 /* Invariants:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
164 * pivot >= all in [lo, l).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
165 * pivot < all in [r, start).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
166 * The second is vacuously true at the start.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
167 */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
168 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
169 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
170 octave_idx_type p = l + ((r - l) >> 1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
171 if (comp (pivot, data[p]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
172 r = p;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
173 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
174 l = p+1;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
175 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
176 while (l < r);
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
177 /* The invariants still hold, so pivot >= all in [lo, l) and
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
178 pivot < all in [l, start), so pivot belongs at l. Note
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
179 that if there are elements equal to pivot, l points to the
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
180 first slot after them -- that's why this sort is stable.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
181 Slide over to make room.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
182 Caution: using memmove is much slower under MSVC 5;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
183 we're not usually moving many slots. */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
184 // NOTE: using swap and going upwards appears to be faster.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
185 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
186 std::swap (pivot, data[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
187 data[start] = pivot;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
188 }
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 return;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
191 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
192
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
193 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
194 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
195 void
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
196 octave_sort<T>::binarysort (T *data, octave_idx_type *idx, octave_idx_type nel,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
197 octave_idx_type start, Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
198 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
199 if (start == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
200 ++start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
201
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
202 for (; start < nel; ++start)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
203 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
204 /* set l to where *start belongs */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
205 octave_idx_type l = 0, r = start;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
206 T pivot = data[start];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
207 /* Invariants:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
208 * pivot >= all in [lo, l).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
209 * pivot < all in [r, start).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
210 * The second is vacuously true at the start.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
211 */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
212 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
213 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
214 octave_idx_type p = l + ((r - l) >> 1);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
215 if (comp (pivot, data[p]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
216 r = p;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
217 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
218 l = p+1;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
219 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
220 while (l < r);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
221 /* The invariants still hold, so pivot >= all in [lo, l) and
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
222 pivot < all in [l, start), so pivot belongs at l. Note
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
223 that if there are elements equal to pivot, l points to the
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
224 first slot after them -- that's why this sort is stable.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
225 Slide over to make room.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
226 Caution: using memmove is much slower under MSVC 5;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
227 we're not usually moving many slots. */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
228 // NOTE: using swap and going upwards appears to be faster.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
229 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
230 std::swap (pivot, data[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
231 data[start] = pivot;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
232 octave_idx_type ipivot = idx[start];
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
233 for (octave_idx_type p = l; p < start; p++)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
234 std::swap (ipivot, idx[p]);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
235 idx[start] = ipivot;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
236 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
237
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
238 return;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
239 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
240
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 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
243 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
244
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
245 lo[0] <= lo[1] <= lo[2] <= ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
246
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
247 or the longest descending sequence, with
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
248
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
249 lo[0] > lo[1] > lo[2] > ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
250
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
251 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
252 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
253 "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
254 sequence without violating stability (strict > ensures there are no equal
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
255 elements to get out of order).
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 Returns -1 in case of error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
258 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
259 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
260 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
261 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
262 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
263 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
264 octave_idx_type n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
265 T *hi = lo + nel;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
266
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
267 descending = false;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
268 ++lo;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
269 if (lo == hi)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
270 return 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
271
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
272 n = 2;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
273
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
274 if (comp (*lo, *(lo-1)))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
275 {
7929
30b952e90c29 misc 64-bit fixes
John W. Eaton <jwe@octave.org>
parents: 7480
diff changeset
276 descending = true;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
277 for (lo = lo+1; lo < hi; ++lo, ++n)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
278 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
279 if (comp (*lo, *(lo-1)))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
280 ;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
281 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
282 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
283 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
284 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
285 else
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
286 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
287 for (lo = lo+1; lo < hi; ++lo, ++n)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
288 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
289 if (comp (*lo, *(lo-1)))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
290 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
291 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
292 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
293
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
294 return n;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
295 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
296
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 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
299 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
300 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
301 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
302
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
303 "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
304
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
305 "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
306 hint is to the final result, the faster this runs.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
307
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
308 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
309
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
310 a[k-1] < key <= a[k]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
311
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
312 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
313 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
314 key, and the last n-k should follow key.
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 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
317 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
318 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
319 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
320 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
321 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
322 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
323 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
324 octave_idx_type ofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
325 octave_idx_type lastofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
326 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
327
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
328 a += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
329 lastofs = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
330 ofs = 1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
331 if (comp (*a, key))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
332 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
333 /* a[hint] < key -- gallop right, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
334 * a[hint + lastofs] < key <= a[hint + ofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
335 */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
336 const octave_idx_type maxofs = n - hint; /* &a[n-1] is highest */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
337 while (ofs < maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
338 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
339 if (comp (a[ofs], key))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
340 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
341 lastofs = ofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
342 ofs = (ofs << 1) + 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
343 if (ofs <= 0) /* int overflow */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
344 ofs = maxofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
345 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
346 else /* key <= a[hint + ofs] */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
347 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
348 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
349 if (ofs > maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
350 ofs = maxofs;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
351 /* Translate back to offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
352 lastofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
353 ofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
354 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
355 else
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
356 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
357 /* key <= a[hint] -- gallop left, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
358 * a[hint - ofs] < key <= a[hint - lastofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
359 */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
360 const octave_idx_type maxofs = hint + 1; /* &a[0] is lowest */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
361 while (ofs < maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
362 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
363 if (comp (*(a-ofs), key))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
364 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
365 /* key <= a[hint - ofs] */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
366 lastofs = ofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
367 ofs = (ofs << 1) + 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
368 if (ofs <= 0) /* int overflow */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
369 ofs = maxofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
370 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
371 if (ofs > maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
372 ofs = maxofs;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
373 /* Translate back to positive offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
374 k = lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
375 lastofs = hint - ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
376 ofs = hint - k;
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 a -= hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
379
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
380 /* 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
381 * 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
382 * search, with invariant a[lastofs-1] < key <= a[ofs].
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
383 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
384 ++lastofs;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
385 while (lastofs < ofs)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
386 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
387 octave_idx_type m = lastofs + ((ofs - lastofs) >> 1);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
388
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
389 if (comp (a[m], key))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
390 lastofs = m+1; /* a[m] < key */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
391 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
392 ofs = m; /* key <= a[m] */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
393 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
394
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
395 return ofs;
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
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
398 /*
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
399 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
400 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
401
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
402 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
403
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
404 a[k-1] <= key < a[k]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
405
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
406 or -1 if error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
407
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
408 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
409 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
410 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
411 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
412 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
413 template <class Comp>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
414 octave_idx_type
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
415 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
416 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
417 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
418 octave_idx_type ofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
419 octave_idx_type lastofs;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
420 octave_idx_type k;
4851
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;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
423 lastofs = 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
424 ofs = 1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
425 if (comp (key, *a))
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
426 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
427 /* key < a[hint] -- gallop left, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
428 * a[hint - ofs] <= key < a[hint - lastofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
429 */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
430 const octave_idx_type maxofs = hint + 1; /* &a[0] is lowest */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
431 while (ofs < maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
432 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
433 if (comp (key, *(a-ofs)))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
434 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
435 lastofs = ofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
436 ofs = (ofs << 1) + 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
437 if (ofs <= 0) /* int overflow */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
438 ofs = maxofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
439 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
440 else /* a[hint - ofs] <= key */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
441 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
442 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
443 if (ofs > maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
444 ofs = maxofs;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
445 /* Translate back to positive offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
446 k = lastofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
447 lastofs = hint - ofs;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
448 ofs = hint - k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
449 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
450 else
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
451 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
452 /* a[hint] <= key -- gallop right, until
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
453 * a[hint + lastofs] <= key < a[hint + ofs]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
454 */
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
455 const octave_idx_type maxofs = n - hint; /* &a[n-1] is highest */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
456 while (ofs < maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
457 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
458 if (comp (key, a[ofs]))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
459 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
460 /* a[hint + ofs] <= key */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
461 lastofs = ofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
462 ofs = (ofs << 1) + 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
463 if (ofs <= 0) /* int overflow */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
464 ofs = maxofs;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
465 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
466 if (ofs > maxofs)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
467 ofs = maxofs;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
468 /* Translate back to offsets relative to &a[0]. */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
469 lastofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
470 ofs += hint;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
471 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
472 a -= hint;
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 /* 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
475 * 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
476 * search, with invariant a[lastofs-1] <= key < a[ofs].
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 ++lastofs;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
479 while (lastofs < ofs)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
480 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
481 octave_idx_type m = lastofs + ((ofs - lastofs) >> 1);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
482
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
483 if (comp (key, a[m]))
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
484 ofs = m; /* key < a[m] */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
485 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
486 lastofs = m+1; /* a[m] <= key */
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
487 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
488
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
489 return ofs;
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
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
492 static inline octave_idx_type
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
493 roundupsize (octave_idx_type n)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
494 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
495 unsigned int nbits = 3;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
496 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
497
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
498 /* Round up:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
499 * If n < 256, to a multiple of 8.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
500 * If n < 2048, to a multiple of 64.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
501 * If n < 16384, to a multiple of 512.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
502 * If n < 131072, to a multiple of 4096.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
503 * If n < 1048576, to a multiple of 32768.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
504 * If n < 8388608, to a multiple of 262144.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
505 * If n < 67108864, to a multiple of 2097152.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
506 * If n < 536870912, to a multiple of 16777216.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
507 * ...
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
508 * 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
509 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
510 * This over-allocates proportional to the list size, making room
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
511 * for additional growth. The over-allocation is mild, but is
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
512 * enough to give linear-time amortized behavior over a long
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
513 * sequence of appends() in the presence of a poorly-performing
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
514 * 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
515 * of Windows, with Win9x behavior being particularly bad -- and
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
516 * we've still got address space fragmentation problems on Win9x
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
517 * 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
518 * provoke them than it used to).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
519 */
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
520 while (n2)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
521 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
522 n2 >>= 3;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
523 nbits += 3;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
524 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
525
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
526 return ((n >> nbits) + 1) << nbits;
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
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
529 /* Ensure enough temp memory for 'need' array slots is available.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
530 * 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
531 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
532 template <class T>
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
533 void
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
534 octave_sort<T>::MergeState::getmem (octave_idx_type need)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
535 {
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
536 if (need <= alloced)
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
537 return;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
538
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
539 need = roundupsize (need);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
540 /* 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
541 * we don't care what's in the block.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
542 */
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
543 delete [] a;
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
544 delete [] ia; // Must do this or fool possible next getmemi.
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
545 a = new T[need];
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
546 alloced = need;
7234
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 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
549
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
550 template <class T>
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
551 void
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
552 octave_sort<T>::MergeState::getmemi (octave_idx_type need)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
553 {
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
554 if (ia && need <= alloced)
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
555 return;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
556
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
557 need = roundupsize (need);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
558 /* 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
559 * we don't care what's in the block.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
560 */
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
561 delete [] a;
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
562 delete [] ia;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
563
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
564 a = new T[need];
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
565 ia = new octave_idx_type[need];
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
566 alloced = need;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
567 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
568
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
569 /* 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
570 * 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
571 * 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
572 * 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
573 * Return 0 if successful, -1 if error.
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 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
576 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
577 int
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
578 octave_sort<T>::merge_lo (T *pa, octave_idx_type na,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
579 T *pb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
580 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
581 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
582 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
583 T *dest;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
584 int result = -1; /* guilty until proved innocent */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
585 octave_idx_type min_gallop = ms->min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
586
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
587 ms->getmem (na);
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
588
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
589 std::copy (pa, pa + na, ms->a);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
590 dest = pa;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
591 pa = ms->a;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
592
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
593 *dest++ = *pb++;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
594 --nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
595 if (nb == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
596 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
597 if (na == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
598 goto CopyB;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
599
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
600 for (;;)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
601 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
602 octave_idx_type acount = 0; /* # of times A won in a row */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
603 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
604
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
605 /* Do the straightforward thing until (if ever) one run
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
606 * appears to win consistently.
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
607 */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
608 for (;;)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
609 {
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
610
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
611 // FIXME: these loops are candidates for further optimizations.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
612 // Rather than testing everything in each cycle, it may be more
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
613 // efficient to do it in hunks.
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
614 if (comp (*pb, *pa))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
615 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
616 *dest++ = *pb++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
617 ++bcount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
618 acount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
619 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
620 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
621 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
622 if (bcount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
623 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
624 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
625 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
626 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
627 *dest++ = *pa++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
628 ++acount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
629 bcount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
630 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
631 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
632 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
633 if (acount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
634 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
635 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
636 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
637
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
638 /* One run is winning so consistently that galloping may
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
639 * 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
640 * (if ever) neither run appears to be winning consistently
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
641 * anymore.
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
642 */
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
643 ++min_gallop;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
644 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
645 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
646 min_gallop -= min_gallop > 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
647 ms->min_gallop = min_gallop;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
648 k = gallop_right (*pb, pa, na, 0, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
649 acount = k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
650 if (k)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
651 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
652 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
653 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
654 dest = std::copy (pa, pa + k, dest);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
655 pa += k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
656 na -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
657 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
658 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
659 /* na==0 is impossible now if the comparison
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
660 * function is consistent, but we can't assume
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
661 * that it is.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
662 */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
663 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
664 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
665 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
666 *dest++ = *pb++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
667 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
668 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
669 goto Succeed;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
670
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
671 k = gallop_left (*pa, pb, nb, 0, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
672 bcount = k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
673 if (k)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
674 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
675 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
676 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
677 dest = std::copy (pb, pb + k, dest);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
678 pb += k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
679 nb -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
680 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
681 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
682 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
683 *dest++ = *pa++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
684 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
685 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
686 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
687 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
688 while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
689
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
690 ++min_gallop; /* penalize it for leaving galloping mode */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
691 ms->min_gallop = min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
692 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
693
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
694 Succeed:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
695 result = 0;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
696
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
697 Fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
698 if (na)
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
699 std::copy (pa, pa + na, dest);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
700 return result;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
701
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
702 CopyB:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
703 /* 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
704 std::copy (pb, pb + nb, dest);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
705 dest[nb] = *pa;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
706
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
707 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
708 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
709
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
710 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
711 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
712 int
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
713 octave_sort<T>::merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
714 T *pb, octave_idx_type *ipb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
715 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
716 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
717 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
718 T *dest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
719 octave_idx_type *idest;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
720 int result = -1; /* guilty until proved innocent */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
721 octave_idx_type min_gallop = ms->min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
722
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
723 ms->getmemi (na);
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
724
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
725 std::copy (pa, pa + na, ms->a);
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
726 std::copy (ipa, ipa + na, ms->ia);
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
727 dest = pa; idest = ipa;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
728 pa = ms->a; ipa = ms->ia;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
729
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
730 *dest++ = *pb++; *idest++ = *ipb++;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
731 --nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
732 if (nb == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
733 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
734 if (na == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
735 goto CopyB;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
736
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
737 for (;;)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
738 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
739 octave_idx_type acount = 0; /* # of times A won in a row */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
740 octave_idx_type bcount = 0; /* # of times B won in a row */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
741
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
742 /* Do the straightforward thing until (if ever) one run
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
743 * appears to win consistently.
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 for (;;)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
746 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
747
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
748 if (comp (*pb, *pa))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
749 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
750 *dest++ = *pb++; *idest++ = *ipb++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
751 ++bcount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
752 acount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
753 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
754 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
755 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
756 if (bcount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
757 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
758 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
759 else
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
760 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
761 *dest++ = *pa++; *idest++ = *ipa++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
762 ++acount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
763 bcount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
764 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
765 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
766 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
767 if (acount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
768 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
769 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
770 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
771
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
772 /* One run is winning so consistently that galloping may
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
773 * be a huge win. So try that, and continue galloping until
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
774 * (if ever) neither run appears to be winning consistently
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
775 * anymore.
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 ++min_gallop;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
778 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
779 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
780 min_gallop -= min_gallop > 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
781 ms->min_gallop = min_gallop;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
782 k = gallop_right (*pb, pa, na, 0, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
783 acount = k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
784 if (k)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
785 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
786 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
787 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
788 dest = std::copy (pa, pa + k, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
789 idest = std::copy (ipa, ipa + k, idest);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
790 pa += k; ipa += k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
791 na -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
792 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
793 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
794 /* na==0 is impossible now if the comparison
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
795 * function is consistent, but we can't assume
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
796 * that it is.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
797 */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
798 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
799 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
800 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
801 *dest++ = *pb++; *idest++ = *ipb++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
802 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
803 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
804 goto Succeed;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
805
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
806 k = gallop_left (*pa, pb, nb, 0, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
807 bcount = k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
808 if (k)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
809 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
810 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
811 goto Fail;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
812 dest = std::copy (pb, pb + k, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
813 idest = std::copy (ipb, ipb + k, idest);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
814 pb += k; ipb += k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
815 nb -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
816 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
817 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
818 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
819 *dest++ = *pa++; *idest++ = *ipa++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
820 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
821 if (na == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
822 goto CopyB;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
823 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
824 while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
825
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
826 ++min_gallop; /* penalize it for leaving galloping mode */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
827 ms->min_gallop = min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
828 }
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 Succeed:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
831 result = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
832
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
833 Fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
834 if (na)
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 std::copy (pa, pa + na, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
837 std::copy (ipa, ipa + na, idest);
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 return result;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
840
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
841 CopyB:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
842 /* The last element of pa belongs at the end of the merge. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
843 std::copy (pb, pb + nb, dest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
844 std::copy (ipb, ipb + nb, idest);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
845 dest[nb] = *pa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
846 idest[nb] = *ipa;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
847
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
848 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
849 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
850
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
851 /* 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
852 * 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
853 * 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
854 * 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
855 * Return 0 if successful, -1 if error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
856 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
857 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
858 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
859 int
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
860 octave_sort<T>::merge_hi (T *pa, octave_idx_type na,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
861 T *pb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
862 Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
863 {
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
864 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
865 T *dest;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
866 int result = -1; /* guilty until proved innocent */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
867 T *basea, *baseb;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
868 octave_idx_type min_gallop = ms->min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
869
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
870 ms->getmem (nb);
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
871
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
872 dest = pb + nb - 1;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
873 std::copy (pb, pb + nb, ms->a);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
874 basea = pa;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
875 baseb = ms->a;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
876 pb = ms->a + nb - 1;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
877 pa += na - 1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
878
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
879 *dest-- = *pa--;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
880 --na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
881 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
882 goto Succeed;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
883 if (nb == 1)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
884 goto CopyA;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
885
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
886 for (;;)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
887 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
888 octave_idx_type acount = 0; /* # of times A won in a row */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
889 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
890
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
891 /* Do the straightforward thing until (if ever) one run
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
892 * appears to win consistently.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
893 */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
894 for (;;)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
895 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
896 if (comp (*pb, *pa))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
897 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
898 *dest-- = *pa--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
899 ++acount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
900 bcount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
901 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
902 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
903 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
904 if (acount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
905 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
906 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
907 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
908 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
909 *dest-- = *pb--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
910 ++bcount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
911 acount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
912 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
913 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
914 goto CopyA;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
915 if (bcount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
916 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
917 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
918 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
919
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
920 /* One run is winning so consistently that galloping may
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
921 * 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
922 * (if ever) neither run appears to be winning consistently
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
923 * anymore.
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 ++min_gallop;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
926 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
927 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
928 min_gallop -= min_gallop > 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
929 ms->min_gallop = min_gallop;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
930 k = gallop_right (*pb, basea, na, na-1, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
931 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
932 goto Fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
933 k = na - k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
934 acount = k;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
935 if (k)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
936 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
937 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
938 pa -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
939 na -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
940 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
941 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
942 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
943 *dest-- = *pb--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
944 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
945 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
946 goto CopyA;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
947
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
948 k = gallop_left (*pa, baseb, nb, nb-1, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
949 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
950 goto Fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
951 k = nb - k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
952 bcount = k;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
953 if (k)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
954 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
955 dest -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
956 pb -= k;
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
957 std::copy (pb+1, pb+1 + k, dest+1);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
958 nb -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
959 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
960 goto CopyA;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
961 /* nb==0 is impossible now if the comparison
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
962 * function is consistent, but we can't assume
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
963 * that it is.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
964 */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
965 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
966 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
967 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
968 *dest-- = *pa--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
969 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
970 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
971 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
972 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
973 ++min_gallop; /* penalize it for leaving galloping mode */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
974 ms->min_gallop = min_gallop;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
975 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
976
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
977 Succeed:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
978 result = 0;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
979
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
980 Fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
981 if (nb)
8206
0168d22e6bba fix sorting of non-POD objects
Jaroslav Hajek <highegg@gmail.com>
parents: 7929
diff changeset
982 std::copy (baseb, baseb + nb, dest-(nb-1));
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
983 return result;
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
984
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
985 CopyA:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
986 /* 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
987 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
988 pa -= na;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
989 *dest = *pb;
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 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
992 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
993
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
994 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
995 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
996 int
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
997 octave_sort<T>::merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
998 T *pb, octave_idx_type *ipb, octave_idx_type nb,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
999 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1000 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1001 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1002 T *dest;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1003 octave_idx_type *idest;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1004 int result = -1; /* guilty until proved innocent */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1005 T *basea, *baseb;
13218
01b7a60e2ff0 fix warnings for unused but set variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1006 octave_idx_type *ibaseb;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1007 octave_idx_type min_gallop = ms->min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1008
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1009 ms->getmemi (nb);
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1010
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1011 dest = pb + nb - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1012 idest = ipb + nb - 1;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1013 std::copy (pb, pb + nb, ms->a);
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1014 std::copy (ipb, ipb + nb, ms->ia);
13218
01b7a60e2ff0 fix warnings for unused but set variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
1015 basea = pa;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1016 baseb = ms->a; ibaseb = ms->ia;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1017 pb = ms->a + nb - 1; ipb = ms->ia + nb - 1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1018 pa += na - 1; ipa += na - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1019
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1020 *dest-- = *pa--; *idest-- = *ipa--;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1021 --na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1022 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1023 goto Succeed;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1024 if (nb == 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1025 goto CopyA;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1026
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1027 for (;;)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1028 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1029 octave_idx_type acount = 0; /* # of times A won in a row */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1030 octave_idx_type bcount = 0; /* # of times B won in a row */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1031
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1032 /* Do the straightforward thing until (if ever) one run
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1033 * appears to win consistently.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1034 */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1035 for (;;)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1036 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1037 if (comp (*pb, *pa))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1038 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1039 *dest-- = *pa--; *idest-- = *ipa--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1040 ++acount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1041 bcount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1042 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1043 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1044 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1045 if (acount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1046 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1047 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1048 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1049 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1050 *dest-- = *pb--; *idest-- = *ipb--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1051 ++bcount;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1052 acount = 0;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1053 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1054 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1055 goto CopyA;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1056 if (bcount >= min_gallop)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1057 break;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1058 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1059 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1060
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1061 /* One run is winning so consistently that galloping may
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1062 * be a huge win. So try that, and continue galloping until
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1063 * (if ever) neither run appears to be winning consistently
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1064 * anymore.
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 ++min_gallop;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1067 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1068 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1069 min_gallop -= min_gallop > 1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1070 ms->min_gallop = min_gallop;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1071 k = gallop_right (*pb, basea, na, na-1, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1072 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1073 goto Fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1074 k = na - k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1075 acount = k;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1076 if (k)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1077 {
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1078 dest = std::copy_backward (pa+1 - k, pa+1, dest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1079 idest = std::copy_backward (ipa+1 - k, ipa+1, idest+1) - 1;
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1080 pa -= k; ipa -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1081 na -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1082 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1083 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1084 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1085 *dest-- = *pb--; *idest-- = *ipb--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1086 --nb;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1087 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1088 goto CopyA;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1089
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1090 k = gallop_left (*pa, baseb, nb, nb-1, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1091 if (k < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1092 goto Fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1093 k = nb - k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1094 bcount = k;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1095 if (k)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1096 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1097 dest -= k; idest -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1098 pb -= k; ipb -= k;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1099 std::copy (pb+1, pb+1 + k, dest+1);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1100 std::copy (ipb+1, ipb+1 + k, idest+1);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1101 nb -= k;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1102 if (nb == 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1103 goto CopyA;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1104 /* nb==0 is impossible now if the comparison
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1105 * function is consistent, but we can't assume
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1106 * that it is.
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1107 */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1108 if (nb == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1109 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1110 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1111 *dest-- = *pa--; *idest-- = *ipa--;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1112 --na;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1113 if (na == 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1114 goto Succeed;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1115 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1116 ++min_gallop; /* penalize it for leaving galloping mode */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1117 ms->min_gallop = min_gallop;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1118 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1119
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1120 Succeed:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1121 result = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1122
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1123 Fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1124 if (nb)
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 std::copy (baseb, baseb + nb, dest-(nb-1));
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1127 std::copy (ibaseb, ibaseb + nb, idest-(nb-1));
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 return result;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1130
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1131 CopyA:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1132 /* The first element of pb belongs at the front of the merge. */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1133 dest = std::copy_backward (pa+1 - na, pa+1, dest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1134 idest = std::copy_backward (ipa+1 - na, ipa+1, idest+1) - 1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1135 pa -= na; ipa -= na;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1136 *dest = *pb; *idest = *ipb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1137
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1138 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1139 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1140
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1141 /* 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
1142 * Returns 0 on success, -1 on error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1143 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1144 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1145 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1146 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1147 octave_sort<T>::merge_at (octave_idx_type i, T *data,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1148 Comp comp)
4851
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 T *pa, *pb;
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1151 octave_idx_type na, nb;
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1152 octave_idx_type k;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1153
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1154 pa = data + ms->pending[i].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1155 na = ms->pending[i].len;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1156 pb = data + ms->pending[i+1].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1157 nb = ms->pending[i+1].len;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1158
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1159 /* 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
1160 * 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
1161 * 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
1162 */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1163 ms->pending[i].len = na + nb;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1164 if (i == ms->n - 3)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1165 ms->pending[i+1] = ms->pending[i+2];
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1166 ms->n--;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1167
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1168 /* 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
1169 * ignored (already in place).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1170 */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1171 k = gallop_right (*pb, pa, na, 0, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1172 if (k < 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1173 return -1;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1174 pa += k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1175 na -= k;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1176 if (na == 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1177 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1178
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1179 /* 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
1180 * ignored (already in place).
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1181 */
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1182 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
1183 if (nb <= 0)
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1184 return nb;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1185
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1186 /* 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
1187 * min(na, nb) elements.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1188 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1189 if (na <= nb)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1190 return merge_lo (pa, na, pb, nb, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1191 else
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1192 return merge_hi (pa, na, pb, nb, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1193 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1194
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1195 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1196 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1197 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1198 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
1199 Comp comp)
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 T *pa, *pb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1202 octave_idx_type *ipa, *ipb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1203 octave_idx_type na, nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1204 octave_idx_type k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1205
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1206 pa = data + ms->pending[i].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1207 ipa = idx + ms->pending[i].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1208 na = ms->pending[i].len;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1209 pb = data + ms->pending[i+1].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1210 ipb = idx + ms->pending[i+1].base;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1211 nb = ms->pending[i+1].len;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1212
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1213 /* 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
1214 * run now, also slide over the last run (which isn't involved
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1215 * 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
1216 */
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1217 ms->pending[i].len = na + nb;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1218 if (i == ms->n - 3)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1219 ms->pending[i+1] = ms->pending[i+2];
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1220 ms->n--;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1221
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1222 /* 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
1223 * ignored (already in place).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1224 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1225 k = gallop_right (*pb, pa, na, 0, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1226 if (k < 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1227 return -1;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1228 pa += k; ipa += k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1229 na -= k;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1230 if (na == 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1231 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1232
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1233 /* 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
1234 * ignored (already in place).
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1235 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1236 nb = gallop_left (pa[na-1], pb, nb, nb-1, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1237 if (nb <= 0)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1238 return nb;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1239
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1240 /* Merge what remains of the runs, using a temp array with
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1241 * min(na, nb) elements.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1242 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1243 if (na <= nb)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1244 return merge_lo (pa, ipa, na, pb, ipb, nb, comp);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1245 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1246 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
1247 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1248
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1249 /* 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
1250 * until the stack invariants are re-established:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1251 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1252 * 1. len[-3] > len[-2] + len[-1]
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1253 * 2. len[-2] > len[-1]
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 * See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1256 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1257 * Returns 0 on success, -1 on error.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1258 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1259 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1260 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1261 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1262 octave_sort<T>::merge_collapse (T *data, Comp comp)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1263 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1264 struct s_slice *p = ms->pending;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1265
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1266 while (ms->n > 1)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1267 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1268 octave_idx_type n = ms->n - 2;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1269 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1270 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1271 if (p[n-1].len < p[n+1].len)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1272 --n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1273 if (merge_at (n, data, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1274 return -1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1275 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1276 else if (p[n].len <= p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1277 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1278 if (merge_at (n, data, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1279 return -1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1280 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1281 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1282 break;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1283 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1284
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1285 return 0;
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
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1288 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1289 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1290 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1291 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
1292 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1293 struct s_slice *p = ms->pending;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1294
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1295 while (ms->n > 1)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1296 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1297 octave_idx_type n = ms->n - 2;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1298 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1299 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1300 if (p[n-1].len < p[n+1].len)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1301 --n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1302 if (merge_at (n, data, idx, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1303 return -1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1304 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1305 else if (p[n].len <= p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1306 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1307 if (merge_at (n, data, idx, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1308 return -1;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1309 }
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1310 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1311 break;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1312 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1313
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1314 return 0;
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
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1317 /* 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
1318 * remains. This is used at the end of the mergesort.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1319 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1320 * Returns 0 on success, -1 on error.
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 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1323 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1324 int
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1325 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
1326 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1327 struct s_slice *p = ms->pending;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1328
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1329 while (ms->n > 1)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1330 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1331 octave_idx_type n = ms->n - 2;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1332 if (n > 0 && p[n-1].len < p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1333 --n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1334 if (merge_at (n, data, comp) < 0)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1335 return -1;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1336 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1337
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1338 return 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1339 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1340
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1341 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1342 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1343 int
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1344 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
1345 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1346 struct s_slice *p = ms->pending;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1347
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1348 while (ms->n > 1)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1349 {
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1350 octave_idx_type n = ms->n - 2;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1351 if (n > 0 && p[n-1].len < p[n+1].len)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1352 --n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1353 if (merge_at (n, data, idx, comp) < 0)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1354 return -1;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1355 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1356
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1357 return 0;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1358 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1359
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1360 /* 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
1361 * than this are boosted artificially via binary insertion.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1362 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1363 * 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
1364 * 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
1365 * 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
1366 * strictly less than, an exact power of 2.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1367 *
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1368 * See listsort.txt for more info.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1369 */
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1370 template <class T>
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1371 octave_idx_type
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1372 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
1373 {
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1374 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
1375
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1376 while (n >= 64)
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1377 {
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1378 r |= n & 1;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1379 n >>= 1;
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1380 }
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1381
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1382 return n + r;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1383 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1384
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1385 template <class T>
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1386 template <class Comp>
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1387 void
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1388 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
1389 {
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1390 /* Re-initialize the Mergestate as this might be the second time called */
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1391 if (! ms) ms = new MergeState;
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1392
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1393 ms->reset ();
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1394 ms->getmem (1024);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1395
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1396 if (nel > 1)
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1397 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1398 octave_idx_type nremaining = nel;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1399 octave_idx_type lo = 0;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1400
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1401 /* March over the array once, left to right, finding natural runs,
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1402 * and extending short natural runs to minrun elements.
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1403 */
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1404 octave_idx_type minrun = merge_compute_minrun (nremaining);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1405 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1406 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1407 bool descending;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1408 octave_idx_type n;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1409
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1410 /* Identify next run. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1411 n = count_run (data + lo, nremaining, descending, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1412 if (n < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1413 goto fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1414 if (descending)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1415 std::reverse (data + lo, data + lo + n);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1416 /* If short, extend to min(minrun, nremaining). */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1417 if (n < minrun)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1418 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1419 const octave_idx_type force = nremaining <= minrun ? nremaining : minrun;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1420 binarysort (data + lo, force, n, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1421 n = force;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1422 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1423 /* Push run onto pending-runs stack, and maybe merge. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1424 assert (ms->n < MAX_MERGE_PENDING);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1425 ms->pending[ms->n].base = lo;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1426 ms->pending[ms->n].len = n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1427 ms->n++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1428 if (merge_collapse (data, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1429 goto fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1430 /* Advance to find next run. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1431 lo += n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1432 nremaining -= n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1433 }
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1434 while (nremaining);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1435
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1436 merge_force_collapse (data, comp);
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
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1439 fail:
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1440 return;
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 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1444 template <class Comp>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1445 void
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1446 octave_sort<T>::sort (T *data, octave_idx_type *idx, octave_idx_type nel,
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1447 Comp comp)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1448 {
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1449 /* Re-initialize the Mergestate as this might be the second time called */
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1450 if (! ms) ms = new MergeState;
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1451
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1452 ms->reset ();
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1453 ms->getmemi (1024);
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1454
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1455 if (nel > 1)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1456 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1457 octave_idx_type nremaining = nel;
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1458 octave_idx_type lo = 0;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1459
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1460 /* 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
1461 * and extending short natural runs to minrun elements.
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1462 */
7433
402168152bb9 [project @ 2008-01-31 18:59:09 by dbateman]
dbateman
parents: 7234
diff changeset
1463 octave_idx_type minrun = merge_compute_minrun (nremaining);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1464 do
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1465 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1466 bool descending;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1467 octave_idx_type n;
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1468
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1469 /* Identify next run. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1470 n = count_run (data + lo, nremaining, descending, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1471 if (n < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1472 goto fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1473 if (descending)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1474 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1475 std::reverse (data + lo, data + lo + n);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1476 std::reverse (idx + lo, idx + lo + n);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1477 }
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1478 /* If short, extend to min(minrun, nremaining). */
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1479 if (n < minrun)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1480 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1481 const octave_idx_type force = nremaining <= minrun ? nremaining : minrun;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1482 binarysort (data + lo, idx + lo, force, n, comp);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1483 n = force;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1484 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1485 /* Push run onto pending-runs stack, and maybe merge. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1486 assert (ms->n < MAX_MERGE_PENDING);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1487 ms->pending[ms->n].base = lo;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1488 ms->pending[ms->n].len = n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1489 ms->n++;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1490 if (merge_collapse (data, idx, comp) < 0)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1491 goto fail;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1492 /* Advance to find next run. */
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1493 lo += n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1494 nremaining -= n;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1495 }
7234
6992e9face25 [project @ 2007-11-30 20:45:42 by jwe]
jwe
parents: 7017
diff changeset
1496 while (nremaining);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1497
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1498 merge_force_collapse (data, idx, comp);
4851
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1499 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1500
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1501 fail:
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1502 return;
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1503 }
047ff938b0d9 [project @ 2004-04-06 17:12:14 by jwe]
jwe
parents:
diff changeset
1504
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1505 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1506 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1507 octave_sort<T>::sort (T *data, octave_idx_type nel)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1508 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1509 #ifdef INLINE_ASCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1510 if (compare == ascending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1511 sort (data, nel, std::less<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1512 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1513 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1514 #ifdef INLINE_DESCENDING_SORT
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1515 if (compare == descending_compare)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1516 sort (data, nel, std::greater<T> ());
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1517 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1518 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1519 if (compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1520 sort (data, nel, compare);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1521 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1522
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1523 template <class T>
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1524 void
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1525 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
1526 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1527 #ifdef INLINE_ASCENDING_SORT
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1528 if (compare == ascending_compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1529 sort (data, idx, nel, std::less<T> ());
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1530 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1531 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1532 #ifdef INLINE_DESCENDING_SORT
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1533 if (compare == descending_compare)
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1534 sort (data, idx, nel, std::greater<T> ());
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1535 else
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1536 #endif
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1537 if (compare)
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1538 sort (data, idx, nel, compare);
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1539 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1540
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1541 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1542 bool
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1543 octave_sort<T>::is_sorted (const T *data, octave_idx_type nel, Comp comp)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1544 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1545 const T *end = data + nel;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1546 if (data != end)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1547 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1548 const T *next = data;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1549 while (++next != end)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1550 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1551 if (comp (*next, *data))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1552 break;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1553 data = next;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1554 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1555 data = next;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1556 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1557
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1558 return data == end;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1559 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1560
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1561 template <class T>
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1562 bool
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1563 octave_sort<T>::is_sorted (const T *data, octave_idx_type nel)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1564 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1565 bool retval = false;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1566 #ifdef INLINE_ASCENDING_SORT
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1567 if (compare == ascending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1568 retval = is_sorted (data, nel, std::less<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1569 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1570 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1571 #ifdef INLINE_DESCENDING_SORT
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1572 if (compare == descending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1573 retval = is_sorted (data, nel, std::greater<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1574 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1575 #endif
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1576 if (compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1577 retval = is_sorted (data, nel, compare);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1578
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1579 return retval;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1580 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1581
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1582 // FIXME: is there really no way to make this local to the following function?
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1583 struct sortrows_run_t
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1584 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1585 sortrows_run_t (octave_idx_type c, octave_idx_type o, octave_idx_type n)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1586 : col (c), ofs (o), nel (n) { }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1587 octave_idx_type col, ofs, nel;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1588 };
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1589
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1590
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1591 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1592 void
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1593 octave_sort<T>::sort_rows (const T *data, octave_idx_type *idx,
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1594 octave_idx_type rows, octave_idx_type cols,
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1595 Comp comp)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1596 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1597 OCTAVE_LOCAL_BUFFER (T, buf, rows);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1598 for (octave_idx_type i = 0; i < rows; i++)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1599 idx[i] = i;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1600
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1601 if (cols == 0 || rows <= 1)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1602 return;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1603
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1604
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1605 // This is a breadth-first traversal.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1606 typedef sortrows_run_t run_t;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1607 std::stack<run_t> runs;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1608
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1609 runs.push (run_t (0, 0, rows));
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1610
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1611 while (! runs.empty ())
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1612 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1613 octave_idx_type col = runs.top ().col;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1614 octave_idx_type ofs = runs.top ().ofs;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1615 octave_idx_type nel = runs.top ().nel;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1616 runs.pop ();
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1617 assert (nel > 1);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1618
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1619 T *lbuf = buf + ofs;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1620 const T *ldata = data + rows*col;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1621 octave_idx_type *lidx = idx + ofs;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1622
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1623 // Gather.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1624 for (octave_idx_type i = 0; i < nel; i++)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1625 lbuf[i] = ldata[lidx[i]];
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1626
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1627 // Sort.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1628 sort (lbuf, lidx, nel, comp);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1629
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1630 // Identify constant runs and schedule subsorts.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1631 if (col < cols-1)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1632 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1633 octave_idx_type lst = 0;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1634 for (octave_idx_type i = 0; i < nel; i++)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1635 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1636 if (comp (lbuf[lst], lbuf[i]))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1637 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1638 if (i > lst + 1)
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1639 runs.push (run_t (col+1, ofs + lst, i - lst));
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1640 lst = i;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1641 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1642 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1643 if (nel > lst + 1)
8820
89b95972e178 fix previously introduced problem in octave_sort, improve design
Jaroslav Hajek <highegg@gmail.com>
parents: 8816
diff changeset
1644 runs.push (run_t (col+1, ofs + lst, nel - lst));
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1645 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1646 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1647 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1648
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1649 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1650 void
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1651 octave_sort<T>::sort_rows (const T *data, octave_idx_type *idx,
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1652 octave_idx_type rows, octave_idx_type cols)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1653 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1654 #ifdef INLINE_ASCENDING_SORT
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1655 if (compare == ascending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1656 sort_rows (data, idx, rows, cols, std::less<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1657 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1658 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1659 #ifdef INLINE_DESCENDING_SORT
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1660 if (compare == descending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1661 sort_rows (data, idx, rows, cols, std::greater<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1662 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1663 #endif
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1664 if (compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1665 sort_rows (data, idx, rows, cols, compare);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1666 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1667
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1668 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1669 bool
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1670 octave_sort<T>::is_sorted_rows (const T *data, octave_idx_type rows,
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1671 octave_idx_type cols, Comp comp)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1672 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1673 if (rows <= 1 || cols == 0)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1674 return true;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1675
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1676 // This is a breadth-first traversal.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1677 const T *lastrow = data + rows*(cols - 1);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1678 typedef std::pair<const T *, octave_idx_type> run_t;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1679 std::stack<run_t> runs;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1680
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1681 bool sorted = true;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1682 runs.push (run_t (data, rows));
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1683 while (sorted && ! runs.empty ())
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1684 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1685 const T *lo = runs.top ().first;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1686 octave_idx_type n = runs.top ().second;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1687 runs.pop ();
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1688 if (lo < lastrow)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1689 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1690 // Not the final column.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1691 assert (n > 1);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1692 const T *hi = lo + n, *lst = lo;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1693 for (lo++; lo < hi; lo++)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1694 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1695 if (comp (*lst, *lo))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1696 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1697 if (lo > lst + 1)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1698 runs.push (run_t (lst + rows, lo - lst));
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1699 lst = lo;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1700 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1701 else if (comp (*lo, *lst))
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1702 break;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1703
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1704 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1705 if (lo == hi)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1706 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1707 if (lo > lst + 1)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1708 runs.push (run_t (lst + rows, lo - lst));
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1709 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1710 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1711 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1712 sorted = false;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1713 break;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1714 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1715 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1716 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1717 // The final column - use fast code.
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1718 sorted = is_sorted (lo, n, comp);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1719 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1720
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1721 return sorted;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1722 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1723
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1724 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1725 bool
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1726 octave_sort<T>::is_sorted_rows (const T *data, octave_idx_type rows,
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1727 octave_idx_type cols)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1728 {
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1729 bool retval = false;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1730
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1731 #ifdef INLINE_ASCENDING_SORT
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1732 if (compare == ascending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1733 retval = is_sorted_rows (data, rows, cols, std::less<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1734 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1735 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1736 #ifdef INLINE_DESCENDING_SORT
8721
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1737 if (compare == descending_compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1738 retval = is_sorted_rows (data, rows, cols, std::greater<T> ());
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1739 else
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1740 #endif
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1741 if (compare)
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1742 retval = is_sorted_rows (data, rows, cols, compare);
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1743
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1744 return retval;
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1745 }
e9cb742df9eb imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents: 8700
diff changeset
1746
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1747 // The simple binary lookup.
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1748
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1749 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1750 octave_idx_type
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1751 octave_sort<T>::lookup (const T *data, octave_idx_type nel,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1752 const T& value, Comp comp)
9362
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1753 {
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1754 octave_idx_type lo = 0, hi = nel;
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1755
9362
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1756 while (lo < hi)
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1757 {
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1758 octave_idx_type mid = lo + ((hi-lo) >> 1);
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1759 if (comp (value, data[mid]))
9362
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1760 hi = mid;
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1761 else
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1762 lo = mid + 1;
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1763 }
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1764
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1765 return lo;
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1766 }
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1767
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1768 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1769 octave_idx_type
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1770 octave_sort<T>::lookup (const T *data, octave_idx_type nel,
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1771 const T& value)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1772 {
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1773 octave_idx_type retval = 0;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1774
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1775 #ifdef INLINE_ASCENDING_SORT
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1776 if (compare == ascending_compare)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1777 retval = lookup (data, nel, value, std::less<T> ());
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1778 else
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1779 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1780 #ifdef INLINE_DESCENDING_SORT
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1781 if (compare == descending_compare)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1782 retval = lookup (data, nel, value, std::greater<T> ());
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1783 else
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1784 #endif
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1785 if (compare)
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1786 retval = lookup (data, nel, value, std::ptr_fun (compare));
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1787
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1788 return retval;
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1789 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1790
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1791 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1792 void
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1793 octave_sort<T>::lookup (const T *data, octave_idx_type nel,
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1794 const T *values, octave_idx_type nvalues,
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1795 octave_idx_type *idx, Comp comp)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1796 {
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1797 // Use a sequence of binary lookups.
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1798 // TODO: Can this be sped up generally? The sorted merge case is dealt with
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1799 // elsewhere.
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1800 for (octave_idx_type j = 0; j < nvalues; j++)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1801 idx[j] = lookup (data, nel, values[j], comp);
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1802 }
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1803
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1804 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1805 void
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1806 octave_sort<T>::lookup (const T *data, octave_idx_type nel,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1807 const T* values, octave_idx_type nvalues,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1808 octave_idx_type *idx)
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1809 {
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1810 #ifdef INLINE_ASCENDING_SORT
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1811 if (compare == ascending_compare)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1812 lookup (data, nel, values, nvalues, idx, std::less<T> ());
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1813 else
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1814 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1815 #ifdef INLINE_DESCENDING_SORT
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1816 if (compare == descending_compare)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1817 lookup (data, nel, values, nvalues, idx, std::greater<T> ());
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1818 else
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1819 #endif
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1820 if (compare)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1821 lookup (data, nel, values, nvalues, idx, std::ptr_fun (compare));
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1822 }
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1823
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1824 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1825 void
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1826 octave_sort<T>::lookup_sorted (const T *data, octave_idx_type nel,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1827 const T *values, octave_idx_type nvalues,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1828 octave_idx_type *idx, bool rev, Comp comp)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1829 {
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1830 if (rev)
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1831 {
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1832 octave_idx_type i = 0, j = nvalues - 1;
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1833
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1834 if (nvalues > 0 && nel > 0)
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1835 {
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1836 while (true)
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1837 {
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1838 if (comp (values[j], data[i]))
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1839 {
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1840 idx[j] = i;
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1841 if (--j < 0)
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1842 break;
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1843 }
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1844 else if (++i == nel)
9362
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1845 break;
8814
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1846 }
de16ebeef93d improve lookup, provide Array<T>::lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8725
diff changeset
1847 }
9362
2ebf3ca62add use a smarter algorithm for default lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 9341
diff changeset
1848
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1849 for (; j >= 0; j--)
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1850 idx[j] = i;
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1851 }
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1852 else
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1853 {
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1854 octave_idx_type i = 0, j = 0;
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1855
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1856 if (nvalues > 0 && nel > 0)
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1857 {
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1858 while (true)
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1859 {
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1860 if (comp (values[j], data[i]))
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1861 {
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1862 idx[j] = i;
9407
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1863 if (++j == nvalues)
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1864 break;
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1865 }
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1866 else if (++i == nel)
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1867 break;
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1868 }
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1869 }
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1870
0951174cbb03 remove experimental stuff from lookup, simplify
Jaroslav Hajek <highegg@gmail.com>
parents: 9400
diff changeset
1871 for (; j != nvalues; j++)
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1872 idx[j] = i;
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1873 }
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1874 }
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1875
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1876 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1877 void
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1878 octave_sort<T>::lookup_sorted (const T *data, octave_idx_type nel,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1879 const T* values, octave_idx_type nvalues,
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1880 octave_idx_type *idx, bool rev)
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1881 {
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1882 #ifdef INLINE_ASCENDING_SORT
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1883 if (compare == ascending_compare)
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1884 lookup_sorted (data, nel, values, nvalues, idx, rev, std::less<T> ());
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1885 else
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1886 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1887 #ifdef INLINE_DESCENDING_SORT
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1888 if (compare == descending_compare)
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1889 lookup_sorted (data, nel, values, nvalues, idx, rev, std::greater<T> ());
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1890 else
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1891 #endif
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1892 if (compare)
9921
7c8392a034e6 fix & improve lookup API
Jaroslav Hajek <highegg@gmail.com>
parents: 9725
diff changeset
1893 lookup_sorted (data, nel, values, nvalues, idx, rev, std::ptr_fun (compare));
9341
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1894 }
9fd5c56ce57a extend lookup capabilities
Jaroslav Hajek <highegg@gmail.com>
parents: 8820
diff changeset
1895
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1896 template <class T> template <class Comp>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1897 void
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1898 octave_sort<T>::nth_element (T *data, octave_idx_type nel,
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1899 octave_idx_type lo, octave_idx_type up,
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1900 Comp comp)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1901 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1902 // Simply wrap the STL algorithms.
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1903 // FIXME: this will fail if we attempt to inline <,> for Complex.
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1904 if (up == lo+1)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1905 std::nth_element (data, data + lo, data + nel, comp);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1906 else if (lo == 0)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1907 std::partial_sort (data, data + up, data + nel, comp);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1908 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1909 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1910 std::nth_element (data, data + lo, data + nel, comp);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1911 if (up == lo + 2)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1912 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1913 // Finding two subsequent elements.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1914 std::swap (data[lo+1],
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1915 *std::min_element (data + lo + 1, data + nel, comp));
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1916 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1917 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1918 std::partial_sort (data + lo + 1, data + up, data + nel, comp);
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1919 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1920 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1921
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1922 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1923 void
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1924 octave_sort<T>::nth_element (T *data, octave_idx_type nel,
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1925 octave_idx_type lo, octave_idx_type up)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1926 {
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1927 if (up < 0)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1928 up = lo + 1;
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1929 #ifdef INLINE_ASCENDING_SORT
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1930 if (compare == ascending_compare)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1931 nth_element (data, nel, lo, up, std::less<T> ());
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1932 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1933 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1934 #ifdef INLINE_DESCENDING_SORT
9725
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1935 if (compare == descending_compare)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1936 nth_element (data, nel, lo, up, std::greater<T> ());
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1937 else
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1938 #endif
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1939 if (compare)
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1940 nth_element (data, nel, lo, up, std::ptr_fun (compare));
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1941 }
aea3a3a950e1 implement nth_element
Jaroslav Hajek <highegg@gmail.com>
parents: 9410
diff changeset
1942
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1943 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1944 bool
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1945 octave_sort<T>::ascending_compare (typename ref_param<T>::type x,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1946 typename ref_param<T>::type y)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1947 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1948 return x < y;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1949 }
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1950
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1951 template <class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1952 bool
8725
d5af326a3ede [mq]: sort-traits
John W. Eaton <jwe@octave.org>
parents: 8721
diff changeset
1953 octave_sort<T>::descending_compare (typename ref_param<T>::type x,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1954 typename ref_param<T>::type y)
8700
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1955 {
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1956 return x > y;
314be237cd5b sorting optimizations
Jaroslav Hajek <highegg@gmail.com>
parents: 8678
diff changeset
1957 }