Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Enumerate.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Enumerate.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 #include <utility>
13 
14 namespace Acts {
23 template <typename container_type,
24  typename container_type_iter =
25  decltype(std::begin(std::declval<container_type>())),
26  typename = decltype(std::end(std::declval<container_type>()))>
27 constexpr auto enumerate(container_type &&iterable) {
28  struct iterator {
29  std::size_t i;
30  container_type_iter iter;
31 
32  bool operator!=(const iterator &rhs) const { return iter != rhs.iter; }
33 
35  void operator++() {
36  ++i;
37  ++iter;
38  }
39 
41  auto operator*() const { return std::tie(i, *iter); }
42  };
43  struct iterable_wrapper {
44  container_type iterable;
45  auto begin() { return iterator{0, std::begin(iterable)}; }
46  auto end() { return iterator{0, std::end(iterable)}; }
47  };
48  return iterable_wrapper{std::forward<container_type>(iterable)};
49 }
50 
51 } // namespace Acts