| 1 |
#ifndef _PermutationInversionCounter_h_ |
| 2 |
#define _PermutationInversionCounter_h_ |
| 3 |
/* PermutationInversionCounter.h |
| 4 |
* |
| 5 |
* Copyright (C) 2025 David Weenink |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "Permutation.h" |
| 22 |
//#include "PermutationInversionCounter_def.h" |
| 23 |
|
| 24 |
Thing_define (PermutationInversionCounter, Thing) { |
| 25 |
constPermutation pdata, potherInverse; // links |
| 26 |
autoPermutation psortingOrder; |
| 27 |
integer numberOfElements; |
| 28 |
autoINTVEC workspace; |
| 29 |
integer totalNumberOfInversions; |
| 30 |
integer totalNumberOfInversionsInInterval; |
| 31 |
integer numberOfInversionsRegistered; |
| 32 |
integer numberOfInversionsToRegister; |
| 33 |
integer posInSortedSelectedInversionIndices; |
| 34 |
INTVEC inversions; // the inversions as pairs of (ilow, ihigh) to be output, link to storage supplied by caller |
| 35 |
INTVEC otherInverse; // link to potherInverse -> p.get() supplied by user |
| 36 |
|
| 37 |
constINTVEC sortedSelectedInversionIndices; // link to storage supplied by caller |
| 38 |
|
| 39 |
void newData (constPermutation p); |
| 40 |
|
| 41 |
void reset (); |
| 42 |
|
| 43 |
integer mergeInversions (integer p, integer q, integer r); |
| 44 |
|
| 45 |
integer mergeSort (integer p, integer r); |
| 46 |
|
| 47 |
integer getNumberOfInversions (constPermutation p); |
| 48 |
|
| 49 |
integer getSelectedInversionsNotInOther (constPermutation p, constPermutation otherInverse, |
| 50 |
constINTVEC const& sortedSelectedInversionIndices, INTVEC const& out_inversions |
| 51 |
); |
| 52 |
|
| 53 |
integer getSelectedInversions ( |
| 54 |
constPermutation p, constINTVEC const& sortedSelectedInversionIndices, INTVEC const& out_inversions |
| 55 |
); |
| 56 |
|
| 57 |
integer getInversions (constPermutation p, INTVEC const& out_inversions); |
| 58 |
|
| 59 |
}; |
| 60 |
|
| 61 |
/* |
| 62 |
Given a vector v with n elements, we define an inversion as i < j && v [i] > v [j]. |
| 63 |
We use a trick to store the inversion between two elements (i,j) with only **one** number code by using the following |
| 64 |
lower triangular (n-1) x (n-1) matrix, with i being the **column** index and j being the **row** index! |
| 65 |
|
| 66 |
1 2 3 4 5 ... n-1 |
| 67 |
+--------------- ...-- |
| 68 |
1(2) | 1 |
| 69 |
2(3) | 2 3 |
| 70 |
3(4) | 4 5 6 |
| 71 |
4(5) | 7 8 9 10 |
| 72 |
5(6) | 11 12 13 14 15 |
| 73 |
... |
| 74 |
n-1(n)| n(n-1)/2 |
| 75 |
|
| 76 |
E.g. the inversion between element 1 and element 2 is coded with the number 1, |
| 77 |
the inversion between element 2 and 5 is code with the number 8. |
| 78 |
From inversion (i<j) to the code: |
| 79 |
code = (j-2)*(j-1)/2 + i |
| 80 |
From the code to the inversion (i,j) |
| 81 |
j = Melder_iroundUp (0.5 + sqrt (0.25 + 2 * code)); |
| 82 |
i = code - (j-2) * (j - 1) / 2; |
| 83 |
|
| 84 |
*/ |
| 85 |
|
| 86 |
inline void getInversionFromCode (integer code, integer& ilow, integer& ihigh) { |
| 87 |
ihigh = Melder_iroundUp (0.5 + sqrt (0.25 + 2 * code)); |
| 88 |
ilow = code - (ihigh - 2) * (ihigh - 1) / 2; |
| 89 |
Melder_assert (ilow < ihigh); |
| 90 |
} |
| 91 |
|
| 92 |
inline integer getCodeFromInversion (integer ilow, integer ihigh) { |
| 93 |
Melder_assert (ilow < ihigh); |
| 94 |
return (ihigh - 2) * (ihigh - 1) / 2 + ilow; |
| 95 |
} |
| 96 |
|
| 97 |
void PermutationInversionCounter_init (integer size); |
| 98 |
|
| 99 |
autoPermutationInversionCounter PermutationInversionCounter_create (integer size); |
| 100 |
|
| 101 |
#endif // _PermutationInversionCounter_h_ |