| 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"cxx . . $ `<file>/dwsys/NUMsorting.h`/ |
| 26 | #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/ |
| 27 | #include "strings_sorting_enums.h"cxx . . $ `<file>/dwsys/strings_sorting_enums.h`/ |
| 28 | #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/ |
| 29 | #include "strings_sorting_enums.h"cxx . . $ `<file>/dwsys/strings_sorting_enums.h`/ |
| 30 | |
| 31 | void MATrankColumnscxx . . $ MATrankColumns(92b731f903f41049). (MATcxx . . $ MAT# m, integercxx . . $ integer# cb, integercxx . . $ integer# ce) { |
| 32 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (cb > 0 && cb <= m.ncol); |
| 33 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (ce > 0 && ce <= m.ncol); |
| 34 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (cb <= ce); |
| 35 | autoVECcxx . . $ autoVEC# v = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (m.nrowcxx . . $ matrix#nrow.); |
| 36 | autoINTVECcxx . . $ autoINTVEC# index = raw_INTVECcxx . . $ raw_INTVEC(dab5d3ef28d6a25). (m.nrowcxx . . $ matrix#nrow.); |
| 37 | |
| 38 | for (integercxx . . $ integer# j = cb; j <= ce; j ++) { |
| 39 | v.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). mcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..columncxx . . $ matrix#column(a8b9553e85c2b1fc). (j); |
| 40 | for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ matrix#nrow.; i ++) |
| 41 | index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = i; |
| 42 | NUMsortTogethercxx . . $ NUMsortTogether(13c11f7bd0e7e3d2). (v.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), index.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 43 | VECrankSortedcxx . . $ VECrankSorted(4452523f51a9471a). (vcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 44 | for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ matrix#nrow.; i ++) |
| 45 | m [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = v [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void VECsort3_inplacecxx . . $ VECsort3_inplace(9c39150d377b356d). (VECcxx . . $ VEC# const& a, INTVECcxx . . $ INTVEC# const& iv1, INTVECcxx . . $ INTVEC# const& iv2, bool descending) { |
| 50 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.size == iv1.size && a.size == iv2.size); |
| 51 | if (a.sizecxx . . $ vector#size. == 1) |
| 52 | return; |
| 53 | autoVECcxx . . $ autoVEC# atmp = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (acxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); |
| 54 | autoINTVECcxx . . $ autoINTVEC# index = newINTVECindexcxx . . $ newINTVECindex(8e12c8178b2404dc). (atmpcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 55 | if (descending) |
| 56 | for (integercxx . . $ integer# j = 1; j <= a.sizecxx . . $ vector#size. / 2; j ++) |
| 57 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j], index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vector#size. - j + 1]); |
| 58 | |
| 59 | for (integercxx . . $ integer# j = 1; j <= a.sizecxx . . $ vector#size.; j ++) |
| 60 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = atmp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]]; |
| 61 | autoINTVECcxx . . $ autoINTVEC# itmp = raw_INTVECcxx . . $ raw_INTVEC(dab5d3ef28d6a25). (a.sizecxx . . $ vector#size.); |
| 62 | itmp.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(2db3a4bfcf125477). iv1cxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).; |
| 63 | for (integercxx . . $ integer# j = 1; j <= a.sizecxx . . $ vector#size.; j ++) |
| 64 | iv1 [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = itmp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]]; |
| 65 | itmp.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(2db3a4bfcf125477). iv2cxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).; |
| 66 | for (integercxx . . $ integer# j = 1; j <= a.sizecxx . . $ vector#size.; j ++) |
| 67 | iv2 [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = itmp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]]; |
| 68 | } |
| 69 | |
| 70 | autoINTMATcxx . . $ autoINTMAT# sortRows_INTMATcxx . . $ sortRows_INTMAT(c81dba6a32d08526). (constINTMATcxx . . $ constINTMAT# mat, integercxx . . $ integer# icol) { // TODO test me |
| 71 | try { |
| 72 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (icol > 0 && icol <= mat.ncol, |
| 73 | U"The column index is not valid."); |
| 74 | autoINTVECcxx . . $ autoINTVEC# colvals = raw_INTVECcxx . . $ raw_INTVEC(dab5d3ef28d6a25). (mat.nrowcxx . . $ constmatrix#nrow.); |
| 75 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrix#nrow.; irow ++) |
| 76 | colvals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow] = mat [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).irow] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol]; |
| 77 | autoINTVECcxx . . $ autoINTVEC# index = newINTVECindexcxx . . $ newINTVECindex(ba45d89df38dc5c8). (colvalscxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 78 | autoINTMATcxx . . $ autoINTMAT# result = raw_INTMATcxx . . $ raw_INTMAT(4cdd710bd00a100c). (mat.nrowcxx . . $ constmatrix#nrow., mat.ncolcxx . . $ constmatrix#ncol.); |
| 79 | for (integercxx . . $ integer# irow = 1; irow <= mat.nrowcxx . . $ constmatrix#nrow.; irow ++) |
| 80 | resultcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow]] <<=cxx . . $ `operator<<=`(2db3a4bfcf125477). matcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf). [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).irow]; |
| 81 | return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).; |
| 82 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 83 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Matrix not sorted."); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* End of file NUMsorting.cpp */ |