Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Zip.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Zip.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <tuple>
12 
13 namespace Acts {
14 
24 template <typename... R>
25 auto zip(R &&...r) {
26  struct It {
27  std::tuple<decltype(r.begin())...> iterators;
28  static_assert(std::tuple_size_v<decltype(iterators)> > 0);
29 
30  using reference = std::tuple<decltype(*r.begin())...>;
31 
32  auto operator++() {
33  std::apply([](auto &...args) { (++args, ...); }, iterators);
34  return *this;
35  }
36 
37  auto operator!=(const It &other) const {
38  return std::get<0>(iterators) != std::get<0>(other.iterators);
39  }
40 
41  reference operator*() {
42  return std::apply([](auto &...args) { return reference{*args...}; },
43  iterators);
44  }
45  };
46 
47  struct Zip {
48  It b, e;
49 
50  auto begin() { return b; }
51  auto end() { return e; }
52  };
53 
54  auto begin = std::make_tuple(r.begin()...);
55  auto end = std::make_tuple(r.end()...);
56  return Zip{It{begin}, It{end}};
57 }
58 
59 } // namespace Acts