Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:10

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 #include <utility>
0013 
0014 namespace Acts {
0015 /// Helper utility to allow indexed enumeration with structured binding
0016 ///
0017 /// Usage:
0018 ///
0019 /// for (auto [ i, value ] = enumerate(container) ) { ... };
0020 ///
0021 /// with 'container' any stl-like container
0022 ///
0023 template <typename container_type,
0024           typename container_type_iter =
0025               decltype(std::begin(std::declval<container_type>())),
0026           typename = decltype(std::end(std::declval<container_type>()))>
0027 constexpr auto enumerate(container_type &&iterable) {
0028   struct iterator {
0029     std::size_t i;
0030     container_type_iter iter;
0031 
0032     bool operator!=(const iterator &rhs) const { return iter != rhs.iter; }
0033 
0034     /** Increase index and iterator at once */
0035     void operator++() {
0036       ++i;
0037       ++iter;
0038     }
0039 
0040     /** Tie them together for returning */
0041     auto operator*() const { return std::tie(i, *iter); }
0042   };
0043   struct iterable_wrapper {
0044     container_type iterable;
0045     auto begin() { return iterator{0, std::begin(iterable)}; }
0046     auto end() { return iterator{0, std::end(iterable)}; }
0047   };
0048   return iterable_wrapper{std::forward<container_type>(iterable)};
0049 }
0050 
0051 }  // namespace Acts