Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-24 08:18:54

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