LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
get_elements.h
Go to the documentation of this file.
1 
10 #ifndef LARCOREALG_COREUTILS_GET_ELEMENTS_H
11 #define LARCOREALG_COREUTILS_GET_ELEMENTS_H
12 
13 // LArSoft libraries
14 #include "larcorealg/CoreUtils/StdUtils.h" // util::get()
15 #include "larcorealg/CoreUtils/span.h" // util::make_transformed_span()
16 
17 // C/C++ libraries
18 #include <cstddef> // std::size_t
19 #include <type_traits>
20 #include <utility> // std::get()
21 
22 namespace util {
23 
24  // -- BEGIN -- Transformed iterations ----------------------------------------
27 
67  template <std::size_t... Indices, typename Coll>
68  decltype(auto) get_elements(Coll&& coll);
69 
78  template <std::size_t... Indices, typename Coll>
79  decltype(auto) get_const_elements(Coll&& coll);
80 
82  // -- END -- Transformed iterations ------------------------------------------
83 
84 } // namespace util
85 
86 //==============================================================================
87 //=== template implementation
88 //==============================================================================
89 //------------------------------------------------------------------------------
90 //--- util::values()
91 //------------------------------------------------------------------------------
92 namespace util::details {
93 
94  //----------------------------------------------------------------------------
95  template <typename Coll, std::size_t First, std::size_t... Others>
97 
98  template <typename T>
99  static constexpr decltype(auto) iterate(T&& coll) noexcept
100  {
101  auto extractor = [](auto&& item) -> decltype(auto) {
102  if constexpr (sizeof...(Others) == 0U) { return util::get<First>(item); }
103  else {
104  return std::forward_as_tuple(util::get<First>(item), util::get<Others...>(item));
105  }
106  };
107  return util::make_transformed_span(coll, extractor);
108  }
109 
110  }; // struct get_elements_impl
111 
112  //----------------------------------------------------------------------------
113 
114 } // namespace util::details
115 
116 //------------------------------------------------------------------------------
117 template <std::size_t... Indices, typename Coll>
118 decltype(auto) util::get_elements(Coll&& coll)
119 {
120  static_assert(sizeof...(Indices) >= 1U, "At least one index must be specified");
121  return details::get_elements_impl<std::decay_t<Coll>, Indices...>::iterate(
122  std::forward<Coll>(coll));
123 } // util::get_elements()
124 
125 //------------------------------------------------------------------------------
126 template <std::size_t... Indices, typename Coll>
127 decltype(auto) util::get_const_elements(Coll&& coll)
128 {
129  return get_elements<Indices...>(std::as_const(coll));
130 }
131 
132 //------------------------------------------------------------------------------
133 
134 #endif // LARCOREALG_COREUTILS_GET_ELEMENTS_H
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
An object with a begin and end iterator.
STL namespace.
decltype(auto) get_elements(Coll &&coll)
Range-for loop helper iterating across some of the element of each value in the specified collection...
decltype(auto) get_const_elements(Coll &&coll)
Range-for loop helper iterating across the constant values of the specified collection.
auto make_transformed_span(BIter begin, EIter end, Op &&op)
Definition: span.h:294
Functions pulling in STL customization if available.