shizaero

Untitled

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