Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-22 07:52:01

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <tuple>
0012 
0013 namespace Acts {
0014 
0015 /// Function that allows to zip some ranges to be used in a range-based for
0016 /// loop. When wanting to mutate the entries, the result must be captured by
0017 /// value:
0018 ///
0019 /// for(auto [a, b, c] : zip(ra, rb, rc)) { a+=2; }
0020 ///
0021 /// @tparam R The ranges type pack
0022 /// @param r The ranges parameter pack
0023 /// @note the behaviour is undefined if the ranges do not have equal range
0024 ///
0025 /// @return Zip object providing iteration over multiple ranges simultaneously
0026 template <typename... R>
0027 auto zip(R &&...r) {
0028   struct It {
0029     std::tuple<decltype(r.begin())...> iterators;
0030     static_assert(std::tuple_size_v<decltype(iterators)> > 0);
0031 
0032     using reference = std::tuple<decltype(*r.begin())...>;
0033 
0034     auto operator++() {
0035       std::apply([](auto &...args) { (++args, ...); }, iterators);
0036       return *this;
0037     }
0038 
0039     auto operator!=(const It &other) const {
0040       return std::get<0>(iterators) != std::get<0>(other.iterators);
0041     }
0042 
0043     reference operator*() {
0044       return std::apply([](auto &...args) { return reference{*args...}; },
0045                         iterators);
0046     }
0047   };
0048 
0049   struct Zip {
0050     It b, e;
0051 
0052     auto begin() { return b; }
0053     auto end() { return e; }
0054   };
0055 
0056   auto begin = std::make_tuple(r.begin()...);
0057   auto end = std::make_tuple(r.end()...);
0058   return Zip{It{begin}, It{end}};
0059 }
0060 
0061 }  // namespace Acts