HOG2
utils
Permutations.h
Go to the documentation of this file.
1
//
2
// Permutations.h
3
// hog2 mac native demos
4
//
5
// Created by Nathan Sturtevant on 5/8/21.
6
// Copyright © 2021 NS Software. All rights reserved.
7
//
8
9
#ifndef Permutations_h
10
#define Permutations_h
11
12
#include "
Combinations.h
"
13
14
// Standard N^2 algorithm for a permutation of N elements
15
template
<
int
N>
16
class
Permutations
{
17
public
:
18
uint64_t
MaxRank
()
const
{
return
ConstFactorial
(N); }
19
uint64_t
Rank
(
const
int
*items)
const
20
{
21
uint64_t hashVal = 0;
22
// local copy that can be modified
23
int
values[N];
24
for
(
unsigned
int
x = 0; x < N; x++)
25
{
26
values[x] = items[x];
27
}
28
29
for
(
unsigned
int
x = 0; x < N; x++)
30
{
31
hashVal += values[x]*
ConstFactorial
(N-1-x);
32
for
(
unsigned
y = x; y < N; y++)
33
{
34
if
(values[y] > values[x])
35
values[y]--;
36
}
37
}
38
return
hashVal;
39
}
40
41
void
Unrank
(uint64_t hash,
int
*items)
const
42
{
43
for
(
int
x = 0; x < N; x++)
44
{
45
items[x] = hash/
ConstFactorial
(N-1-x);
46
hash = hash%
ConstFactorial
(N-1-x);
47
}
48
for
(
int
x = N-2; x >= 0; x--)
49
{
50
for
(
int
y = x+1; y < N; y++)
51
{
52
if
(items[y] >= items[x])
53
items[y]++;
54
}
55
}
56
57
}
58
};
59
60
61
#endif
/* Permutations_h */
Combinations.h
Permutations::MaxRank
uint64_t MaxRank() const
Definition:
Permutations.h:18
Permutations::Unrank
void Unrank(uint64_t hash, int *items) const
Definition:
Permutations.h:41
Permutations::Rank
uint64_t Rank(const int *items) const
Definition:
Permutations.h:19
Permutations
Definition:
Permutations.h:16
ConstFactorial
constexpr uint64_t ConstFactorial(int n)
Definition:
Combinations.h:11
Generated by
1.8.17