Debian Codegraph @

contrib/ms-sbvar/utilities_dw/sort/dw_matrix_sort.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_matrix_sort.h"
19 #include "dw_error.h"
20 #include "dw_std.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 static void b_qsort_array_ascending_real(PRECISION *x, int m);
26 static void b_qsort_array_descending_real(PRECISION *x, int m);
27 static void b_qsort_matrix_columns_ascending_real(PRECISION *x, int m, int n, int idx);
28 static void b_qsort_matrix_columns_descending_real(PRECISION *x, int m, int n, int idx);
29 static void b_qsort_matrix_rows_ascending_real(PRECISION *x, int m, int n, int br, int er, int idx);
30 static void b_qsort_matrix_rows_descending_real(PRECISION *x, int m, int n, int br, int er, int idx);
31
32 /*
33 Assumes
34 X : m x n matrix or null
35 Y : m x n matrix
36 j : column to sort
37
38 Results
39 The rows of X are sorted in ascending order on the ith column. The matrix
40 X is created if null.
41
42 Returns
43 Returns X upon success and null on failure. Call GetError() to
44 determine the cause of failure.
45
46 Notes
47 X and Y do not have to be distinct matrices. Uses the quick sort algorithm,
48 */
49 TMatrix SortMatrixRowsAscending(TMatrix X, TMatrix Y, int j)
50 {
51 if (!Y)
52 {
53 dw_Error(NULL_ERR);
54 return (TMatrix)NULL;
55 }
56 if ((X != Y) && !(X=EquateMatrix(X,Y)))
57 return (TMatrix)NULL;
58 if (MajorForm(X) == ROW_MAJOR)
59 b_qsort_matrix_columns_ascending_real(pElementM(X),ColM(X),RowM(X),j);
60 else
61 b_qsort_matrix_rows_ascending_real(pElementM(X),RowM(X),ColM(X),0,RowM(X)-1,j*RowM(X));
62 return X;
63 }
64
65 /*
66 Assumes
67 X : m x n matrix or null
68 Y : m x n matrix
69 j : column to sort
70
71 Results
72 The rows of X are sorted in descending order on the ith column. The matrix
73 X is created if null.
74
75 Returns
76 Returns X upon success and null on failure. Call GetError() to
77 determine the cause of failure.
78
79 Notes
80 X and Y do not have to be distinct matrices. Uses the quick sort algorithm,
81 */
82 TMatrix SortMatrixRowsDescending(TMatrix X, TMatrix Y, int j)
83 {
84 if (!Y)
85 {
86 dw_Error(NULL_ERR);
87 return (TMatrix)NULL;
88 }
89 if ((X != Y) && !(X=EquateMatrix(X,Y)))
90 return (TMatrix)NULL;
91 if (MajorForm(X) == ROW_MAJOR)
92 b_qsort_matrix_columns_descending_real(pElementM(X),ColM(X),RowM(X),j);
93 else
94 b_qsort_matrix_rows_descending_real(pElementM(X),RowM(X),ColM(X),0,RowM(X)-1,j*RowM(X));
95 return X;
96 }
97
98 /*
99 Assumes
100 X : m x n matrix or null
101 Y : m x n matrix
102 i : row to sort
103
104 Results
105 The columns of X are sorted in ascending order on the ith row. The matrix X
106 is created if null.
107
108 Returns
109 Returns X upon success and null on failure. Call GetError() to
110 determine the cause of failure.
111
112 Notes
113 X and Y do not have to be distinct matrices. Uses the quick sort algorithm,
114 */
115 TMatrix SortMatrixColumnsAscending(TMatrix X, TMatrix Y, int i)
116 {
117 if (!Y)
118 {
119 dw_Error(NULL_ERR);
120 return (TMatrix)NULL;
121 }
122 if ((X != Y) && !(X=EquateMatrix(X,Y)))
123 return (TMatrix)NULL;
124 if (MajorForm(X) == ROW_MAJOR)
125 b_qsort_matrix_rows_ascending_real(pElementM(X),ColM(X),RowM(X),0,ColM(X)-1,i*RowM(X));
126 else
127 b_qsort_matrix_columns_ascending_real(pElementM(X),RowM(X),ColM(X),i);
128 return X;
129 }
130
131 /*
132 Assumes
133 X : m x n matrix or null
134 Y : m x n matrix
135 i : row to sort
136
137 Results
138 The columns of X are sorted in descending order on the ith row. The matrix
139 X is created if null.
140
141 Returns
142 Returns X upon success and null on failure. Call GetError() to
143 determine the cause of failure.
144
145 Notes
146 X and Y do not have to be distinct matrices. Uses the quick sort algorithm,
147 */
148 TMatrix SortMatrixColumnsDescending(TMatrix X, TMatrix Y, int i)
149 {
150 if (!Y)
151 {
152 dw_Error(NULL_ERR);
153 return (TMatrix)NULL;
154 }
155 if ((X != Y) && !(X=EquateMatrix(X,Y)))
156 return (TMatrix)NULL;
157 if (MajorForm(X) == ROW_MAJOR)
158 b_qsort_matrix_rows_descending_real(pElementM(X),ColM(X),RowM(X),0,ColM(X)-1,i*RowM(X));
159 else
160 b_qsort_matrix_columns_descending_real(pElementM(X),RowM(X),ColM(X),i);
161 return X;
162 }
163
164 /*
165 Assumes
166 x : m vector or null
167 y : m vector
168
169 Results
170 The vector x is sorted in ascending order. The vector x is created if
171 null.
172
173 Returns
174 Returns x upon success and null on failure. Call GetError() to
175 determine the cause of failure.
176
177 Notes
178 x and x do not have to be distinct vectors. Uses the quick sort algorithm,
179 */
180 TVector SortVectorAscending(TVector x, TVector y)
181 {
182 if (!y)
183 {
184 dw_Error(NULL_ERR);
185 return (TVector)NULL;
186 }
187 if ((x != y) && !(x=EquateVector(x,y)))
188 return (TVector)NULL;
189 b_qsort_array_ascending_real(pElementV(x),DimV(x));
190 return x;
191 }
192
193 /*
194 Assumes
195 x : m vector or null
196 y : m vector
197
198 Results
199 The vector x is sorted in descending order. The vector x is created if
200 null.
201
202 Returns
203 Returns x upon success and null on failure. Call GetError() to
204 determine the cause of failure.
205
206 Notes
207 x and x do not have to be distinct vectors. Uses the quick sort algorithm,
208 */
209 TVector SortVectorDescending(TVector x, TVector y)
210 {
211 if (!y)
212 {
213 dw_Error(NULL_ERR);
214 return (TVector)NULL;
215 }
216 if ((x != y) && !(x=EquateVector(x,y)))
217 return (TVector)NULL;
218 b_qsort_array_descending_real(pElementV(x),DimV(x));
219 return x;
220 }
221
222 /*******************************************************************************/
223 /*******************************************************************************/
224 /*******************************************************************************/
225 /*
226 Assumes:
227 x - array of length m
228
229 Results:
230 x is sorted in ascending order
231
232 Notes:
233 Uses the quick sort mean algorithm. Switches to insertion sort when the
234 size of the list is 10 or less.
235 */
236 static void b_qsort_array_ascending_real(PRECISION *x, int m)
237 {
238 PRECISION y, c;
239 int j, k;
240 if (m > 10)
241 {
242 // quick sort
243 m--;
244
245 if (x[0] == x[m])
246 c=x[0];
247 else
248 {
249 if (x[0] > x[m])
250 { y=x[m]; x[m]=x[0]; x[0]=y; }
251 c=0.5*(x[0] + x[m]);
252 }
253
254 for (j=1; (j < m) && (x[j] <= c); j++);
255 for (k=m-1; (k > 0) && (x[k] >= c); k--);
256 while (j < k)
257 {
258 y=x[j]; x[j]=x[k]; x[k]=y;
259 while (x[j] <= c) j++;
260 while (x[k] >= c) k--;
261 }
262 if (k > 0)
263 b_qsort_array_ascending_real(x,k+1);
264 if (j < m)
265 b_qsort_array_ascending_real(x+j,m-j+1);
266 }
267 else
268 {
269 // insertion sort
270 for (j=1; j < m; j++)
271 {
272 y=x[j];
273 for (k=j-1; k >= 0; k--)
274 if (x[k] <= y)
275 break;
276 else
277 x[k+1]=x[k];
278 x[k+1]=y;
279 }
280 }
281 }
282
283 /*
284 Assumes:
285 x - array of length m
286
287 Results:
288 x is sorted in descending order
289
290 Notes:
291 Uses the quick sort mean algorithm. Switches to insertion sort when the
292 size of the list is 10 or less.
293 */
294 static void b_qsort_array_descending_real(PRECISION *x, int m)
295 {
296 PRECISION y, c;
297 int j, k;
298 if (m > 10)
299 {
300 // quick sort
301 m--;
302
303 if (x[0] == x[m])
304 c=x[0];
305 else
306 {
307 if (x[0] < x[m])
308 { y=x[m]; x[m]=x[0]; x[0]=y; }
309 c=0.5*(x[0] + x[m]);
310 }
311
312 for (j=1; (j < m) && (x[j] >= c); j++);
313 for (k=m-1; (k > 0) && (x[k] <= c); k--);
314 while (j < k)
315 {
316 y=x[j]; x[j]=x[k]; x[k]=y;
317 while (x[j] >= c) j++;
318 while (x[k] <= c) k--;
319 }
320 if (k > 0)
321 b_qsort_array_descending_real(x,k+1);
322 if (j < m)
323 b_qsort_array_descending_real(x+j,m-j+1);
324 }
325 else
326 {
327 // insertion sort
328 for (j=1; j < m; j++)
329 {
330 y=x[j];
331 for (k=j-1; k >= 0; k--)
332 if (x[k] >= y)
333 break;
334 else
335 x[k+1]=x[k];
336 x[k+1]=y;
337 }
338 }
339
340 }
341
342 /*
343 Assumes:
344 x - array of length m*n in colum major format.
345 m - number of rows
346 n - number of columns
347
348 Results:
349 The columns of x are sorted in ascending order on row idx.
350
351 Notes:
352 Uses the quick sort mean algorithm. Switches to insertion sort when the
353 size of the list is 10 or less. If the matrix is in row major format, then
354 m is the number of columns, n is the number of rows, and the rows of x are
355 sorted in ascending order on column idx.
356 */
357 static void b_qsort_matrix_columns_ascending_real(PRECISION *x, int m, int n, int idx)
358 {
359 PRECISION *y, c;
360 int j, k, p, s;
361 y=(PRECISION*)dw_malloc(s=m*sizeof(PRECISION));
362 if (n > 10)
363 {
364 // quick sort
365 p=(n-1)*m;
366 k=p+idx;
367
368 if (x[idx] == x[k])
369 c=x[idx];
370 else
371 {
372 if (x[idx] > x[k])
373 { memcpy(y,x+p,s); memcpy(x+p,x,s); memcpy(x,y,s); }
374 c=0.5*(x[idx] + x[k]);
375 }
376
377 for (j=m+idx; (j < p) && (x[j] <= c); j+=m);
378 for (k-=m; (k > idx) && (x[k] >= c); k-=m);
379 while (j < k)
380 {
381 memcpy(y,x+j-idx,s); memcpy(x+j-idx,x+k-idx,s); memcpy(x+k-idx,y,s);
382 while (x[j] <= c) j+=m;
383 while (x[k] >= c) k-=m;
384 }
385 if (k > idx)
386 b_qsort_matrix_columns_ascending_real(x,m,(k-idx)/m+1,idx);
387 if (j < p)
388 b_qsort_matrix_columns_ascending_real(x+j-idx,m,n-(j-idx)/m,idx);
389 }
390 else
391 {
392 // insertion sort
393 p=n*m;
394 for (j=m+idx; j < p; j+=m)
395 if (x[j-m] > x[j])
396 {
397 memcpy(y,x+j-idx,s);
398 memcpy(x+j-idx,x+j-m-idx,s);
399 for (k=j-m-m; k >= 0; k-=m)
400 if (x[k] <= y[idx])
401 break;
402 else
403 memcpy(x+k+m-idx,x+k-idx,s);
404 memcpy(x+k+m-idx,y,s);
405 }
406 }
407 dw_free(y);
408 }
409
410 /*
411 Assumes:
412 x - array of length m*n in colum major format.
413 m - number of rows
414 n - number of columns
415
416 Results:
417 The columns of x are sorted in ascending order on row idx.
418
419 Notes:
420 Uses the quick sort mean algorithm. Switches to insertion sort when the
421 size of the list is 10 or less. If the matrix is in row major format, then
422 m is the number of columns, n is the number of rows, and the rows of x are
423 sorted in ascending order on column idx.
424 */
425 static void b_qsort_matrix_columns_descending_real(PRECISION *x, int m, int n, int idx)
426 {
427 PRECISION *y, c;
428 int j, k, p, s;
429 y=(PRECISION*)dw_malloc(s=m*sizeof(PRECISION));
430 if (n > 10)
431 {
432 // quick sort
433 p=(n-1)*m;
434 k=p+idx;
435
436 if (x[idx] == x[k])
437 c=x[idx];
438 else
439 {
440 if (x[idx] < x[k])
441 { memcpy(y,x+p,s); memcpy(x+p,x,s); memcpy(x,y,s); }
442 c=0.5*(x[idx] + x[k]);
443 }
444
445 for (j=m+idx; (j < p) && (x[j] >= c); j+=m);
446 for (k-=m; (k > idx) && (x[k] <= c); k-=m);
447 while (j < k)
448 {
449 memcpy(y,x+j-idx,s); memcpy(x+j-idx,x+k-idx,s); memcpy(x+k-idx,y,s);
450 while (x[j] >= c) j+=m;
451 while (x[k] <= c) k-=m;
452 }
453 if (k > idx)
454 b_qsort_matrix_columns_descending_real(x,m,(k-idx)/m+1,idx);
455 if (j < p)
456 b_qsort_matrix_columns_descending_real(x+j-idx,m,n-(j-idx)/m,idx);
457 }
458 else
459 {
460 // insertion sort
461 p=n*m;
462 for (j=m+idx; j < p; j+=m)
463 if (x[j-m] < x[j])
464 {
465 memcpy(y,x+j-idx,s);
466 memcpy(x+j-idx,x+j-m-idx,s);
467 for (k=j-m-m; k >= 0; k-=m)
468 if (x[k] >= y[idx])
469 break;
470 else
471 memcpy(x+k+m-idx,x+k-idx,s);
472 memcpy(x+k+m-idx,y,s);
473 }
474 }
475 dw_free(y);
476 }
477
478 /*
479 Assumes:
480 x - array of length m*n in colum major format.
481 m - number of rows
482 n - number of columns
483 br - first row in block to sort
484 er - last row in block to sort to sort
485 idx - idx/m is column to sort
486
487 Results:
488 The rows of x are sorted in ascending order on column idx/m.
489
490 Notes:
491 Uses the quick sort mean algorithm. Switches to insertion sort when the
492 size of the list is 10 or less. If the matrix is in row major format, then
493 m is the number of columns, n is the number of rows, and the columns of x
494 are sorted in ascending order on row idx.
495 */
496 static void b_qsort_matrix_rows_ascending_real(PRECISION *x, int m, int n, int br, int er, int idx)
497 {
498 PRECISION y, c;
499 int i, j, k;
500 if (er-br+1 > 10)
501 {
502 // quick sort
503 if (x[idx+br] == x[idx+er])
504 c=x[idx+br];
505 else
506 {
507 if (x[idx+br] > x[idx+er])
508 for (i=(n-1)*m; i >= 0; i-=m)
509 { y=x[i+br]; x[i+br]=x[i+er]; x[i+er]=y; }
510 c=0.5*(x[idx+br] + x[idx+er]);
511 }
512
513 for (j=br+1; (j < er) && (x[idx+j] <= c); j++);
514 for (k=er-1; (k > br) && (x[idx+k] >= c); k--);
515 while (j < k)
516 {
517 for (i=(n-1)*m; i >= 0; i-=m)
518 { y=x[i+j]; x[i+j]=x[i+k]; x[i+k]=y; }
519 while (x[idx+j] <= c) j++;
520 while (x[idx+k] >= c) k--;
521 }
522 if (k > br)
523 b_qsort_matrix_rows_ascending_real(x,m,n,br,k,idx);
524 if (j < er)
525 b_qsort_matrix_rows_ascending_real(x,m,n,j,er,idx);
526 }
527 else
528 {
529 // insertion sort
530 int r;
531 for (j=br+1; j <= er; j++)
532 {
533 for (k=j-1; k >= br; k--)
534 if (x[idx+k] <= x[idx+j]) break;
535 if (++k < j)
536 for (i=(n-1)*m; i >= 0; i-=m)
537 {
538 y=x[i+j];
539 for (r=j; r > k; r--) x[i+r]=x[i+r-1];
540 x[i+k]=y;
541 }
542 }
543 }
544 }
545
546 /*
547 Assumes:
548 x - array of length m*n in colum major format.
549 m - number of rows
550 n - number of columns
551 br - first row in block to sort
552 er - last row in block to sort to sort
553 idx - idx/m is column to sort
554
555 Results:
556 The rows of x are sorted in ascending order on column idx/m.
557
558 Notes:
559 Uses the quick sort mean algorithm. Switches to insertion sort when the
560 size of the list is 10 or less. If the matrix is in row major format, then
561 m is the number of columns, n is the number of rows, and the columns of x
562 are sorted in ascending order on row idx.
563 */
564 static void b_qsort_matrix_rows_descending_real(PRECISION *x, int m, int n, int br, int er, int idx)
565 {
566 PRECISION y, c;
567 int i, j, k;
568 if (er-br+1 > 10)
569 {
570 // quick sort
571 if (x[idx+br] == x[idx+er])
572 c=x[idx+br];
573 else
574 {
575 if (x[idx+br] < x[idx+er])
576 for (i=(n-1)*m; i >= 0; i-=m)
577 { y=x[i+br]; x[i+br]=x[i+er]; x[i+er]=y; }
578 c=0.5*(x[idx+br] + x[idx+er]);
579 }
580
581 for (j=br+1; (j < er) && (x[idx+j] >= c); j++);
582 for (k=er-1; (k > br) && (x[idx+k] <= c); k--);
583 while (j < k)
584 {
585 for (i=(n-1)*m; i >= 0; i-=m)
586 { y=x[i+j]; x[i+j]=x[i+k]; x[i+k]=y; }
587 while (x[idx+j] >= c) j++;
588 while (x[idx+k] <= c) k--;
589 }
590 if (k > br)
591 b_qsort_matrix_rows_descending_real(x,m,n,br,k,idx);
592 if (j < er)
593 b_qsort_matrix_rows_descending_real(x,m,n,j,er,idx);
594 }
595 else
596 {
597 // insertion sort
598 int r;
599 for (j=br+1; j <= er; j++)
600 {
601 for (k=j-1; k >= br; k--)
602 if (x[idx+k] >= x[idx+j]) break;
603 if (++k < j)
604 for (i=(n-1)*m; i >= 0; i-=m)
605 {
606 y=x[i+j];
607 for (r=j; r > k; r--) x[i+r]=x[i+r-1];
608 x[i+k]=y;
609 }
610 }
611 }
612 }
613
614 /*
615 Assumes:
616 x - array of length m
617
618 Results:
619 x is sorted in ascending order
620
621 Notes:
622 Uses the quick sort median of three algorithm
623
624 Currently this function is not being used and may be untested
625
626 */
627 /* static void b_median_qsort_array_ascending(PRECISION *x, int m) */
628 /* { */
629 /* PRECISION y; */
630 /* int j, k; */
631 /* if (m > 10) */
632 /* { */
633 /* // Quick sort */
634 /* j=(m--)/2; */
635
636 /* y=x[j]; x[j]=x[1]; x[1]=y; */
637
638 /* if (x[1] > x[m]) */
639 /* if (x[0] > x[m]) */
640 /* if (x[0] > x[1]) */
641 /* { y=x[0]; x[0]=x[m]; x[m]=y; } */
642 /* else */
643 /* { y=x[0]; x[0]=x[m]; x[m]=x[1]; x[1]=y; } */
644 /* else */
645 /* { y=x[1]; x[1]=x[m]; x[m]=y; } */
646 /* else */
647 /* if (x[0] > x[1]) */
648 /* if (x[0] > x[m]) */
649 /* { y=x[0]; x[0]=x[1]; x[1]=x[m]; x[m]=y; } */
650 /* else */
651 /* { y=x[0]; x[0]=x[1]; x[1]=y; }; */
652
653 /* for (j=2; (j < m) && (x[j] <= x[1]); j++); */
654 /* for (k=m-1; (k > 1) && (x[k] >= x[1]); k--); */
655 /* while (j < k) */
656 /* { */
657 /* y=x[j]; x[j]=x[k]; x[k]=y; */
658 /* while (x[j] <= x[1]) j++; */
659 /* while (x[k] >= x[1]) k--; */
660 /* } */
661 /* if (k > 1) */
662 /* { */
663 /* y=x[k]; x[k]=x[1]; x[1]=y; */
664 /* b_median_qsort_array_ascending(x,k); */
665 /* } */
666 /* if (j < m) */
667 /* b_median_qsort_array_ascending(x+j,m-j+1); */
668 /* } */
669 /* else */
670 /* { */
671 /* // Insertion sort */
672 /* for (j=1; j < m; j++) */
673 /* { */
674 /* for (k=j-1; k >= 0; k--) */
675 /* if (x[j] >= x[k]) break; */
676 /* if (++k < j) */
677 /* { */
678 /* y=x[j]; */
679 /* memmove(x+k+1,x+k,(j-k)*sizeof(PRECISION)); */
680 /* x[k]=y; */
681 /* } */
682 /* } */
683 /* } */
684 /* } */
685