shizaero

Untitled

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