annotate liboctave/oct-sort.cc @ 9921:7c8392a034e6

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