LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ForEachAssociatedGroup.h
Go to the documentation of this file.
1 
15 #ifndef LARDATA_UTILITIES_FOR_EACH_ASSOCIATED_GROUP_H
16 #define LARDATA_UTILITIES_FOR_EACH_ASSOCIATED_GROUP_H
17 
18 // LArSoft libraries
20 
21 // framework libraries
22 #include "canvas/Persistency/Common/AssnsAlgorithms.h" // art::for_each_group()
23 
24 // range library
25 #include "range/v3/algorithm/for_each.hpp"
26 #include "range/v3/view/group_by.hpp"
27 #include "range/v3/view/transform.hpp"
28 #include "range/v3/view/map.hpp" // range::view::values
29 #include "range/v3/view/all.hpp"
30 
31 // C/C++ standard libraries
32 #include <utility> // std::make_pair()
33 #include <iterator> // std::next()
34 
35 
36 namespace util {
47  template <class A, class F>
48  [[deprecated("Use art::for_each_group() instead")]]
49  void for_each_associated_group(A const & assns, F & func)
50  { art::for_each_group(assns, func); }
51 
52 
93  template <class A>
94  auto associated_groups(A const & assns) {
95  return assns |
96  ranges::view::all |
97  ranges::view::group_by([](auto a1, auto a2) { return a1.first == a2.first;}) |
98  ranges::view::transform([] (auto pairs) {return pairs | ranges::view::values | util::range_for;}) |
100  ;
101  } // associated_groups()
102 
103 
151  template <class A>
152  auto associated_groups_with_left(A const & assns) {
153  return assns
154  | ranges::view::all
155  | ranges::view::group_by([](auto a1, auto a2) { return a1.first == a2.first;})
156  | ranges::view::transform([] (auto pairs)
157  {
158  return std::make_pair(
159  pairs.front().first, // assuming they're all the same, pick first
160  pairs | ranges::view::values | util::range_for
161  );
162  })
164  ;
165  } // associated_groups_with_left()
166 
167 
179  template <typename Groups>
180  auto groupByIndex(Groups&& groups, std::size_t index) -> decltype(auto)
181  { return *(std::next(groups.begin(), index)); }
182 
183 } // namespace util
184 
185 #endif // LARDATA_UTILITIES_FOR_EACH_ASSOCIATED_GROUP_H
auto associated_groups_with_left(A const &assns)
Helper functions to access associations in order, also with key.
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
void for_each_associated_group(A const &assns, F &func)
Helper functions to access associations in order.
Utility function to enable range-for on different type iterators.
auto groupByIndex(Groups &&groups, std::size_t index) -> decltype(auto)
Returns the group within groups with the specified index.
#define a2
auto associated_groups(A const &assns)
Helper functions to access associations in order.
constexpr RangeForWrapperTag range_for
void for_each_group(art::Assns< A, B, D > const &assns, F func)
Helper functions to access associations in order.
#define a1