Mercurial > octave-nkf
annotate liboctave/oct-sort.h @ 8710:739141cde75a ss-3-1-52
fix typo in Array-f.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 09 Feb 2009 21:51:31 +0100 |
parents | 314be237cd5b |
children | e9cb742df9eb |
rev | line source |
---|---|
4851 | 1 /* |
7017 | 2 Copyright (C) 2003, 2004, 2005, 2006, 2007 David Bateman |
4851 | 3 |
4 This file is part of Octave. | |
5 | |
6 Octave is free software; you can redistribute it and/or modify it | |
7 under the terms of the GNU General Public License as published by the | |
7016 | 8 Free Software Foundation; either version 3 of the License, or (at your |
9 option) any later version. | |
4851 | 10 |
11 Octave is distributed in the hope that it will be useful, but WITHOUT | |
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
7016 | 17 along with Octave; see the file COPYING. If not, see |
18 <http://www.gnu.org/licenses/>. | |
4851 | 19 |
20 Code stolen in large part from Python's, listobject.c, which itself had | |
21 no license header. However, thanks to Tim Peters for the parts of the | |
22 code I ripped-off. | |
23 | |
24 As required in the Python license the short description of the changes | |
25 made are | |
26 | |
27 * convert the sorting code in listobject.cc into a generic class, | |
28 replacing PyObject* with the type of the class T. | |
29 | |
30 The Python license is | |
31 | |
32 PSF LICENSE AGREEMENT FOR PYTHON 2.3 | |
33 -------------------------------------- | |
34 | |
35 1. This LICENSE AGREEMENT is between the Python Software Foundation | |
36 ("PSF"), and the Individual or Organization ("Licensee") accessing and | |
37 otherwise using Python 2.3 software in source or binary form and its | |
38 associated documentation. | |
39 | |
40 2. Subject to the terms and conditions of this License Agreement, PSF | |
41 hereby grants Licensee a nonexclusive, royalty-free, world-wide | |
42 license to reproduce, analyze, test, perform and/or display publicly, | |
43 prepare derivative works, distribute, and otherwise use Python 2.3 | |
44 alone or in any derivative version, provided, however, that PSF's | |
45 License Agreement and PSF's notice of copyright, i.e., "Copyright (c) | |
46 2001, 2002, 2003 Python Software Foundation; All Rights Reserved" are | |
47 retained in Python 2.3 alone or in any derivative version prepared by | |
48 Licensee. | |
49 | |
50 3. In the event Licensee prepares a derivative work that is based on | |
51 or incorporates Python 2.3 or any part thereof, and wants to make | |
52 the derivative work available to others as provided herein, then | |
53 Licensee hereby agrees to include in any such work a brief summary of | |
54 the changes made to Python 2.3. | |
55 | |
56 4. PSF is making Python 2.3 available to Licensee on an "AS IS" | |
57 basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR | |
58 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND | |
59 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS | |
60 FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT | |
61 INFRINGE ANY THIRD PARTY RIGHTS. | |
62 | |
63 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON | |
64 2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS | |
65 A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3, | |
66 OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. | |
67 | |
68 6. This License Agreement will automatically terminate upon a material | |
69 breach of its terms and conditions. | |
70 | |
71 7. Nothing in this License Agreement shall be deemed to create any | |
72 relationship of agency, partnership, or joint venture between PSF and | |
73 Licensee. This License Agreement does not grant permission to use PSF | |
74 trademarks or trade name in a trademark sense to endorse or promote | |
75 products or services of Licensee, or any third party. | |
76 | |
77 8. By copying, installing or otherwise using Python 2.3, Licensee | |
78 agrees to be bound by the terms and conditions of this License | |
79 Agreement. | |
80 */ | |
81 | |
82 #if !defined (octave_sort_h) | |
83 #define octave_sort_h 1 | |
84 | |
7234 | 85 // The maximum number of entries in a MergeState's pending-runs stack. |
86 // This is enough to sort arrays of size up to about | |
87 // 32 * phi ** MAX_MERGE_PENDING | |
88 // where phi ~= 1.618. 85 is ridiculously large enough, good for an array | |
89 // with 2**64 elements. | |
4851 | 90 #define MAX_MERGE_PENDING 85 |
91 | |
7234 | 92 // When we get into galloping mode, we stay there until both runs win less |
93 // often than MIN_GALLOP consecutive times. See listsort.txt for more info. | |
4851 | 94 #define MIN_GALLOP 7 |
95 | |
7234 | 96 // Avoid malloc for small temp arrays. |
4851 | 97 #define MERGESTATE_TEMP_SIZE 1024 |
98 | |
7433 | 99 // Enum for type of sort function |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
100 enum sortmode { ASCENDING, DESCENDING }; |
7433 | 101 |
4851 | 102 template <class T> |
103 class | |
104 octave_sort | |
105 { | |
4998 | 106 public: |
107 | |
4851 | 108 octave_sort (void); |
109 | |
110 octave_sort (bool (*comp) (T, T)); | |
111 | |
4998 | 112 ~octave_sort (void) { merge_freemem (); } |
113 | |
114 void set_compare (bool (*comp) (T, T)) { compare = comp; } | |
4851 | 115 |
8700 | 116 void sort (T *data, octave_idx_type nel); |
117 | |
118 void sort (T *data, octave_idx_type *idx, octave_idx_type nel); | |
119 | |
120 static bool ascending_compare (T, T); | |
121 static bool descending_compare (T, T); | |
4851 | 122 |
4998 | 123 private: |
124 | |
7234 | 125 // One MergeState exists on the stack per invocation of mergesort. |
126 // It's just a convenient way to pass state around among the helper | |
127 // functions. | |
128 // | |
129 // DGB: This isn't needed with mergesort in a class, but it doesn't | |
130 // slow things up, and it is likely to make my life easier for any | |
131 // potential backporting of changes in the Python code. | |
4851 | 132 |
133 struct s_slice | |
134 { | |
8700 | 135 octave_idx_type base, len; |
4851 | 136 }; |
137 | |
7234 | 138 struct MergeState |
4851 | 139 { |
7234 | 140 // This controls when we get *into* galloping mode. It's |
141 // initialized to MIN_GALLOP. merge_lo and merge_hi tend to nudge | |
142 // it higher for random data, and lower for highly structured | |
143 // data. | |
7433 | 144 octave_idx_type min_gallop; |
4851 | 145 |
7234 | 146 // 'a' is temp storage to help with merges. It contains room for |
147 // alloced entries. | |
148 T *a; // may point to temparray below | |
8700 | 149 octave_idx_type *ia; |
7433 | 150 octave_idx_type alloced; |
4851 | 151 |
7234 | 152 // A stack of n pending runs yet to be merged. Run #i starts at |
153 // address base[i] and extends for len[i] elements. It's always | |
154 // true (so long as the indices are in bounds) that | |
155 // | |
156 // pending[i].base + pending[i].len == pending[i+1].base | |
157 // | |
158 // so we could cut the storage for this, but it's a minor amount, | |
159 // and keeping all the info explicit simplifies the code. | |
7433 | 160 octave_idx_type n; |
4851 | 161 struct s_slice pending[MAX_MERGE_PENDING]; |
7234 | 162 }; |
4851 | 163 |
7234 | 164 bool (*compare) (T, T); |
4851 | 165 |
166 MergeState ms; | |
167 | |
8700 | 168 |
169 template <class Comp> | |
170 void binarysort (T *data, octave_idx_type nel, | |
171 octave_idx_type start, Comp comp); | |
172 | |
173 template <class Comp> | |
174 void binarysort (T *data, octave_idx_type *idx, octave_idx_type nel, | |
175 octave_idx_type start, Comp comp); | |
4851 | 176 |
8700 | 177 template <class Comp> |
178 octave_idx_type count_run (T *lo, octave_idx_type n, bool& descending, Comp comp); | |
4851 | 179 |
8700 | 180 template <class Comp> |
181 octave_idx_type gallop_left (T key, T *a, octave_idx_type n, octave_idx_type hint, | |
182 Comp comp); | |
4851 | 183 |
8700 | 184 template <class Comp> |
185 octave_idx_type gallop_right (T key, T *a, octave_idx_type n, octave_idx_type hint, | |
186 Comp comp); | |
4851 | 187 |
7234 | 188 void merge_init (void); |
4851 | 189 |
7234 | 190 void merge_freemem (void); |
4851 | 191 |
7433 | 192 int merge_getmem (octave_idx_type need); |
4851 | 193 |
8700 | 194 int merge_getmemi (octave_idx_type need); |
195 | |
196 template <class Comp> | |
197 int merge_lo (T *pa, octave_idx_type na, | |
198 T *pb, octave_idx_type nb, | |
199 Comp comp); | |
4851 | 200 |
8700 | 201 template <class Comp> |
202 int merge_lo (T *pa, octave_idx_type *ipa, octave_idx_type na, | |
203 T *pb, octave_idx_type *ipb, octave_idx_type nb, | |
204 Comp comp); | |
205 | |
206 template <class Comp> | |
207 int merge_hi (T *pa, octave_idx_type na, | |
208 T *pb, octave_idx_type nb, | |
209 Comp comp); | |
4851 | 210 |
8700 | 211 template <class Comp> |
212 int merge_hi (T *pa, octave_idx_type *ipa, octave_idx_type na, | |
213 T *pb, octave_idx_type *ipb, octave_idx_type nb, | |
214 Comp comp); | |
215 | |
216 template <class Comp> | |
217 int merge_at (octave_idx_type i, T *data, | |
218 Comp comp); | |
4851 | 219 |
8700 | 220 template <class Comp> |
221 int merge_at (octave_idx_type i, T *data, octave_idx_type *idx, | |
222 Comp comp); | |
223 | |
224 template <class Comp> | |
225 int merge_collapse (T *data, Comp comp); | |
4851 | 226 |
8700 | 227 template <class Comp> |
228 int merge_collapse (T *data, octave_idx_type *idx, Comp comp); | |
229 | |
230 template <class Comp> | |
231 int merge_force_collapse (T *data, Comp comp); | |
232 | |
233 template <class Comp> | |
234 int merge_force_collapse (T *data, octave_idx_type *idx, Comp comp); | |
4851 | 235 |
7433 | 236 octave_idx_type merge_compute_minrun (octave_idx_type n); |
8700 | 237 |
238 template <class Comp> | |
239 void sort (T *data, octave_idx_type nel, Comp comp); | |
240 | |
241 template <class Comp> | |
242 void sort (T *data, octave_idx_type *idx, octave_idx_type nel, Comp comp); | |
243 | |
4851 | 244 }; |
245 | |
7433 | 246 template <class T> |
247 class | |
248 vec_index | |
249 { | |
250 public: | |
251 T vec; | |
252 octave_idx_type indx; | |
253 }; | |
4851 | 254 #endif |
255 | |
256 /* | |
257 ;;; Local Variables: *** | |
258 ;;; mode: C++ *** | |
259 ;;; End: *** | |
260 */ |