Advertisement
Guest User

Untitled

a guest
May 27th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. template<typename... Cs>
  2. struct zipped
  3. {
  4.   zipped(Cs&... containers)
  5.     : _M_containers(containers...)
  6.   { }
  7.  
  8.   template<typename... Ts>
  9.   struct iterator_impl
  10.   {
  11.     iterator_impl(std::tuple<Ts...> its)
  12.       : _M_its(its)
  13.     { }
  14.  
  15.     std::tuple<typename Ts::reference...> operator*() {
  16.       return std::apply([](auto&... it){ return std::tuple<typename Ts::reference...> { *it... }; }, _M_its);
  17.     }
  18.  
  19.     iterator_impl& operator++() {
  20.       std::apply([](auto&... it){ (++it, ...); } , _M_its);
  21.       return *this;
  22.     }
  23.  
  24.     bool operator==(const iterator_impl& other) {
  25.       return std::apply([&](auto&... it1){
  26.         return std::apply([&](auto&... it2) {
  27.             return (true && ... && (it1 == it2));
  28.         }, other._M_its);
  29.       }, _M_its);
  30.     }
  31.  
  32.     bool operator!=(const iterator_impl& other) {
  33.       return !(*this == other);
  34.     }
  35.  
  36.   private:
  37.     std::tuple<Ts...> _M_its;
  38.   };
  39.  
  40.   using iterator = iterator_impl<typename Cs::iterator...>;
  41.  
  42.   iterator begin() {
  43.     return iterator(std::apply([](auto&... containers){ return std::tuple { containers.begin()... }; }, _M_containers)); }
  44.   iterator end() {
  45.     return iterator(std::apply([](auto&... containers){ return std::tuple { containers.end()... }; }, _M_containers)); }
  46.  
  47. private:
  48.   std::tuple<Cs&...> _M_containers;
  49. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement