LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ForEachAssociatedGroup.h
Go to the documentation of this file.
1 
14 #ifndef LARDATA_UTILITIES_FOR_EACH_ASSOCIATED_GROUP_H
15 #define LARDATA_UTILITIES_FOR_EACH_ASSOCIATED_GROUP_H
16 
17 // LArSoft libraries
19 
20 // framework libraries
21 #include "canvas/Persistency/Common/AssnsAlgorithms.h" // art::for_each_group()
22 
23 // range library
24 #include "range/v3/algorithm/for_each.hpp"
25 #include "range/v3/view/all.hpp"
26 #include "range/v3/view/group_by.hpp"
27 #include "range/v3/view/map.hpp" // range::views::values
28 #include "range/v3/view/transform.hpp"
29 
30 // C/C++ standard libraries
31 #include <iterator> // std::next()
32 #include <utility> // std::make_pair()
33 
34 namespace util {
45  template <class A, class F>
46  [[deprecated("Use art::for_each_group() instead")]] void for_each_associated_group(A const& assns,
47  F& func)
48  {
49  art::for_each_group(assns, func);
50  }
51 
92  template <class A>
93  auto associated_groups(A const& assns)
94  {
95  return assns | ranges::views::all |
96  ranges::views::chunk_by([](auto a1, auto a2) { return a1.first == a2.first; }) |
97  ranges::views::transform(
98  [](auto pairs) { return pairs | ranges::views::values | util::range_for; }) |
100  } // associated_groups()
101 
149  template <class A>
150  auto associated_groups_with_left(A const& assns)
151  {
152  return assns | ranges::views::all |
153  ranges::views::chunk_by([](auto a1, auto a2) { return a1.first == a2.first; }) |
154  ranges::views::transform([](auto pairs) {
155  return std::make_pair(pairs.front().first, // assuming they're all the same, pick first
157  }) |
159  } // associated_groups_with_left()
160 
172  template <typename Groups>
173  auto groupByIndex(Groups&& groups, std::size_t index) -> decltype(auto)
174  {
175  return *(std::next(groups.begin(), index));
176  }
177 
178 } // namespace util
179 
180 #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:26
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.
#define a2
auto associated_groups(A const &assns)
Helper functions to access associations in order.
constexpr RangeForWrapperTag range_for
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
auto groupByIndex(Groups &&groups, std::size_t index) -> decltype(auto)
Returns the group within groups with the specified index.
void for_each_group(art::Assns< A, B, D > const &assns, F func)
Helper functions to access associations in order.
#define a1