-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation.cxx
259 lines (205 loc) · 8.25 KB
/
permutation.cxx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// vim: set ts=4 sw=4 tw=120:
//=====================================================================================================================|
//
// Copyright (c) 2012, Marsh Ray
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
// OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
//=====================================================================================================================|
//
// permutation.cxx
#include "qak/permutation.hxx"
#include <cstring> // memcpy
namespace qak { //=====================================================================================================|
namespace permutation_imp {
template <int N>
bool index_permutation_imp<N>::empty() const
{
assert(f_.size() == r_.size());
return f_.empty();
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
typename index_permutation_imp<N>::uint_type index_permutation_imp<N>::size() const
{
auto sz = f_.size();
assert(sz == r_.size());
assert(sz <= std::numeric_limits<uint_type>::max());
return static_cast<uint_type>(sz);
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
typename index_permutation_imp<N>::uint_type index_permutation_imp<N>::f_at(uint_type ix) const
{
auto sz = f_.size();
assert(sz == r_.size());
assert(ix < sz);
return f_[ix];
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
typename index_permutation_imp<N>::uint_type index_permutation_imp<N>::r_at(uint_type ix) const
{
auto sz = r_.size();
assert(sz == f_.size());
assert(ix < sz);
return r_[ix];
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::set_identity()
{
reset(size());
};
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::reset(uint_type n)
{
f_.resize(0);
r_.resize(0);
extend(n);
};
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::invert()
{
f_.swap(r_);
};
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::extend(uint_type n)
{
uint_type sz = size();
uint_type new_sz = sz + n;
f_.reserve(new_sz);
r_.reserve(new_sz);
for (uint_type ix = sz; ix < new_sz; ++ix)
{
f_.push_back(ix);
r_.push_back(ix);
}
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::swap_two(uint_type ix_a, uint_type ix_b)
{
uint_type jx_a = f_[ix_a]; assert(r_[jx_a] == ix_a);
uint_type jx_b = f_[ix_b]; assert(r_[jx_b] == ix_b);
f_[ix_a] = jx_b;
f_[ix_b] = jx_a;
r_[jx_a] = ix_b;
r_[jx_b] = ix_a;
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::rotate(uint_type ix_b, uint_type ix_m, uint_type ix_e)
{
assert(0 <= ix_b && ix_b <= ix_m && ix_m <= ix_e && ix_e <= f_.size());
//? OPT See the very nice section from Programming Pearls (available online) with several other
// algorithms benchmarked for permuting the forward vector.
uint_type sz_l = ix_m - ix_b;
uint_type sz_r = ix_e - ix_m;
// Early exit if there's no effective rotation.
if ( ! (sz_l && sz_r) )
return;
if (sz_l == sz_r) // left is equal to the right
{
vector_type v_tmp(&f_[ix_b], &f_[ix_b + sz_l]);
std::memcpy(&f_[ix_b], &f_[ix_m], sz_l*sizeof(typename vector_type::value_type));
std::memcpy(&f_[ix_m], &v_tmp[0], sz_l*sizeof(typename vector_type::value_type));
}
else // unequal left and right
{
if (sz_l < sz_r) // left is smaller than the right
{
vector_type v_tmp(&f_[ix_e - sz_l], &f_[ix_e - 1] + 1);
std::memcpy(&f_[ix_e - sz_l], &f_[ix_b], sz_l*sizeof(typename vector_type::value_type));
std::memmove(&f_[ix_b], &f_[ix_m], (sz_r - sz_l)*sizeof(typename vector_type::value_type));
std::memcpy(&f_[ix_b + sz_r - sz_l], &v_tmp[0], sz_l*sizeof(typename vector_type::value_type));
}
else // right is smaller than the left
{
vector_type v_tmp(&f_[ix_b], &f_[ix_b + sz_r]);
std::memcpy(&f_[ix_b], &f_[ix_m], sz_r*sizeof(typename vector_type::value_type));
std::memmove(&f_[ix_b + sz_r*2], &f_[ix_b + sz_r], (sz_l - sz_r)*sizeof(typename vector_type::value_type));
std::memcpy(&f_[ix_b + sz_r], &v_tmp[0], sz_r*sizeof(typename vector_type::value_type));
}
}
// Fix up the reverse mapping.
//? OPT There might be some somewhat faster ways of doing this.
// For example, we could track the lowest and highest reverse index seen when we do the forward rotate,
// and then only fix up that. Also, we could modify the reverse mapping arithmetically without referencing
// memory in the forward vector.
for (uint_type ix = ix_b; ix < ix_e; ++ix)
r_[f_[ix]] = ix;
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::square()
{
// Save f_.
f_.swap(r_);
// Compute f_ from saved f_.
uint_type sz = size();
for (uint_type ix = 0; ix < sz; ++ix)
f_[ix] = r_[r_[ix]];
// Compute r_ from f_.
for (uint_type ix = 0; ix < sz; ++ix)
r_[f_[ix]] = ix;
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::mult_by(index_permutation_imp const & that)
{
assert(this->size() == that.size());
// Save f_.
f_.swap(r_);
// Compute f_ from saved f_.
uint_type sz = size();
for (uint_type ix = 0; ix < sz; ++ix)
f_[ix] = r_[that.f_[ix]];
// Compute r_ from f_.
for (uint_type ix = 0; ix < sz; ++ix)
r_[f_[ix]] = ix;
}
//-----------------------------------------------------------------------------------------------------------------|
template <int N>
void index_permutation_imp<N>::remove(uint_type ix)
{
assert(0 <= ix && ix < size());
uint_type new_size = size() - 1;
uint_type f_ix = ix;
uint_type r_ix = f_[f_ix];
for (uint_type ix = 0; ix < f_ix; ++ix)
if (r_ix < f_[ix])
--f_[ix];
for (uint_type ix = f_ix; ix + 1 < f_.size(); ++ix)
f_[ix] = f_[ix + 1] - (r_ix < f_[ix + 1]);
f_.resize(new_size);
for (uint_type ix = 0; ix < r_ix; ++ix)
if (f_ix < r_[ix])
--r_[ix];
for (uint_type ix = r_ix; ix + 1 < r_.size(); ++ix)
r_[ix] = r_[ix + 1] - (f_ix < r_[ix + 1]);
r_.resize(new_size);
}
//=================================================================================================================|
//-----------------------------------------------------------------------------------------------------------------|
//=================================================================================================================|
// Cause instantiation of the templates in this translation unit.
template struct index_permutation_imp<1>;
template struct index_permutation_imp<2>;
template struct index_permutation_imp<4>;
template struct index_permutation_imp<8>;
} // namespace permutation_imp
} // namespace qak ====================================================================================================|