Advertisement
Ilya_konstantinov

chto-to_ochen_plooe

Sep 24th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | Source Code | 0 0
  1. #pragma once
  2. #include <cstdint>
  3. #include <utility>
  4. #include <type_traits>
  5. #include <iterator>
  6.  
  7. template <class Iterator>
  8. class IteratorRange {
  9. public:
  10.     IteratorRange(Iterator begin, Iterator end) : begin_(begin), end_(end) {
  11.     }
  12.  
  13.     Iterator begin() const {  // NOLINT
  14.         return begin_;
  15.     }
  16.  
  17.     Iterator end() const {  // NOLINT
  18.         return end_;
  19.     }
  20.  
  21. private:
  22.     Iterator begin_, end_;
  23. };
  24.  
  25. template <class T>
  26. class Range {
  27. public:
  28.     struct Iterator {
  29.         Iterator(T value, T step = 1) : value_(value), step_(step) {
  30.         }
  31.  
  32.         Iterator& operator++() {
  33.             value_ += step_;
  34.             return *this;
  35.         }
  36.  
  37.         bool operator!=(const Iterator& other) {
  38.             return value_ < other.value_;
  39.         }
  40.  
  41.         T& operator*() {
  42.             return value_;
  43.         }
  44.  
  45.         T value_, step_;
  46.     };
  47.     Range(const T& begin, const T& end, T k = 1)
  48.         : its(Iterator(begin, k), Iterator(end, k)) {
  49.     }
  50.     Range(T end) : its(Iterator(0), Iterator(end)) {
  51.     }
  52.  
  53.     Iterator begin() const {
  54.         return its.begin();
  55.     }
  56.  
  57.     Iterator end() const {
  58.         return its.end();
  59.     }
  60.  
  61.     Iterator cbegin() const {
  62.         return its.begin();
  63.     }
  64.  
  65.     Iterator cend() const {
  66.         return its.end();
  67.     }
  68.  
  69. private:
  70.     const IteratorRange<Iterator> its;
  71. };
  72.  
  73. template <typename T1, typename T2>
  74. class Zip {
  75. public:
  76.     using IteratorFirst = decltype(std::declval<T1>().cbegin());
  77.     using IteratorSecond = decltype(std::declval<T2>().cbegin());
  78.  
  79.     struct Iterator {
  80.         using ObjectFirst = decltype(*std::declval<IteratorFirst>());
  81.         using ObjectSecond = decltype(*std::declval<IteratorSecond>());
  82.  
  83.         Iterator(IteratorFirst it1, IteratorSecond it2) : it1_(it1), it2_(it2) {}
  84.  
  85.         Iterator& operator++() {
  86.             ++it1_;
  87.             ++it2_;
  88.             return *this;
  89.         }
  90.  
  91.         bool operator!=(const Iterator& other) const {
  92.             return it1_ != other.it1_ && it2_ != other.it2_;
  93.         }
  94.  
  95.         std::pair<const ObjectFirst&, const ObjectSecond&> operator*() {
  96.             return std::pair<const ObjectFirst&, const ObjectSecond&>(*it1_, *it2_);
  97.         }
  98.  
  99.         IteratorFirst it1_;
  100.         IteratorSecond it2_;
  101.     };
  102.  
  103.     Zip(T1&& ft,T2&& sc) : its(
  104.         Iterator(std::cbegin(std::forward<T1>(ft))), std::cbegin(std::forward<T2>(sc))),
  105.         Iterator(std::cend(std::forward<T1>(ft)), std::cend(std::forward<T2>(sc)))
  106.          {}
  107.  
  108.     Iterator begin() const {
  109.         return its.begin();
  110.     }
  111.  
  112.     Iterator end() const {
  113.         return its.end();
  114.     }
  115.  
  116. private:
  117.     const IteratorRange<Iterator> its;
  118. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement