AlexSSH

Untitled

Jan 28th, 2023
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. template<typename RandomIt>
  6. void MakeJosephusPermutation(RandomIt first, RandomIt last, uint32_t step_size) {
  7.     vector<typename RandomIt::value_type> pool(std::move(first), std::move(last));
  8.     size_t cur_pos = 0;
  9.     while (!pool.empty()) {
  10.         *(first++) = pool[cur_pos];
  11.         pool.erase(pool.begin() + cur_pos);
  12.         if (pool.empty()) {
  13.             break;
  14.         }
  15.         cur_pos = (cur_pos + step_size - 1) % pool.size();
  16.     }
  17. }
  18.  
  19. vector<int> MakeTestVector() {
  20.     vector<int> numbers(10);
  21.     iota(begin(numbers), end(numbers), 0);
  22.     return numbers;
  23. }
  24.  
  25. void TestIntVector() {
  26.     const vector<int> numbers = MakeTestVector();
  27.     {
  28.         vector<int> numbers_copy = numbers;
  29.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 1);
  30.         assert(numbers_copy == numbers);
  31.     }
  32.     {
  33.         vector<int> numbers_copy = numbers;
  34.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 3);
  35.         assert(numbers_copy == vector<int>({0, 3, 6, 9, 4, 8, 5, 2, 7, 1}));
  36.     }
  37. }
  38.  
  39. // Это специальный тип, который поможет вам убедиться, что ваша реализация
  40. // функции MakeJosephusPermutation не выполняет копирование объектов.
  41. // Сейчас вы, возможно, не понимаете как он устроен, однако мы расскажем
  42. // об этом далее в нашем курсе
  43.  
  44. struct NoncopyableInt {
  45.     int value;
  46.  
  47.     NoncopyableInt(const NoncopyableInt &) = delete;
  48.  
  49.     NoncopyableInt &operator=(const NoncopyableInt &) = delete;
  50.  
  51.     NoncopyableInt(NoncopyableInt &&) = default;
  52.  
  53.     NoncopyableInt &operator=(NoncopyableInt &&) = default;
  54. };
  55.  
  56. bool operator==(const NoncopyableInt &lhs, const NoncopyableInt &rhs) {
  57.     return lhs.value == rhs.value;
  58. }
  59.  
  60. ostream &operator<<(ostream &os, const NoncopyableInt &v) {
  61.     return os << v.value;
  62. }
  63.  
  64.  
  65. void TestAvoidsCopying() {
  66.     vector<NoncopyableInt> numbers;
  67.     numbers.push_back({1});
  68.     numbers.push_back({2});
  69.     numbers.push_back({3});
  70.     numbers.push_back({4});
  71.     numbers.push_back({5});
  72.  
  73.     MakeJosephusPermutation(begin(numbers), end(numbers), 2);
  74.  
  75.     vector<NoncopyableInt> expected;
  76.     expected.push_back({1});
  77.     expected.push_back({3});
  78.     expected.push_back({5});
  79.     expected.push_back({4});
  80.     expected.push_back({2});
  81.  
  82.     assert(numbers == expected);
  83. }
  84.  
  85. int main() {
  86.     TestIntVector();
  87.     TestAvoidsCopying();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment