| 1 |
#ifndef _Permutation_h_ |
| 2 |
#define _Permutation_h_ |
| 3 |
/* Permutation.h |
| 4 |
* |
| 5 |
* Copyright (C) 2005-2022, 2025-2026 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 "melder.h" |
| 22 |
#include "Collection.h" |
| 23 |
#include "Graphics.h" |
| 24 |
#include "NUMsorting.h" |
| 25 |
|
| 26 |
#include "Permutation_def.h" |
| 27 |
|
| 28 |
/* |
| 29 |
Class invariant: any permutation equals the identity permutation after all its elements are sorted ascendingly. |
| 30 |
Alternative: a permutation of size n contains all numbers from 1 to n. |
| 31 |
*/ |
| 32 |
|
| 33 |
void Permutation_init (Permutation me, integer numberOfElements); |
| 34 |
|
| 35 |
autoPermutation Permutation_createSimplePermutation (constINTVEC const& numbers); |
| 36 |
|
| 37 |
void Permutation_tableJump_inline (Permutation me, integer jumpSize, integer first); |
| 38 |
|
| 39 |
autoPermutation Permutation_create (integer numberOfElements, bool identity); |
| 40 |
/* |
| 41 |
Create the Permutation data structure and fill |
| 42 |
with the identical permutation (1,2,..n) if identity is true |
| 43 |
else create a random permutation |
| 44 |
*/ |
| 45 |
|
| 46 |
autoPermutation Permutation_createAsSortingIndex (constSTRVEC const& strvec, kStrings_sorting sorting); |
| 47 |
|
| 48 |
void Permutation_drawAsLine (Permutation me, Graphics g, bool garnish); |
| 49 |
|
| 50 |
void Permutation_checkInvariant (Permutation me); |
| 51 |
/* Check that the elements, if sorted ascendingly, are exactly equal to the identity (1,2,...). */ |
| 52 |
|
| 53 |
|
| 54 |
void Permutation_sort (Permutation me); |
| 55 |
/* Set p [1..n] = 1,..n */ |
| 56 |
|
| 57 |
integer Permutation_getNumberOfInversions (Permutation me); |
| 58 |
/* |
| 59 |
Count the number of times when i < j && p [i] > p [j]. |
| 60 |
E.g. {1,2,3} -> 0 inversions; {3,2,1} -> 3; {1,3,2} -> 1 inversion. |
| 61 |
*/ |
| 62 |
|
| 63 |
autoINTVEC Permutation_getRandomInversionIndices (Permutation me, integer maxNumberOfRandomInversions); |
| 64 |
autoINTVEC Permutation_getAllInversionIndices (Permutation me); |
| 65 |
|
| 66 |
autoMAT Permutation_getRandomInversions (Permutation me, integer maxNumberOfRandomInversions); /* convenience for interface */ |
| 67 |
autoMAT Permutation_getAllInversions (Permutation me); /* convenience for interface */ |
| 68 |
|
| 69 |
void Permutation_permuteRandomly_inplace (Permutation me, integer from, integer to); |
| 70 |
|
| 71 |
autoPermutation Permutation_permuteRandomly (Permutation me, integer from, integer to); |
| 72 |
/* Generate a new sequence by permuting the elements from..to */ |
| 73 |
|
| 74 |
autoPermutation Permutation_rotate (Permutation me, integer from, integer to, integer step); |
| 75 |
|
| 76 |
void Permutation_swapOneFromRange (Permutation me, integer from, integer to, integer pos, bool forbidsame); |
| 77 |
/* Swap item at pos with one randomly chosen in interval [from,to]. If pos in [from,to] |
| 78 |
and forbidsame==true then new position may not be equal to pos. */ |
| 79 |
|
| 80 |
void Permutation_swapBlocks (Permutation me, integer from, integer to, integer blocksize); |
| 81 |
/* Swap two blocks */ |
| 82 |
|
| 83 |
void Permutation_swapPositions (Permutation me, integer i1, integer i2); |
| 84 |
|
| 85 |
void Permutation_swapNumbers (Permutation me, integer i1, integer i2); |
| 86 |
|
| 87 |
autoPermutation Permutation_interleave (Permutation me, integer from, integer to, integer blocksize, integer offset); |
| 88 |
|
| 89 |
autoPermutation Permutation_permuteBlocksRandomly (Permutation me, integer from, integer to, integer blocksize, bool permuteWithinBlocks, bool noDoublets); |
| 90 |
/* Permute blocks of size blocksize randomly. If permuteWithinBlocks=true and noDoublets=true forbid that the last |
| 91 |
number in a block and the first number in the following block are 'equal modulo blocksize'. */ |
| 92 |
|
| 93 |
integer Permutation_getValueAtIndex (Permutation me, integer i); |
| 94 |
/* return i > 0 && i < my n ? my p [i] : -1 */ |
| 95 |
|
| 96 |
integer Permutation_getIndexAtValue (Permutation me, integer value); |
| 97 |
/* Find i for which p [i] = value */ |
| 98 |
|
| 99 |
autoPermutation Permutation_invert (Permutation me); |
| 100 |
void Permutation_invert_into (Permutation me, mutablePermutation result); |
| 101 |
/* */ |
| 102 |
|
| 103 |
void Permutation_reverse_inline (Permutation me, integer from, integer to); |
| 104 |
autoPermutation Permutation_reverse (Permutation me, integer from, integer to); |
| 105 |
/* (n1,n2,...nn) to (nn,...n2,n1) */ |
| 106 |
|
| 107 |
void Permutation_next_inplace (Permutation me); |
| 108 |
|
| 109 |
void Permutation_previous_inplace (Permutation me); |
| 110 |
|
| 111 |
autoPermutation Permutations_multiply2 (Permutation me, Permutation thee); |
| 112 |
void Permutations_multiply2_into (Permutation me, Permutation thee, mutablePermutation result); |
| 113 |
|
| 114 |
autoPermutation Permutations_multiply (OrderedOf<structPermutation>* me); |
| 115 |
|
| 116 |
autoPermutation Permutation_permutePartByOther (Permutation me, integer startPosition, Permutation other); |
| 117 |
|
| 118 |
void Permutation_permuteSubsetByOther_inout (Permutation me, constINTVEC const& subsetPositions, Permutation other); |
| 119 |
|
| 120 |
void Permutation_permuteVEC_inout (Permutation me, VEC vec); |
| 121 |
void Permutation_permuteINTVEC_inout (Permutation me, INTVEC vec); |
| 122 |
|
| 123 |
void Permutation_permuteSTRVEC_inout (Permutation me, autoSTRVEC & vec); // special |
| 124 |
|
| 125 |
autoPermutation Permutation_moveElementsToTheFront (Permutation me, constINTVEC const& subsetPositions); |
| 126 |
|
| 127 |
void Permutations_swap (Permutation me, Permutation thee); |
| 128 |
|
| 129 |
#endif /* _Permutation_h_ */ |