-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutation.hh
146 lines (121 loc) · 2.87 KB
/
Permutation.hh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef PERMUTATION_HH
#define PERMUTATION_HH
#include <set>
#include <utility>
template< typename Element,
auto set,
template< typename,
typename > typename Map >
class Permutation
{
private:
Map< Element, Element > m;
bool
element_of_set( Element q )
{
for( Element element : set )
{
if( q == element )
{
return true;
}
}
return false;
}
bool
without_repeating_elements( )
{
for( std::set< Element > s;
auto element : set )
{
if( 1u == s.count( element ) )
{
return false;
}
s.insert( element );
}
return true;
}
bool
injectiveness( )
{
for( std::set< Element > destinations;
Element element : set )
{
if( false == destinations.insert( operator()( element ) ).second )
{
return false;
}
}
return true;
}
bool
surjectiveness( )
{
std::set< Element > destinations;
for( const Element element : set )
{
destinations.insert( operator()( element ) );
}
return set.size( ) == destinations.size( );
}
public:
Permutation( )
{
assert( without_repeating_elements( ) );
for( const Element element : set )
{
m[ element ] = element;
}
}
Permutation( std::vector<
std::pair<
Element,
Element >
> transitions )
: Permutation( )
{
for( std::set< Element > sources,
destinations;
auto [ source, destination ] : transitions )
{
assert( true == sources.insert( source ).second );
assert( true == destinations.insert( destination ).second );
}
for( auto [ source, destination ] : transitions )
{
m[ source ] = destination;
assert( element_of_set( source ) &&
element_of_set( destination ) );
}
assert( true == injectiveness( ) &&
true == surjectiveness( ) );
}
Element
operator()( const Element q )
{
assert( element_of_set( q ) );
return m.at( q );
}
template< typename Permutation >
Permutation
operator*( Permutation q ) const
{
static_assert(
std::is_same_v< Permutation,
std::decay_t< decltype( *this ) >
>
);
std::vector<
std::pair<
Element,
Element >
> r;
for( const auto [ source, destination ] : m )
{
r.emplace_back( source, q( destination ) );
}
return Permutation( r );
}
};
#endif