annotate liboctave/oct-sort.cc @ 10843:229675bb7647 ss-3-3-52

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