Debian Codegraph @

dwsys/Permutation_and_Index.cpp

raw symbols SCIP
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"
26
27 autoPermutation Index_to_Permutation_permuteRandomly (Index me, bool permuteWithinClasses) {
28 try {
29 const integer numberOfClasses = my classes->size;
30
31 autoPermutation thee = Permutation_create (my numberOfItems, true);
32 autoPermutation classes = Permutation_create (numberOfClasses, false);
33 autoPermutation classesinv = Permutation_invert (classes.get());
34 autoINTMAT indices = zero_INTMAT (numberOfClasses, 4);
35
36 for (integer i = 1; i <= my numberOfItems; i ++)
37 indices [my classIndex [i]] [2] ++; // column 2: number of elements in class
38
39 /* Get some other indices ready */
40 for (integer i = 1; i <= numberOfClasses; i ++) {
41 const integer klass = classes -> p [i];
42 indices [i] [1] = klass;
43 indices [i] [3] = ( i == 1 ? 0 : indices [i - 1] [3] + indices [i - 1] [2] ); // column 3: index at start of class
44 }
45
46 for (integer i = 1; i <= my numberOfItems; i ++) {
47 const integer klass = my classIndex [i];
48 const integer newindex = classesinv -> p [klass];
49 indices [newindex] [4] ++; // column 4: number of elements processed for class
50 const integer newpos = indices [newindex] [3] + indices [newindex] [4];
51 thy p [newpos] = i;
52 }
53 if (permuteWithinClasses) {
54 for (integer i = 1; i <= numberOfClasses; i ++) {
55 const integer from = indices [i] [3] + 1;
56 const integer to = from + indices [i] [2] - 1;
57 if (to > from)
58 Permutation_permuteRandomly_inplace (thee.get(), from, to);
59 }
60 }
61 return thee;
62 } catch (MelderError) {
63 Melder_throw (me, U": Permutation not created.");
64 }
65 }
66
67 /* End of file Permutation_and_Index.cpp */