| 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" |
| 20 | |
| 21 | void shuffle_VEC_inout (VECVU const& x) { |
| 22 | for (integer i = 1; i < x.size; i ++) |
| 23 | std::swap (x [i], x [NUMrandomInteger (i, x.size)]); |
| 24 | } |
| 25 | |
| 26 | void shuffle_INTVEC_inout (INTVECVU const& x) { |
| 27 | for (integer i = 1; i < x.size; i ++) |
| 28 | std::swap (x [i], x [NUMrandomInteger (i, x.size)]); |
| 29 | } |
| 30 | |
| 31 | void shuffle_STRVEC_inout (STRVEC const& x) { |
| 32 | for (integer i = 1; i < x.size; i ++) |
| 33 | std::swap (x [i], x [NUMrandomInteger (i, x.size)]); |
| 34 | } |
| 35 | autoSTRVEC shuffle_STRVEC (STRVEC const& x) { |
| 36 | autoSTRVEC result = copy_STRVEC (x); |
| 37 | shuffle_STRVEC_inout (result.get()); |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | void sort_e_VEC_inout (VEC const& x) { |
| 42 | Melder_require (NUMdefined (x), |
| 43 | U"Vector contains one or more undefined elements. Cannot sort."); |
| 44 | std::sort (x.begin(), x.end(), |
| 45 | [] (double first, double last) { |
| 46 | return first < last; |
| 47 | } |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | void sort_VEC_inout (VEC const& x) { |
| 52 | std::sort (x.begin(), x.end(), |
| 53 | [] (double first, double last) { |
| 54 | return first < last; |
| 55 | } |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | autoVEC sort_removeUndefined_VEC (constVECVU const& vec) { |
| 60 | const integer newSize = NUMcountDefined (vec); |
| 61 | autoVEC result = raw_VEC (newSize); |
| 62 | integer latestIndex = 0; |
| 63 | for (integer i = 1; i <= vec.size; i ++) |
| 64 | if (isdefined (vec [i])) |
| 65 | result [++ latestIndex] = vec [i]; |
| 66 | Melder_assert (latestIndex == newSize); |
| 67 | std::sort (result.begin(), result.end(), |
| 68 | [] (double first, double last) { |
| 69 | return first < last; |
| 70 | } |
| 71 | ); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | void sort_INTVEC_inout (INTVEC const& x) noexcept { |
| 76 | std::sort (x.begin(), x.end(), |
| 77 | [] (integer first, integer last) { |
| 78 | return first < last; |
| 79 | } |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | void sort_STRVEC_inout (STRVEC const& array) noexcept { |
| 84 | std::sort (array.begin(), array.end(), |
| 85 | [] (conststring32 first, conststring32 last) { |
| 86 | return str32cmp (first, last) < 0; |
| 87 | } |
| 88 | ); |
| 89 | } |
| 90 | autoSTRVEC sort_STRVEC (STRVEC const& x) { |
| 91 | autoSTRVEC result = copy_STRVEC (x); |
| 92 | sort_STRVEC_inout (result.get()); |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | void sort_numberAware_STRVEC_inout (STRVEC const& array) noexcept { |
| 97 | std::sort (array.begin(), array.end(), |
| 98 | [] (conststring32 first, conststring32 last) { |
| 99 | return str32coll_numberAware (first, last) < 0; |
| 100 | } |
| 101 | ); |
| 102 | } |
| 103 | autoSTRVEC sort_numberAware_STRVEC (STRVEC const& x) { |
| 104 | autoSTRVEC result = copy_STRVEC (x); |
| 105 | sort_numberAware_STRVEC_inout (result.get()); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | double NUMquantile (constVECVU const& a, double factor) noexcept { |
| 110 | if (a.size < 1) |
| 111 | return undefined; |
| 112 | if (a.size == 1) |
| 113 | return a [1]; |
| 114 | const double place = factor * a.size + 0.5; |
| 115 | Melder_assert (a.size - 1 >= 1); |
| 116 | const integer left = Melder_clipped (1_integer, Melder_ifloor (place), a.size - 1); |
| 117 | const double slope = a [left + 1] - a [left]; |
| 118 | if (slope == 0.0) |
| 119 | return a [left]; // or a [left + 1], which is the same |
| 120 | return a [left] + (place - left) * slope; |
| 121 | } |
| 122 | |
| 123 | /* End of file melder_sort.cpp */ |