| 1 | /* melder_sort.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2015,2017-2022,2024,2025 Paul Boersma |
| 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 2 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. |
| 13 | * See the GNU 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 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 20 | |
| 21 | void shuffle_VEC_inoutcxx . . $ shuffle_VEC_inout(4452523f51a9471a). (VECVUcxx . . $ VECVU# const& x) { |
| 22 | for (integercxx . . $ integer# i = 1; i < x.sizecxx . . $ vectorview#size.; i ++) |
| 23 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i], x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (i, x.sizecxx . . $ vectorview#size.)]); |
| 24 | } |
| 25 | |
| 26 | void shuffle_INTVEC_inoutcxx . . $ shuffle_INTVEC_inout(751b3f60dca57818). (INTVECVUcxx . . $ INTVECVU# const& x) { |
| 27 | for (integercxx . . $ integer# i = 1; i < x.sizecxx . . $ vectorview#size.; i ++) |
| 28 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i], x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (i, x.sizecxx . . $ vectorview#size.)]); |
| 29 | } |
| 30 | |
| 31 | void shuffle_STRVEC_inoutcxx . . $ shuffle_STRVEC_inout(bedb44e9ae7c63cf). (STRVECcxx . . $ STRVEC# const& x) { |
| 32 | for (integercxx . . $ integer# i = 1; i < x.sizecxx . . $ _stringvector#size.; i ++) |
| 33 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (x [cxx . . $ _stringvector#`operator[]`(a7f36fee1ae21897).i], x [cxx . . $ _stringvector#`operator[]`(a7f36fee1ae21897).NUMrandomIntegercxx . . $ NUMrandomInteger(d0a0e4c270da0eca). (i, x.sizecxx . . $ _stringvector#size.)]); |
| 34 | } |
| 35 | autoSTRVECcxx . . $ autoSTRVEC# shuffle_STRVECcxx . . $ shuffle_STRVEC(6a8a775aea9c867b). (STRVECcxx . . $ STRVEC# const& x) { |
| 36 | autoSTRVECcxx . . $ autoSTRVEC# result = copy_STRVECcxx . . $ copy_STRVEC(cfa8029879abedac). (xcxx . . $ _conststringvector#_conststringvector(93d9e701af42cb0d).); |
| 37 | shuffle_STRVEC_inoutcxx . . $ shuffle_STRVEC_inout(bedb44e9ae7c63cf). (result.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 38 | return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).; |
| 39 | } |
| 40 | |
| 41 | void sort_e_VEC_inoutcxx . . $ sort_e_VEC_inout(e75f7985a5e05502). (VECcxx . . $ VEC# const& x) { |
| 42 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (NUMdefined (x), |
| 43 | U"Vector contains one or more undefined elements. Cannot sort."); |
| 44 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (x.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), x.endcxx . . $ vector#end(5a2a78a048fb49a8).(), |
| 45 | [] (double first, double last) { |
| 46 | return first < last; |
| 47 | } |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | void sort_VEC_inoutcxx . . $ sort_VEC_inout(e75f7985a5e05502). (VECcxx . . $ VEC# const& x) { |
| 52 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (x.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), x.endcxx . . $ vector#end(5a2a78a048fb49a8).(), |
| 53 | [] (double first, double last) { |
| 54 | return first < last; |
| 55 | } |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | autoVECcxx . . $ autoVEC# sort_removeUndefined_VECcxx . . $ sort_removeUndefined_VEC(d4415e68b25283a8). (constVECVUcxx . . $ constVECVU# const& vec) { |
| 60 | const integercxx . . $ integer# newSize = NUMcountDefinedcxx . . $ NUMcountDefined(a7a82c2bc2b2c948). (vec); |
| 61 | autoVECcxx . . $ autoVEC# result = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (newSize); |
| 62 | integercxx . . $ integer# latestIndex = 0; |
| 63 | for (integercxx . . $ integer# i = 1; i <= vec.sizecxx . . $ constvectorview#size.; i ++) |
| 64 | if (isdefinedcxx . . $ isdefined(f8197d536af80c96). (vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i])) |
| 65 | result [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ latestIndex] = vec [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i]; |
| 66 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (latestIndex == newSize); |
| 67 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (result.begincxx . . $ autovector#begin(5a2a78a048fb49a8).(), result.endcxx . . $ autovector#end(5a2a78a048fb49a8).(), |
| 68 | [] (double first, double last) { |
| 69 | return first < last; |
| 70 | } |
| 71 | ); |
| 72 | return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 73 | } |
| 74 | |
| 75 | void sort_INTVEC_inoutcxx . . $ sort_INTVEC_inout(f38cbc42cb047d9). (INTVECcxx . . $ INTVEC# const& x) noexcept { |
| 76 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (x.begincxx . . $ vector#begin(5a2a78a048fb49a8).(), x.endcxx . . $ vector#end(5a2a78a048fb49a8).(), |
| 77 | [] (integercxx . . $ integer# first, integercxx . . $ integer# last) { |
| 78 | return first < last; |
| 79 | } |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | void sort_STRVEC_inoutcxx . . $ sort_STRVEC_inout(276ad51d55a4042f). (STRVECcxx . . $ STRVEC# const& array) noexcept { |
| 84 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (array.begincxx . . $ _stringvector#begin(3871cfe815cb200).(), array.endcxx . . $ _stringvector#end(3871cfe815cb200).(), |
| 85 | [] (conststring32cxx . . $ conststring32# first, conststring32cxx . . $ conststring32# last) { |
| 86 | return str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (first, last) < 0; |
| 87 | } |
| 88 | ); |
| 89 | } |
| 90 | autoSTRVECcxx . . $ autoSTRVEC# sort_STRVECcxx . . $ sort_STRVEC(6a8a775aea9c867b). (STRVECcxx . . $ STRVEC# const& x) { |
| 91 | autoSTRVECcxx . . $ autoSTRVEC# result = copy_STRVECcxx . . $ copy_STRVEC(cfa8029879abedac). (xcxx . . $ _conststringvector#_conststringvector(93d9e701af42cb0d).); |
| 92 | sort_STRVEC_inoutcxx . . $ sort_STRVEC_inout(276ad51d55a4042f). (result.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 93 | return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).; |
| 94 | } |
| 95 | |
| 96 | void sort_numberAware_STRVEC_inoutcxx . . $ sort_numberAware_STRVEC_inout(276ad51d55a4042f). (STRVECcxx . . $ STRVEC# const& array) noexcept { |
| 97 | stdcxx . . $ std/::sortcxx . . $ std/sort(4d3091562df4e52e). (array.begincxx . . $ _stringvector#begin(3871cfe815cb200).(), array.endcxx . . $ _stringvector#end(3871cfe815cb200).(), |
| 98 | [] (conststring32cxx . . $ conststring32# first, conststring32cxx . . $ conststring32# last) { |
| 99 | return str32coll_numberAwarecxx . . $ str32coll_numberAware(8be1707edab15857). (first, last) < 0; |
| 100 | } |
| 101 | ); |
| 102 | } |
| 103 | autoSTRVECcxx . . $ autoSTRVEC# sort_numberAware_STRVECcxx . . $ sort_numberAware_STRVEC(6a8a775aea9c867b). (STRVECcxx . . $ STRVEC# const& x) { |
| 104 | autoSTRVECcxx . . $ autoSTRVEC# result = copy_STRVECcxx . . $ copy_STRVEC(cfa8029879abedac). (xcxx . . $ _conststringvector#_conststringvector(93d9e701af42cb0d).); |
| 105 | sort_numberAware_STRVEC_inoutcxx . . $ sort_numberAware_STRVEC_inout(276ad51d55a4042f). (result.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 106 | return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).; |
| 107 | } |
| 108 | |
| 109 | double NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (constVECVUcxx . . $ constVECVU# const& a, double factor) noexcept { |
| 110 | if (a.sizecxx . . $ constvectorview#size. < 1) |
| 111 | return undefinedcxx . . $ undefined.; |
| 112 | if (a.sizecxx . . $ constvectorview#size. == 1) |
| 113 | return a [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).1]; |
| 114 | const double place = factor * a.sizecxx . . $ constvectorview#size. + 0.5; |
| 115 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.size - 1 >= 1); |
| 116 | const integercxx . . $ integer# left = Melder_clippedcxx . . $ Melder_clipped(5eb09607cedc971b). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (place), a.sizecxx . . $ constvectorview#size. - 1); |
| 117 | const double slope = a [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).left + 1] - a [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).left]; |
| 118 | if (slope == 0.0) |
| 119 | return a [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).left]; // or a [left + 1], which is the same |
| 120 | return a [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).left] + (place - left) * slope; |
| 121 | } |
| 122 | |
| 123 | /* End of file melder_sort.cpp */ |