2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3826
|
27 #include "lo-mappers.h" |
4153
|
28 #include "quit.h" |
3826
|
29 |
2928
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 |
|
35 // This is algorithm 5.2.4L from Knuth, Volume 3. |
|
36 |
|
37 // XXX FIXME XXX -- there is way too much duplicated code here given |
|
38 // that the sort algorithms are all the same, and only the type of the |
|
39 // data and the comparison changes... |
|
40 // |
|
41 // Maybe some cpp abuse will make it better. |
|
42 |
|
43 static Array<int> |
|
44 create_index_array (int n) |
|
45 { |
|
46 Array<int> l (n+2); |
|
47 |
|
48 l (0) = 1; |
|
49 |
|
50 for (int i = 1; i < n - 1; i++) |
|
51 l (i) = -(i+2); |
|
52 |
|
53 l (n-1) = 0; |
|
54 l (n) = 0; |
|
55 l (n+1) = 2; |
|
56 |
|
57 return l; |
|
58 } |
|
59 |
|
60 #define SORT_INIT_PHASE(n) \ |
|
61 int s = 0; \ |
|
62 int t = n + 1; \ |
|
63 int p = l (s); \ |
|
64 int q = l (t); \ |
|
65 if (q == 0) \ |
|
66 break |
|
67 |
|
68 #define SORT_COMMON_CODE \ |
|
69 p = -p; \ |
|
70 q = -q; \ |
|
71 if (q == 0) \ |
|
72 { \ |
|
73 l (s) = (l (s) < 0) \ |
|
74 ? ((p < 0) ? p : -p) \ |
|
75 : ((p >= 0) ? p : -p); \ |
|
76 l (t) = 0; \ |
|
77 break; \ |
|
78 } \ |
|
79 |
|
80 #define SORT_REORDER_PHASE_ONE \ |
|
81 l (s) = (l (s) < 0) \ |
|
82 ? ((q < 0) ? q : -q) \ |
|
83 : ((q >= 0) ? q : -q); \ |
|
84 s = q; \ |
|
85 q = l (q); \ |
|
86 if (q <= 0) \ |
|
87 { \ |
|
88 l (s) = p; \ |
|
89 s = t; \ |
|
90 do \ |
|
91 { \ |
|
92 t = p; \ |
|
93 p = l (p); \ |
|
94 } \ |
|
95 while (p > 0); \ |
|
96 SORT_COMMON_CODE; \ |
|
97 } \ |
|
98 |
|
99 #define SORT_REORDER_PHASE_TWO \ |
|
100 l (s) = (l (s) < 0) \ |
|
101 ? ((p < 0) ? p : -p) \ |
|
102 : ((p >= 0) ? p : -p); \ |
|
103 s = p; \ |
|
104 p = l (p); \ |
|
105 if (p <= 0) \ |
|
106 { \ |
|
107 l (s) = q; \ |
|
108 s = t; \ |
|
109 do \ |
|
110 { \ |
|
111 t = q; \ |
|
112 q = l (q); \ |
|
113 } \ |
|
114 while (q > 0); \ |
|
115 SORT_COMMON_CODE; \ |
|
116 } |
|
117 |
|
118 #define DO_SORT(n, condition) \ |
|
119 while (1) \ |
|
120 { \ |
|
121 SORT_INIT_PHASE(n); \ |
|
122 while (1) \ |
|
123 { \ |
4153
|
124 OCTAVE_QUIT; \ |
2928
|
125 if (condition) \ |
|
126 { \ |
|
127 SORT_REORDER_PHASE_ONE; \ |
|
128 } \ |
|
129 else \ |
|
130 { \ |
|
131 SORT_REORDER_PHASE_TWO; \ |
|
132 } \ |
|
133 } \ |
|
134 } |
|
135 |
|
136 #define VECTOR_CREATE_RETURN_VALUES(vs, v) \ |
|
137 int k = l (0); \ |
|
138 idx (0) = k; \ |
|
139 vs (0) = v (k-1); \ |
|
140 for (int i = 1; i < n; i++) \ |
|
141 { \ |
4153
|
142 OCTAVE_QUIT; \ |
2928
|
143 k = l (static_cast<int> (idx (i-1))); \ |
|
144 idx (i) = k; \ |
|
145 vs (i) = v (k-1); \ |
|
146 } |
|
147 |
|
148 #define MATRIX_CREATE_RETURN_VALUES(ms, m) \ |
|
149 int k = l (0); \ |
|
150 idx (0, j) = k; \ |
|
151 ms (0, j) = m (k-1, j); \ |
|
152 for (int i = 1; i < nr; i++) \ |
|
153 { \ |
4153
|
154 OCTAVE_QUIT; \ |
2928
|
155 k = l (static_cast<int> (idx (i-1, j))); \ |
|
156 idx (i, j) = k; \ |
|
157 ms (i, j) = m (k-1, j); \ |
|
158 } |
|
159 |
|
160 static octave_value_list |
|
161 mx_sort (const Matrix& m) |
|
162 { |
|
163 octave_value_list retval; |
|
164 |
|
165 int nr = m.rows (); |
|
166 int nc = m.columns (); |
|
167 |
|
168 Matrix ms (nr, nc); |
|
169 Matrix idx (nr, nc); |
|
170 |
|
171 if (nr == 1 && nc > 0) |
|
172 { |
3418
|
173 retval(1) = Matrix (nr, nc, 1.0); |
|
174 retval(0) = m; |
2928
|
175 |
|
176 return retval; |
|
177 } |
|
178 else if (nr > 1 && nc > 0) |
|
179 { |
|
180 for (int j = 0; j < nc; j++) |
|
181 { |
|
182 Array<int> l = create_index_array (nr); |
|
183 |
3826
|
184 DO_SORT (nr, (xisnan (m (p-1, j)) || m (p-1, j) > m (q-1, j))); |
2928
|
185 |
|
186 MATRIX_CREATE_RETURN_VALUES (ms, m); |
|
187 } |
|
188 } |
|
189 |
3418
|
190 retval(1) = idx; |
|
191 retval(0) = ms; |
2928
|
192 |
|
193 return retval; |
|
194 } |
|
195 |
|
196 static octave_value_list |
|
197 mx_sort (const RowVector& v) |
|
198 { |
|
199 octave_value_list retval; |
|
200 |
|
201 int n = v.capacity (); |
|
202 |
|
203 RowVector vs (n); |
|
204 RowVector idx (n); |
|
205 |
|
206 if (n == 1) |
|
207 { |
3418
|
208 retval(1) = RowVector (n, 1.0); |
|
209 retval(0) = v; |
2928
|
210 |
|
211 return retval; |
|
212 } |
|
213 else if (n > 1) |
|
214 { |
|
215 Array<int> l = create_index_array (n); |
|
216 |
3826
|
217 DO_SORT (n, (xisnan (v (p-1)) || v (p-1) > v (q-1))); |
2928
|
218 |
|
219 VECTOR_CREATE_RETURN_VALUES (vs, v); |
|
220 } |
|
221 |
3418
|
222 retval(1) = idx; |
|
223 retval(0) = vs; |
2928
|
224 |
|
225 return retval; |
|
226 } |
|
227 |
|
228 static octave_value_list |
|
229 mx_sort (const ComplexMatrix& cm) |
|
230 { |
|
231 octave_value_list retval; |
|
232 |
|
233 int nr = cm.rows (); |
|
234 int nc = cm.columns (); |
|
235 |
|
236 ComplexMatrix cms (nr, nc); |
|
237 Matrix idx (nr, nc); |
|
238 |
|
239 if (nr == 1 && nc > 0) |
|
240 { |
3418
|
241 retval(1) = Matrix (nr, nc, 1.0); |
|
242 retval(0) = cm; |
2928
|
243 |
|
244 return retval; |
|
245 } |
|
246 else if (nr > 1 && nc > 0) |
|
247 { |
|
248 for (int j = 0; j < nc; j++) |
|
249 { |
|
250 Array<int> l = create_index_array (nr); |
|
251 |
3826
|
252 bool all_elts_real = true; |
2928
|
253 for (int i = 0; i < nr; i++) |
4153
|
254 { |
|
255 OCTAVE_QUIT; |
|
256 if (imag (cm (i, j)) != 0.0) |
|
257 { |
|
258 all_elts_real = false; |
|
259 break; |
|
260 } |
|
261 } |
2928
|
262 |
|
263 DO_SORT (nr, ((all_elts_real |
3826
|
264 && (xisnan (real (cm (p-1, j))) |
|
265 || real (cm (p-1, j)) > real (cm (q-1, j)))) |
|
266 || xisnan (cm (p-1, j)) |
2928
|
267 || abs (cm (p-1, j)) > abs (cm (q-1, j)))); |
|
268 |
|
269 MATRIX_CREATE_RETURN_VALUES (cms, cm); |
|
270 } |
|
271 } |
|
272 |
3418
|
273 retval(1) = idx; |
|
274 retval(0) = cms; |
2928
|
275 |
|
276 return retval; |
|
277 } |
|
278 |
|
279 static octave_value_list |
|
280 mx_sort (ComplexRowVector& cv) |
|
281 { |
|
282 octave_value_list retval; |
|
283 |
|
284 int n = cv.capacity (); |
|
285 |
|
286 ComplexRowVector cvs (n); |
|
287 RowVector idx (n); |
|
288 |
|
289 if (n == 1) |
|
290 { |
3418
|
291 retval(1) = RowVector (n, 1.0); |
|
292 retval(0) = cv; |
2928
|
293 |
|
294 return retval; |
|
295 } |
|
296 else if (n > 1) |
|
297 { |
|
298 Array<int> l = create_index_array (n); |
|
299 |
3826
|
300 bool all_elts_real = true; |
2928
|
301 for (int i = 0; i < n; i++) |
4153
|
302 { |
|
303 OCTAVE_QUIT; |
|
304 if (imag (cv (i)) != 0.0) |
|
305 { |
|
306 all_elts_real = false; |
|
307 break; |
|
308 } |
|
309 } |
2928
|
310 |
|
311 DO_SORT (n, ((all_elts_real |
3826
|
312 && (xisnan (real (cv (p-1))) |
|
313 || real (cv (p-1)) > real (cv (q-1)))) |
|
314 || xisnan (cv (p-1)) |
2928
|
315 || abs (cv (p-1)) > abs (cv (q-1)))); |
|
316 |
|
317 VECTOR_CREATE_RETURN_VALUES (cvs, cv); |
|
318 } |
|
319 |
3418
|
320 retval(1) = idx; |
|
321 retval(0) = cvs; |
2928
|
322 |
|
323 return retval; |
|
324 } |
|
325 |
|
326 DEFUN_DLD (sort, args, nargout, |
3369
|
327 "-*- texinfo -*-\n\ |
|
328 @deftypefn {Loadable Function} {[@var{s}, @var{i}] =} sort (@var{x})\n\ |
|
329 Return a copy of @var{x} with the elements elements arranged in\n\ |
|
330 increasing order. For matrices, @code{sort} orders the elements in each\n\ |
|
331 column.\n\ |
|
332 \n\ |
|
333 For example,\n\ |
|
334 \n\ |
|
335 @example\n\ |
|
336 @group\n\ |
|
337 sort ([1, 2; 2, 3; 3, 1])\n\ |
|
338 @result{} 1 1\n\ |
|
339 2 2\n\ |
|
340 3 3\n\ |
|
341 @end group\n\ |
|
342 @end example\n\ |
2928
|
343 \n\ |
3369
|
344 The @code{sort} function may also be used to produce a matrix\n\ |
|
345 containing the original row indices of the elements in the sorted\n\ |
|
346 matrix. For example,\n\ |
|
347 \n\ |
|
348 @example\n\ |
|
349 @group\n\ |
|
350 [s, i] = sort ([1, 2; 2, 3; 3, 1])\n\ |
|
351 @result{} s = 1 1\n\ |
|
352 2 2\n\ |
|
353 3 3\n\ |
|
354 @result{} i = 1 3\n\ |
|
355 2 1\n\ |
|
356 3 2\n\ |
|
357 @end group\n\ |
|
358 @end example\n\ |
|
359 @end deftypefn") |
2928
|
360 { |
|
361 octave_value_list retval; |
|
362 |
|
363 int nargin = args.length (); |
|
364 |
|
365 if (nargin != 1) |
|
366 { |
|
367 print_usage ("sort"); |
|
368 return retval; |
|
369 } |
|
370 |
|
371 int return_idx = nargout > 1; |
|
372 if (return_idx) |
|
373 retval.resize (2); |
|
374 else |
|
375 retval.resize (1); |
|
376 |
|
377 octave_value arg = args(0); |
|
378 |
|
379 if (arg.is_real_type ()) |
|
380 { |
4628
|
381 const Matrix m = arg.matrix_value (); |
2928
|
382 |
|
383 if (! error_state) |
|
384 { |
|
385 if (m.rows () == 1) |
|
386 { |
|
387 int nc = m.columns (); |
|
388 RowVector v (nc); |
|
389 for (int i = 0; i < nc; i++) |
4628
|
390 v(i) = m(0,i); |
2928
|
391 |
|
392 retval = mx_sort (v); |
|
393 } |
|
394 else |
|
395 retval = mx_sort (m); |
|
396 } |
|
397 } |
|
398 else if (arg.is_complex_type ()) |
|
399 { |
4628
|
400 const ComplexMatrix cm = arg.complex_matrix_value (); |
2928
|
401 |
|
402 if (! error_state) |
|
403 { |
|
404 if (cm.rows () == 1) |
|
405 { |
|
406 int nc = cm.columns (); |
|
407 ComplexRowVector cv (nc); |
|
408 for (int i = 0; i < nc; i++) |
4628
|
409 cv(i) = cm(0,i); |
2928
|
410 |
|
411 retval = mx_sort (cv); |
|
412 } |
|
413 else |
|
414 retval = mx_sort (cm); |
|
415 } |
|
416 } |
|
417 else |
|
418 gripe_wrong_type_arg ("sort", arg); |
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
|
423 /* |
|
424 ;;; Local Variables: *** |
|
425 ;;; mode: C++ *** |
|
426 ;;; End: *** |
|
427 */ |