shizaero

Untitled

Aug 22nd, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 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.     size_t cur_pos = 0;
  14.     for(auto it = first; it < last; it++){
  15.         pool.push_back(move(*(it+cur_pos)));
  16.         cur_pos = (cur_pos + step_size - 1) % pool.size();
  17.     }
  18.    
  19. }
  20.  
  21. vector<int> MakeTestVector() {
  22.     vector<int> numbers(10);
  23.     iota(begin(numbers), end(numbers), 0);
  24.     return numbers;
  25. }
  26.  
  27. void TestIntVector() {
  28.     const vector<int> numbers = MakeTestVector();
  29.     {
  30.         vector<int> numbers_copy = numbers;
  31.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 1);
  32.         assert(numbers_copy == numbers);
  33.     }
  34.     {
  35.         vector<int> numbers_copy = numbers;
  36.         MakeJosephusPermutation(begin(numbers_copy), end(numbers_copy), 3);
  37.         assert(numbers_copy == vector<int>({0, 3, 6, 9, 4, 8, 5, 2, 7, 1}));
  38.     }
  39. }
  40.  
  41. // Это специальный тип, который поможет вам убедиться, что ваша реализация
  42. // функции MakeJosephusPermutation не выполняет копирование объектов.
  43. // Сейчас вы, возможно, не понимаете как он устроен, однако мы расскажем
  44. // об этом далее в нашем курсе
  45.  
  46. struct NoncopyableInt {
  47.     int value;
  48.  
  49.     NoncopyableInt(const NoncopyableInt&) = delete;
  50.     NoncopyableInt& operator=(const NoncopyableInt&) = delete;
  51.  
  52.     NoncopyableInt(NoncopyableInt&&) = default;
  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. void TestAvoidsCopying() {
  65.     vector<NoncopyableInt> numbers;
  66.     numbers.push_back({1});
  67.     numbers.push_back({2});
  68.     numbers.push_back({3});
  69.     numbers.push_back({4});
  70.     numbers.push_back({5});
  71.  
  72.     MakeJosephusPermutation(begin(numbers), end(numbers), 2);
  73.  
  74.     vector<NoncopyableInt> expected;
  75.     expected.push_back({1});
  76.     expected.push_back({3});
  77.     expected.push_back({5});
  78.     expected.push_back({4});
  79.     expected.push_back({2});
  80.  
  81.     assert(numbers == expected);
  82. }
  83.  
  84. int main() {
  85.     TestIntVector();
  86.     TestAvoidsCopying();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment