-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutation.h
89 lines (67 loc) · 1.99 KB
/
permutation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef PERMUTATION_H
#define PERMUTATION_H
#include "tiny_id_func.h"
#include <string>
template<class IDIDFunc>
bool is_permutation(const IDIDFunc&f){
if(f.preimage_count() != f.image_count())
return false;
int id_count = f.preimage_count();
BitIDFunc already_seen(id_count);
already_seen.fill(false);
for(int i=0; i<id_count; ++i){
int x = f(i);
if(x < 0 || x >= id_count)
return false;
if(already_seen(x))
return false;
already_seen.set(x, true);
}
return true;
}
template<class IDIDFunc>
ArrayIDIDFunc inverse_permutation(const IDIDFunc&f){
assert(is_permutation(f));
int id_count = f.preimage_count();
ArrayIDIDFunc inv_f(id_count, id_count);
for(int i=0; i<id_count; ++i)
inv_f[f(i)] = i;
return inv_f; // NVRO
}
inline
ArrayIDIDFunc identity_permutation(int id_count){
ArrayIDIDFunc f(id_count, id_count);
for(int i=0; i<id_count; ++i)
f[i] = i;
return f; // NVRO
}
ArrayIDIDFunc load_permutation(const std::string& order_file);
ArrayIDIDFunc uncached_load_permutation(const std::string& order_file);
void save_permutation(const std::string& order_file, const ArrayIDIDFunc&order);
/*
template<class Permutation, class IDFunc>
typename std::enable_if<
is_only_id_func<IDFunc>::value,
ArrayIDFunc<typename id_func_image_type<IDFunc>::type>
>::type apply_permutation(const Permutation&p, const IDFunc&f){
assert(is_permutation(p));
assert(p.image_count() == f.preimage_count());
ArrayIDFunc<typename id_func_image_type<IDFunc>::type> result(p.preimage_count());
for(int i=0; i<p.preimage_count(); ++i)
result[i] = f(p(i));
return result; // NVRO
}
template<class Permutation, class IDIDFunc>
typename std::enable_if<
is_id_id_func<IDIDFunc>::value,
ArrayIDIDFunc
>::type apply_permutation(const Permutation&p, const IDIDFunc&f){
assert(is_permutation(p));
assert(p.image_count() == f.preimage_count());
ArrayIDIDFunc result(p.preimage_count(), f.image_count());
for(int i=0; i<p.preimage_count(); ++i)
result[i] = f(p(i));
return result; // NVRO
}
*/
#endif