Debian Codegraph @

dwsys/NUMsorting.h

raw symbols SCIP
1 #cxx . . $ `<file>/dwsys/NUMsorting.h`/ifndef _NUMsorting_h_
2 #define _NUMsorting_h_cxx . . $ `dwsys/NUMsorting.h:2:9`!
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"cxx . . $ `<file>/melder/melder.h`/
22
23 #include "strings_sorting_enums.h"cxx . . $ `<file>/dwsys/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 NUMsortTogethercxx . . $ NUMsortTogether(5d5a7cfb164642bb). (Keys const& a, Compare compare, Values const& b) {
42 using ElementOfAcxx . . $ NUMsortTogether(5d5a7cfb164642bb).ElementOfA# = stdcxx . . $ std/::remove_reference_t <decltype (a [1])>;
43 using ElementOfBcxx . . $ NUMsortTogether(5d5a7cfb164642bb).ElementOfB# = stdcxx . . $ std/::remove_reference_t <decltype (b [1])>;
44 using stdcxx . . $ std/::swapcxx . . $ std/swap(90aec5ea4c4ede5e).;
45
46 Melder_assertcxx . . $ abort(15d318a79ddce9e0). (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 swapcxx . . $ NUMsortTogether(5d5a7cfb164642bb).swap(9df3a874cbadce38). (a [1], a [2]);
52 swapcxx . . $ NUMsortTogether(5d5a7cfb164642bb).swap(98e01073b2cf8874). (b [1], b [2]);
53 }
54 return;
55 }
56 if (a.size <= 12) {
57 for (integercxx . . $ integer# i = 1; i < a.size; i ++) {
58 ElementOfAcxx . . $ NUMsortTogether(5d5a7cfb164642bb).ElementOfA# min = a [i];
59 integercxx . . $ integer# imin = i;
60 for (integercxx . . $ 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 swapcxx . . $ NUMsortTogether(5d5a7cfb164642bb).swap(fd72003b7956e937). (b [imin], b [i]);
68 }
69 return;
70 }
71 /* H1 Initialize */
72 integercxx . . $ integer# l = (a.size >> 1) +cxx . . $ operator+(a1743dea189d4e35). 1;
73 integercxx . . $ integer# r = a.size;
74 for (;;) {
75 ElementOfAcxx . . $ NUMsortTogether(5d5a7cfb164642bb).ElementOfA# k;
76 ElementOfBcxx . . $ NUMsortTogether(5d5a7cfb164642bb).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 integercxx . . $ 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 NUMsortTogethercxx . . $ NUMsortTogether(13c11f7bd0e7e3d2). (Keys const& a, Values const& b) {
125 using ElementOfAcxx . . $ NUMsortTogether(13c11f7bd0e7e3d2).ElementOfA# = stdcxx . . $ std/::remove_reference_t <decltype (a [1])>;
126 static_assert (! stdcxx . . $ std/::is_pointer_v <ElementOfAcxx . . $ NUMsortTogether(13c11f7bd0e7e3d2).ElementOfA#>, "Pointer keys cannot be compared.");
127 NUMsortTogethercxx . . $ NUMsortTogether(13c11f7bd0e7e3d2). (a, stdcxx . . $ std/::lesscxx . . $ std/less#<>{}, b);
128 }
129
130 template <typename Values>
131 void NUMsortTogethercxx . . $ NUMsortTogether(fce7383e8ea9e411). (STRVECcxx . . $ STRVEC# const& a, Values const& b) {
132 NUMsortTogethercxx . . $ NUMsortTogether(fce7383e8ea9e411). (
133 a,
134 [] (char32cxx . . $ char32# *first, char32cxx . . $ char32# *last) {
135 return str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (first, last) < 0;
136 },
137 b
138 );
139 }
140
141 template <typename T>
142 void NUMreverseOrdercxx . . $ NUMreverseOrder(77d9f17553126ecb). (T const& vec) {
143 for (integercxx . . $ integer# i = 1; i <= vec.size / 2; i ++)
144 stdcxx . . $ std/::swapcxx . . $ std/swap(32e1b6853fc532ff). (vec [i], vec [vec.size +cxx . . $ operator+(2d1012574a38976e). 1 -cxx . . $ operator-(347dad5598ca2ed7). i]);
145 }
146
147 void VECsort3_inplacecxx . . $ VECsort3_inplace(9c39150d377b356d). (VECcxx . . $ VEC# const& a, INTVECcxx . . $ INTVEC# const& iv1, INTVECcxx . . $ 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_inoutcxx . . $ INTVECindex_inout(9436e2757451c0fb). (INTVECcxx . . $ INTVEC# index, constINTVECcxx . . $ constINTVEC# const& v) {
159 stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(4d3091562df4e52e). (index.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), index.endcxx . . $ vector#end(5a2a78a048fb49a8).(),
160 [& v] (integercxx . . $ integer# ix, integercxx . . $ integer# iy) {
161 return v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] < v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).iy];
162 }
163 );
164 }
165
166 inline void INTVECindex_inoutcxx . . $ INTVECindex_inout(639e9ce8f59cc0d6). (INTVECcxx . . $ INTVEC# index, constVECcxx . . $ constVEC# const& v) {
167 stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(4d3091562df4e52e). (index.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), index.endcxx . . $ vector#end(5a2a78a048fb49a8).(),
168 [& v] (integercxx . . $ integer# ix, integercxx . . $ integer# iy) {
169 return v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ix] < v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).iy];
170 }
171 );
172 }
173
174 inline void INTVECindex_inoutcxx . . $ INTVECindex_inout(343f0af1a4dd57fb). (INTVECcxx . . $ INTVEC# index, STRVECcxx . . $ STRVEC# const& v) {
175 stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(4d3091562df4e52e). (index.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), index.endcxx . . $ vector#end(5a2a78a048fb49a8).(),
176 [& v] (integercxx . . $ integer# ix, integercxx . . $ integer# iy) {
177 return Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (v [cxx . . $ _stringvector#`operator[]`(a7f36fee1ae21897).ix], v [cxx . . $ _stringvector#`operator[]`(a7f36fee1ae21897).iy]) < 0;
178 }
179 );
180 }
181
182 inline void INTVECindex_inoutcxx . . $ INTVECindex_inout(b1bcb50d503c374f). (INTVECcxx . . $ INTVEC# index, constSTRVECcxx . . $ constSTRVEC# const& v) {
183 stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(4d3091562df4e52e). (index.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), index.endcxx . . $ vector#end(5a2a78a048fb49a8).(),
184 [& v] (integercxx . . $ integer# ix, integercxx . . $ integer# iy) {
185 return Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (v [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).ix], v [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).iy]) < 0;
186 }
187 );
188 }
189
190 inline void INTVECindex_numberAware_inoutcxx . . $ INTVECindex_numberAware_inout(52c24aa879fe9217). (INTVECcxx . . $ INTVEC# const& index, constSTRVECcxx . . $ constSTRVEC# const& v) {
191 stdcxx . . $ std/::stable_sortcxx . . $ std/stable_sort(4d3091562df4e52e). (index.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), index.endcxx . . $ vector#end(5a2a78a048fb49a8).(),
192 [& v] (integercxx . . $ integer# ix, integercxx . . $ integer# iy) {
193 return str32coll_numberAwarecxx . . $ str32coll_numberAware(8be1707edab15857). (v [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).ix], v [cxx . . $ _conststringvector#`operator[]`(2065423e0524e64a).iy]) < 0;
194 }
195 );
196 }
197
198 inline autoINTVECcxx . . $ autoINTVEC# newINTVECindexcxx . . $ newINTVECindex(8e12c8178b2404dc). (constVECcxx . . $ constVEC# const& a) {
199 autoINTVECcxx . . $ autoINTVEC# result = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (a.sizecxx . . $ constvector#size.);
200 INTVECindex_inoutcxx . . $ INTVECindex_inout(639e9ce8f59cc0d6). (result.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), a);
201 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
202 }
203
204 inline autoINTVECcxx . . $ autoINTVEC# newINTVECindexcxx . . $ newINTVECindex(ba45d89df38dc5c8). (constINTVECcxx . . $ constINTVEC# const& a) {
205 autoINTVECcxx . . $ autoINTVEC# result = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (a.sizecxx . . $ constvector#size.);
206 INTVECindex_inoutcxx . . $ INTVECindex_inout(9436e2757451c0fb). (result.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), a);
207 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
208 }
209
210 inline autoINTVECcxx . . $ autoINTVEC# newINTVECindexcxx . . $ newINTVECindex(2a3ded6a1e48a31). (constSTRVECcxx . . $ constSTRVEC# const& a) {
211 autoINTVECcxx . . $ autoINTVEC# result = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (a.sizecxx . . $ _conststringvector#size.);
212 INTVECindex_inoutcxx . . $ INTVECindex_inout(b1bcb50d503c374f). (result.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), a);
213 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
214 }
215
216 void MATrankColumnscxx . . $ MATrankColumns(92b731f903f41049). (MATcxx . . $ MAT# m, integercxx . . $ integer# cb, integercxx . . $ 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 VECrankSortedcxx . . $ VECrankSorted(4452523f51a9471a). (VECVUcxx . . $ VECVU# const& a) {
224 integercxx . . $ integer# jt, j = 1;
225 while (j < a.sizecxx . . $ vectorview#size.) {
226 for (jt = j + 1; jt <= a.sizecxx . . $ vectorview#size. && a [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jt] == a [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).j]; jt ++) {
227 }
228 const double rank = (j + jt - 1) * 0.5;
229 for (integercxx . . $ integer# i = j; i <= jt - 1; i ++)
230 a [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i] = rank;
231 j = jt;
232 }
233 if (j == a.sizecxx . . $ vectorview#size.)
234 a [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vectorview#size.] = a.sizecxx . . $ vectorview#size.;
235 }
236
237 template <typename T>
238 automatrixcxx . . $ automatrix#<T> matrix_sortRowscxx . . $ matrix_sortRows(60e0ff80205b651c). (constmatrixcxx . . $ constmatrix#<T> const& m, constINTVECVUcxx . . $ constINTVECVU# const& columnNumbers) {
239 automatrixcxx . . $ automatrix#<T> result = newmatrixrawcxx . . $ newmatrixraw(902b25f6b635a55a).<T> (m.nrowcxx . . $ constmatrix#nrow., m.ncolcxx . . $ constmatrix#ncol.);
240 autoINTVECcxx . . $ autoINTVEC# rows = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (m.nrowcxx . . $ constmatrix#nrow.);
241 stdcxx . . $ std/::sortcxx . . $ std/sort(249673c20e2b8d5). (rows.begincxx . . $ autovector#begin(5a2a78a048fb49a8).(), rows.endcxx . . $ autovector#end(5a2a78a048fb49a8).(),
242 [&] (integercxx . . $ integer# rowi, integercxx . . $ integer# rowj) -> bool {
243 for (integercxx . . $ integer# icol = 1; icol <= columnNumbers.sizecxx . . $ constvectorview#size.; icol ++) {
244 const integercxx . . $ integer# col = columnNumbers [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).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 (integercxx . . $ integer# irow = 1; irow <= m.nrowcxx . . $ constmatrix#nrow.; irow ++)
254 result.rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow) <<=cxx . . $ `operator<<=`(f06f0c2ee3e650b7). m.rowcxx . . $ constmatrix#row(aae4ad074ae650ba). (rows [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow]);
255 return result;
256 }
257
258 inline autoINTMATcxx . . $ autoINTMAT# sortRows_INTMATcxx . . $ sortRows_INTMAT(53fa5db83f4673d3). (constINTMATcxx . . $ constINTMAT# const& m, constINTVECVUcxx . . $ constINTVECVU# const& columnNumbers) {
259 return matrix_sortRowscxx . . $ matrix_sortRows(60e0ff80205b651c).<integercxx . . $ integer#> (m, columnNumbers);
260 }
261
262 inline autoMATcxx . . $ autoMAT# sortRows_MATcxx . . $ sortRows_MAT(ac54bf071a79f5cf). (constMATcxx . . $ constMAT# const& m, constINTVECVUcxx . . $ constINTVECVU# const& columnNumbers) {
263 return matrix_sortRowscxx . . $ matrix_sortRows(60e0ff80205b651c).<double> (m, columnNumbers);
264 }
265
266 #endif /* _NUMsorting_h_ */