shushus

Спринт 8. Функция move и другие перемещения.

Feb 2nd, 2023
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <numeric>
  4. #include <vector>
  5. #include <list>
  6. #include <utility>
  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;
  13.     for (auto iter = first; iter != last; ++iter){
  14.         pool.push_back(move(*iter));
  15.     }
  16.     size_t cur_pos = 0;
  17.     while (!pool.empty()) {
  18.         auto iter = pool.begin();
  19.         advance(iter, cur_pos);
  20.         *(first++) = move(*iter);
  21.         iter = pool.erase(iter);
  22.         if (pool.empty()) {
  23.             break;
  24.         }
  25.         cur_pos = (cur_pos+step_size-1)% pool.size();
  26.     }
  27. }
  28.  
  29. vector<int> MakeTestVector() {
  30.     vector<int> numbers(10);
  31.     iota(begin(numbers), end(numbers), 0);
  32.     return numbers;
  33. }
  34.  
  35. void TestIntVector() {
  36.     const vector<int> numbers = MakeTestVector();
  37.     {
  38.         vector<int> numbers_copy = numbers;
  39.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 1);
  40.         assert(numbers_copy == numbers);
  41.     }
  42.     {
  43.         vector<int> numbers_copy = numbers;
  44.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 3);
  45.         assert(numbers_copy == vector<int>({0, 3, 6, 9, 4, 8, 5, 2, 7, 1}));
  46.     }
  47. }
  48.  
  49. struct NoncopyableInt {
  50.     int value;
  51.  
  52.     NoncopyableInt(const NoncopyableInt&) = delete;
  53.     NoncopyableInt& operator=(const NoncopyableInt&) = delete;
  54.  
  55.     NoncopyableInt(NoncopyableInt&&) = default;
  56.     NoncopyableInt& operator=(NoncopyableInt&&) = default;
  57. };
  58.  
  59. bool operator==(const NoncopyableInt& lhs, const NoncopyableInt& rhs) {
  60.     return lhs.value == rhs.value;
  61. }
  62.  
  63. ostream& operator<<(ostream& os, const NoncopyableInt& v) {
  64.     return os << v.value;
  65. }
  66.  
  67. void TestAvoidsCopying() {
  68.     vector<NoncopyableInt> numbers;
  69.     numbers.push_back({1});
  70.     numbers.push_back({2});
  71.     numbers.push_back({3});
  72.     numbers.push_back({4});
  73.     numbers.push_back({5});
  74.  
  75.     MakeJosephusPermutation(begin(numbers), end(numbers), 2);
  76.  
  77.     vector<NoncopyableInt> expected;
  78.     expected.push_back({1});
  79.     expected.push_back({3});
  80.     expected.push_back({5});
  81.     expected.push_back({4});
  82.     expected.push_back({2});
  83.  
  84.     assert(numbers == expected);
  85. }
  86.  
  87. int main() {
  88.     TestIntVector();
  89.     TestAvoidsCopying();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment