LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
values.h
Go to the documentation of this file.
1 
10 #ifndef LARCOREALG_COREUTILS_VALUES_H
11 #define LARCOREALG_COREUTILS_VALUES_H
12 
13 // LArSoft libraries
15 #include "larcorealg/CoreUtils/span.h" // util::make_transformed_span()
16 
17 // C/C++ libraries
18 #include <cstddef> // std::size_t
19 #include <map>
20 #include <tuple> // std::get()
21 #include <type_traits>
22 #include <unordered_map>
23 #include <utility> // std::forward(), std::as_const()
24 
25 namespace util {
26 
27  // -- BEGIN -- Transformed iterations ----------------------------------------
30 
57  template <typename Coll>
58  decltype(auto) values(Coll&& coll);
59 
68  template <typename Coll>
69  decltype(auto) const_values(Coll&& coll);
70 
72  // -- END -- Transformed iterations ------------------------------------------
73 
74 } // namespace util
75 
76 //==============================================================================
77 //=== template implementation
78 //==============================================================================
79 //------------------------------------------------------------------------------
80 //--- util::values()
81 //------------------------------------------------------------------------------
82 namespace util::details {
83 
84  //----------------------------------------------------------------------------
85  template <typename Coll, typename = void>
86  struct values_impl {
87 
88  template <typename T>
89  static constexpr decltype(auto) iterate(T&& coll) noexcept
90  {
91  return coll;
92  }
93 
94  }; // struct values_impl
95 
96  //----------------------------------------------------------------------------
97  template <typename Map, std::size_t NElement = 1U>
98  struct map_values_impl {
99 
100  template <typename T>
101  static constexpr decltype(auto) iterate(T&& coll) noexcept
102  {
103  return util::get_elements<NElement>(std::forward<T>(coll));
104  }
105 
106  }; // map_values_impl
107 
108  //----------------------------------------------------------------------------
109  template <typename Key, typename Value, typename... Args>
110  struct values_impl<std::map<Key, Value, Args...>>
111  : map_values_impl<std::map<Key, Value, Args...>> {};
112  template <typename Key, typename Value, typename... Args>
113  struct values_impl<std::unordered_map<Key, Value, Args...>>
114  : map_values_impl<std::unordered_map<Key, Value, Args...>> {};
115 
116  //----------------------------------------------------------------------------
117 
118 } // namespace util::details
119 
120 //------------------------------------------------------------------------------
121 template <typename Coll>
122 decltype(auto) util::values(Coll&& coll)
123 {
124  return details::values_impl<std::decay_t<Coll>>::iterate(std::forward<Coll>(coll));
125 } // util::values()
126 
127 //------------------------------------------------------------------------------
128 template <typename Coll>
129 decltype(auto) util::const_values(Coll&& coll)
130 {
131  return values(std::as_const(coll));
132 }
133 
134 //------------------------------------------------------------------------------
135 
136 #endif // LARCOREALG_COREUTILS_VALUES_H
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
An object with a begin and end iterator.
Definition of util::get_elements() and util::get_const_elements().
decltype(auto) const_values(Coll &&coll)
Range-for loop helper iterating across the constant values of the specified collection.
STL namespace.
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.