| 1 |
/* NUMsorting.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1993-2022 David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
djmw 20030121 Initial version |
| 21 |
djmw 20030627 Removed bug in MACRO_NUMindex |
| 22 |
djmw 20120305 Latest modification |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "NUMsorting.h" |
| 26 |
#include "enums_getText.h" |
| 27 |
#include "strings_sorting_enums.h" |
| 28 |
#include "enums_getValue.h" |
| 29 |
#include "strings_sorting_enums.h" |
| 30 |
|
| 31 |
void MATrankColumns (MAT m, integer cb, integer ce) { |
| 32 |
Melder_assert (cb > 0 && cb <= m.ncol); |
| 33 |
Melder_assert (ce > 0 && ce <= m.ncol); |
| 34 |
Melder_assert (cb <= ce); |
| 35 |
autoVEC v = raw_VEC (m.nrow); |
| 36 |
autoINTVEC index = raw_INTVEC (m.nrow); |
| 37 |
|
| 38 |
for (integer j = cb; j <= ce; j ++) { |
| 39 |
v.all() <<= m.column (j); |
| 40 |
for (integer i = 1; i <= m.nrow; i ++) |
| 41 |
index [i] = i; |
| 42 |
NUMsortTogether (v.get(), index.get()); |
| 43 |
VECrankSorted (v.get()); |
| 44 |
for (integer i = 1; i <= m.nrow; i ++) |
| 45 |
m [index [i]] [j] = v [i]; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void VECsort3_inplace (VEC const& a, INTVEC const& iv1, INTVEC const& iv2, bool descending) { |
| 50 |
Melder_assert (a.size == iv1.size && a.size == iv2.size); |
| 51 |
if (a.size == 1) |
| 52 |
return; |
| 53 |
autoVEC atmp = copy_VEC (a); |
| 54 |
autoINTVEC index = newINTVECindex (atmp.get()); |
| 55 |
if (descending) |
| 56 |
for (integer j = 1; j <= a.size / 2; j ++) |
| 57 |
std::swap (index [j], index [a.size - j + 1]); |
| 58 |
|
| 59 |
for (integer j = 1; j <= a.size; j ++) |
| 60 |
a [j] = atmp [index [j]]; |
| 61 |
autoINTVEC itmp = raw_INTVEC (a.size); |
| 62 |
itmp.all() <<= iv1; |
| 63 |
for (integer j = 1; j <= a.size; j ++) |
| 64 |
iv1 [j] = itmp [index [j]]; |
| 65 |
itmp.all() <<= iv2; |
| 66 |
for (integer j = 1; j <= a.size; j ++) |
| 67 |
iv2 [j] = itmp [index [j]]; |
| 68 |
} |
| 69 |
|
| 70 |
autoINTMAT sortRows_INTMAT (constINTMAT mat, integer icol) { // TODO test me |
| 71 |
try { |
| 72 |
Melder_require (icol > 0 && icol <= mat.ncol, |
| 73 |
U"The column index is not valid."); |
| 74 |
autoINTVEC colvals = raw_INTVEC (mat.nrow); |
| 75 |
for (integer irow = 1; irow <= mat.nrow; irow ++) |
| 76 |
colvals [irow] = mat [irow] [icol]; |
| 77 |
autoINTVEC index = newINTVECindex (colvals.get()); |
| 78 |
autoINTMAT result = raw_INTMAT (mat.nrow, mat.ncol); |
| 79 |
for (integer irow = 1; irow <= mat.nrow; irow ++) |
| 80 |
result [index [irow]] <<= mat [irow]; |
| 81 |
return result; |
| 82 |
} catch (MelderError) { |
| 83 |
Melder_throw (U"Matrix not sorted."); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/* End of file NUMsorting.cpp */ |