| 1 | /* Permutation_and_Index.cpp |
| 2 | * |
| 3 | * Copyright (C) 2005-2012,2015 David Weenink, 2011,2012,2015-2017,2019,2020 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 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. |
| 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 | /* |
| 20 | * djmw 20050725 |
| 21 | * djmw 20110503 Latest modification |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "Permutation_and_Index.h"cxx . . $ `<file>/dwsys/Permutation_and_Index.h`/ |
| 26 | |
| 27 | autoPermutationcxx . . $ autoPermutation# Index_to_Permutation_permuteRandomlycxx . . $ Index_to_Permutation_permuteRandomly(4bcc13cf49fc2833). (Indexcxx . . $ Index# me, bool permuteWithinClasses) { |
| 28 | try { |
| 29 | const integercxx . . $ integer# numberOfClasses = mycxx . . $ `melder/melder.h:44:9`! classescxx . . $ structIndex#classes.->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).sizecxx . . $ CollectionOf#size.; |
| 30 | |
| 31 | autoPermutationcxx . . $ autoPermutation# thee = Permutation_createcxx . . $ Permutation_create(50161d5495f26400). (mycxx . . $ `melder/melder.h:44:9`! numberOfItemscxx . . $ structIndex#numberOfItems., true); |
| 32 | autoPermutationcxx . . $ autoPermutation# classes = Permutation_createcxx . . $ Permutation_create(50161d5495f26400). (numberOfClasses, false); |
| 33 | autoPermutationcxx . . $ autoPermutation# classesinv = Permutation_invertcxx . . $ Permutation_invert(183440ac36fdd98b). (classes.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 34 | autoINTMATcxx . . $ autoINTMAT# indices = zero_INTMATcxx . . $ zero_INTMAT(4cdd710bd00a100c). (numberOfClasses, 4); |
| 35 | |
| 36 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfItemscxx . . $ structIndex#numberOfItems.; i ++) |
| 37 | indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).mycxx . . $ `melder/melder.h:44:9`! classIndexcxx . . $ structIndex#classIndex. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] ++; // column 2: number of elements in class |
| 38 | |
| 39 | /* Get some other indices ready */ |
| 40 | for (integercxx . . $ integer# i = 1; i <= numberOfClasses; i ++) { |
| 41 | const integercxx . . $ integer# klass = classes ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pcxx . . $ structPermutation#p. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 42 | indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = klass; |
| 43 | indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] = ( i == 1 ? 0 : indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i - 1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] + indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i - 1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] ); // column 3: index at start of class |
| 44 | } |
| 45 | |
| 46 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfItemscxx . . $ structIndex#numberOfItems.; i ++) { |
| 47 | const integercxx . . $ integer# klass = mycxx . . $ `melder/melder.h:44:9`! classIndexcxx . . $ structIndex#classIndex. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 48 | const integercxx . . $ integer# newindex = classesinv ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pcxx . . $ structPermutation#p. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).klass]; |
| 49 | indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).newindex] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).4] ++; // column 4: number of elements processed for class |
| 50 | const integercxx . . $ integer# newpos = indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).newindex] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] + indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).newindex] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).4]; |
| 51 | thycxx . . $ `melder/melder.h:45:9`! pcxx . . $ structPermutation#p. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).newpos] = i; |
| 52 | } |
| 53 | if (permuteWithinClasses) { |
| 54 | for (integercxx . . $ integer# i = 1; i <= numberOfClasses; i ++) { |
| 55 | const integercxx . . $ integer# from = indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] + 1; |
| 56 | const integercxx . . $ integer# to = from + indices [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] - 1; |
| 57 | if (to > from) |
| 58 | Permutation_permuteRandomly_inplacecxx . . $ Permutation_permuteRandomly_inplace(2a83444aef2ae232). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), from, to); |
| 59 | } |
| 60 | } |
| 61 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 62 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 63 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": Permutation not created."); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* End of file Permutation_and_Index.cpp */ |