-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
100 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <list> | ||
#include <cstdint> | ||
#include <iterator> | ||
#include <utility> | ||
|
||
using namespace std; | ||
|
||
|
||
// Вспомогательная функция, позволяющая «зациклить» список | ||
template <typename Container, typename ForwardIt> | ||
ForwardIt LoopIterator(Container& container, ForwardIt pos) { | ||
return pos == container.end() ? container.begin() : pos; | ||
} | ||
|
||
template <typename RandomIt> | ||
void MakeJosephusPermutation(RandomIt first, RandomIt last, | ||
uint32_t step_size) { | ||
list<typename RandomIt::value_type> pool; | ||
for (auto it = first; it != last; ++it) { | ||
pool.push_back(move(*it)); | ||
} | ||
auto cur_pos = pool.begin(); | ||
while (!pool.empty()) { | ||
*(first++) = move(*cur_pos); | ||
if (pool.size() == 1) { | ||
break; | ||
} | ||
const auto next_pos = LoopIterator(pool, next(cur_pos)); | ||
pool.erase(cur_pos); | ||
cur_pos = next_pos; | ||
for (uint32_t step_index = 1; step_index < step_size; ++step_index) { | ||
cur_pos = LoopIterator(pool, next(cur_pos)); | ||
} | ||
} | ||
} |