Alex_St

Untitled

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