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