12311k

Untitled

Feb 22nd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <list>
  4. #include <numeric>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. template <typename Container, typename ForwardIt>
  11. ForwardIt LoopIterator(Container& container, ForwardIt pos) {
  12. return pos == container.end() ? container.begin() : pos;
  13. }
  14.  
  15. template <typename RandomIt>
  16. void MakeJosephusPermutation(RandomIt first, RandomIt last, uint32_t step_size) {
  17. list<typename RandomIt::value_type> pool;
  18. for (auto it = first; it != last; ++it) {
  19. pool.push_back(move(*it));
  20. }
  21. auto cur_pos = pool.begin();
  22. while (!pool.empty()) {
  23. *(first++) = move(*cur_pos);
  24. if (pool.size() == 1) {
  25. break;
  26. }
  27. const auto next_pos = LoopIterator(pool, next(cur_pos));
  28. pool.erase(cur_pos);
  29. cur_pos = next_pos;
  30. for (uint32_t step_index = 1; step_index < step_size; ++step_index) {
  31. cur_pos = LoopIterator(pool, next(cur_pos));
  32. }
  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
Add Comment
Please, Sign In to add comment