| 1 | #ifndef _NUMsorting_h_ |
| 2 | #define _NUMsorting_h_ |
| 3 | /* NUMsorting.h |
| 4 | * |
| 5 | * Copyright (C) 2022,2025 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "melder.h" |
| 22 | |
| 23 | #include "strings_sorting_enums.h" |
| 24 | |
| 25 | /* |
| 26 | Uses heapsort to sort the second array in parallel with the first one. |
| 27 | |
| 28 | Algorithm follows p. 145 and 642 in: |
| 29 | Donald E. Knuth (1998): The art of computer programming. Third edition. Vol. 3: sorting and searching. |
| 30 | Boston: Addison-Wesley, printed May 2002. |
| 31 | Modification: there is no distinction between record and key, and |
| 32 | Floyd's optimization (page 642) is used. |
| 33 | Sorts (in place) an array a [1..n] into ascending order using the Heapsort algorithm, |
| 34 | while making the corresponding rearrangement of the companion |
| 35 | array b [1..n]. A characteristic of heapsort is that it does not conserve |
| 36 | the order of equals: e.g., the array 3,1,1,2 will be sorted as 1,1,2,3 and |
| 37 | it may occur that the first 1 after sorting came from position 3 and the second |
| 38 | 1 came from position 2. |
| 39 | */ |
| 40 | template <typename Keys, typename Compare, typename Values> |
| 41 | void NUMsortTogether (Keys const& a, Compare compare, Values const& b) { |
| 42 | using ElementOfA = std::remove_reference_t <decltype (a [1])>; |
| 43 | using ElementOfB = std::remove_reference_t <decltype (b [1])>; |
| 44 | using std::swap; |
| 45 | |
| 46 | Melder_assert (a.size == b.size); |
| 47 | if (a.size < 2) |
| 48 | return; // already sorted |
| 49 | if (a.size == 2) { |
| 50 | if (compare (a [2], a [1])) { |
| 51 | swap (a [1], a [2]); |
| 52 | swap (b [1], b [2]); |
| 53 | } |
| 54 | return; |
| 55 | } |
| 56 | if (a.size <= 12) { |
| 57 | for (integer i = 1; i < a.size; i ++) { |
| 58 | ElementOfA min = a [i]; |
| 59 | integer imin = i; |
| 60 | for (integer j = i + 1; j <= a.size; j ++) |
| 61 | if (compare (a [j], min)) { |
| 62 | min = a [j]; |
| 63 | imin = j; |
| 64 | } |
| 65 | a [imin] = a [i]; |
| 66 | a [i] = min; |
| 67 | swap (b [imin], b [i]); |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | /* H1 Initialize */ |
| 72 | integer l = (a.size >> 1) + 1; |
| 73 | integer r = a.size; |
| 74 | for (;;) { |
| 75 | ElementOfA k; |
| 76 | ElementOfB kb; |
| 77 | /* H2 Decrease */ |
| 78 | if (l > 1) { |
| 79 | l --; |
| 80 | k = a [l]; |
| 81 | kb = b [l]; |
| 82 | } else /* l == 1 */ { |
| 83 | k = a [r]; |
| 84 | kb = b [r]; |
| 85 | a [r] = a [1]; |
| 86 | b [r] = b [1]; |
| 87 | r --; |
| 88 | if (r == 1) { |
| 89 | a [1] = k; |
| 90 | b [1] = kb; |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | /* H3 */ |
| 95 | integer i, j = l; |
| 96 | for (;;) { /* H4 */ |
| 97 | i = j; |
| 98 | j = j << 1; |
| 99 | if (j > r) |
| 100 | break; |
| 101 | if (j < r && compare (a [j], a [j + 1])) |
| 102 | j ++; /* H5 */ |
| 103 | /* if (k >= a [j]) break; H6 */ |
| 104 | a [i] = a [j]; |
| 105 | b [i] = b [j]; /* H7 */ |
| 106 | } |
| 107 | /* a [i] = k; b [i] = kb; H8 */ |
| 108 | for (;;) { /*H8' */ |
| 109 | j = i; |
| 110 | i = j >> 1; |
| 111 | /* H9' */ |
| 112 | if (j == l || ! compare (a [i], k)) { |
| 113 | a [j] = k; |
| 114 | b [j] = kb; |
| 115 | break; |
| 116 | } |
| 117 | a [j] = a [i]; |
| 118 | b [j] = b [i]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | template <typename Keys, typename Values> |
| 124 | void NUMsortTogether (Keys const& a, Values const& b) { |
| 125 | using ElementOfA = std::remove_reference_t <decltype (a [1])>; |
| 126 | static_assert (! std::is_pointer_v <ElementOfA>, "Pointer keys cannot be compared."); |
| 127 | NUMsortTogether (a, std::less<>{}, b); |
| 128 | } |
| 129 | |
| 130 | template <typename Values> |
| 131 | void NUMsortTogether (STRVEC const& a, Values const& b) { |
| 132 | NUMsortTogether ( |
| 133 | a, |
| 134 | [] (char32 *first, char32 *last) { |
| 135 | return str32cmp (first, last) < 0; |
| 136 | }, |
| 137 | b |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | template <typename T> |
| 142 | void NUMreverseOrder (T const& vec) { |
| 143 | for (integer i = 1; i <= vec.size / 2; i ++) |
| 144 | std::swap (vec [i], vec [vec.size + 1 - i]); |
| 145 | } |
| 146 | |
| 147 | void VECsort3_inplace (VEC const& a, INTVEC const& iv1, INTVEC const& iv2, bool descending); // TODO template |
| 148 | /* Sort a together with iv1 and iv2 */ |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | Precondition: index must have been initialised with (1, 2, 3, ... index.size) |
| 153 | The interpretation of the index is as follows: |
| 154 | v[index[i]] <= v[index[j]] if i < j, |
| 155 | therefore if index [i] is 2, 4, 10, ... |
| 156 | the order is: v[2] <= v[4] <= v[20] <= ... |
| 157 | */ |
| 158 | inline void INTVECindex_inout (INTVEC index, constINTVEC const& v) { |
| 159 | std::stable_sort (index.begin(), index.end(), |
| 160 | [& v] (integer ix, integer iy) { |
| 161 | return v [ix] < v [iy]; |
| 162 | } |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | inline void INTVECindex_inout (INTVEC index, constVEC const& v) { |
| 167 | std::stable_sort (index.begin(), index.end(), |
| 168 | [& v] (integer ix, integer iy) { |
| 169 | return v [ix] < v [iy]; |
| 170 | } |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | inline void INTVECindex_inout (INTVEC index, STRVEC const& v) { |
| 175 | std::stable_sort (index.begin(), index.end(), |
| 176 | [& v] (integer ix, integer iy) { |
| 177 | return Melder_cmp (v [ix], v [iy]) < 0; |
| 178 | } |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | inline void INTVECindex_inout (INTVEC index, constSTRVEC const& v) { |
| 183 | std::stable_sort (index.begin(), index.end(), |
| 184 | [& v] (integer ix, integer iy) { |
| 185 | return Melder_cmp (v [ix], v [iy]) < 0; |
| 186 | } |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | inline void INTVECindex_numberAware_inout (INTVEC const& index, constSTRVEC const& v) { |
| 191 | std::stable_sort (index.begin(), index.end(), |
| 192 | [& v] (integer ix, integer iy) { |
| 193 | return str32coll_numberAware (v [ix], v [iy]) < 0; |
| 194 | } |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | inline autoINTVEC newINTVECindex (constVEC const& a) { |
| 199 | autoINTVEC result = to_INTVEC (a.size); |
| 200 | INTVECindex_inout (result.get(), a); |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | inline autoINTVEC newINTVECindex (constINTVEC const& a) { |
| 205 | autoINTVEC result = to_INTVEC (a.size); |
| 206 | INTVECindex_inout (result.get(), a); |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | inline autoINTVEC newINTVECindex (constSTRVEC const& a) { |
| 211 | autoINTVEC result = to_INTVEC (a.size); |
| 212 | INTVECindex_inout (result.get(), a); |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | void MATrankColumns (MAT m, integer cb, integer ce); |
| 217 | |
| 218 | /* |
| 219 | Replace content of sorted array by rank number, including midranking of ties. |
| 220 | E.g. The elements {10, 20.1, 20.1, 20.1, 20.1, 30} in array a will be replaced |
| 221 | by {1, 3.5, 3.5, 3.5, 3.5, 4}, respectively. |
| 222 | */ |
| 223 | inline void VECrankSorted (VECVU const& a) { |
| 224 | integer jt, j = 1; |
| 225 | while (j < a.size) { |
| 226 | for (jt = j + 1; jt <= a.size && a [jt] == a [j]; jt ++) { |
| 227 | } |
| 228 | const double rank = (j + jt - 1) * 0.5; |
| 229 | for (integer i = j; i <= jt - 1; i ++) |
| 230 | a [i] = rank; |
| 231 | j = jt; |
| 232 | } |
| 233 | if (j == a.size) |
| 234 | a [a.size] = a.size; |
| 235 | } |
| 236 | |
| 237 | template <typename T> |
| 238 | automatrix<T> matrix_sortRows (constmatrix<T> const& m, constINTVECVU const& columnNumbers) { |
| 239 | automatrix<T> result = newmatrixraw<T> (m.nrow, m.ncol); |
| 240 | autoINTVEC rows = to_INTVEC (m.nrow); |
| 241 | std::sort (rows.begin(), rows.end(), |
| 242 | [&] (integer rowi, integer rowj) -> bool { |
| 243 | for (integer icol = 1; icol <= columnNumbers.size; icol ++) { |
| 244 | const integer col = columnNumbers [icol]; |
| 245 | if (m [rowi] [col] < m [rowj] [col]) |
| 246 | return true; |
| 247 | if (m [rowi] [col] > m [rowj] [col]) |
| 248 | return false; |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | ); |
| 253 | for (integer irow = 1; irow <= m.nrow; irow ++) |
| 254 | result.row (irow) <<= m.row (rows [irow]); |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | inline autoINTMAT sortRows_INTMAT (constINTMAT const& m, constINTVECVU const& columnNumbers) { |
| 259 | return matrix_sortRows<integer> (m, columnNumbers); |
| 260 | } |
| 261 | |
| 262 | inline autoMAT sortRows_MAT (constMAT const& m, constINTVECVU const& columnNumbers) { |
| 263 | return matrix_sortRows<double> (m, columnNumbers); |
| 264 | } |
| 265 | |
| 266 | #endif /* _NUMsorting_h_ */ |