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