LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
AssnsAlgorithms.h
Go to the documentation of this file.
1 
17 #ifndef canvas_Persistency_Common_AssnsAlgorithms_h
18 #define canvas_Persistency_Common_AssnsAlgorithms_h
19 
21 
22 // range library
23 #include "range/v3/algorithm/for_each.hpp"
24 #include "range/v3/view/all.hpp"
25 #include "range/v3/view/chunk_by.hpp"
26 #include "range/v3/view/map.hpp"
27 #include "range/v3/view/transform.hpp"
28 
29 // C/C++ standard libraries
30 #include <iterator> // std::next()
31 
32 namespace art {
109  template <typename A, typename B, typename D, typename F>
110  void
111  for_each_group(art::Assns<A, B, D> const& assns, F func)
112  {
113  ::ranges::for_each(assns | ::ranges::views::all |
114  ::ranges::views::chunk_by([](auto a1, auto a2) {
115  return a1.first == a2.first;
116  }) |
117  ::ranges::views::transform([](auto pairs) {
118  return pairs | ::ranges::views::values;
119  }),
120  func);
121  }
122 
123  /*
124  * @brief Helper functions to access associations in order
125  * @tparam A type of association being read
126  * @tparam F type of functor to be called on each LHS and associated RHS
127  * @param assns the association being read
128  * @param func functor to be called on each LHS and associated RHS
129  *
130  * This function performs the following:
131  * -- takes an association collection as the first argument
132  * -- calls the provided callable object
133  * For example, given an art::Assns<L, R>, it will transform this
134  * collection to a form that supports calling a function with two arguments
135  * (auto const & left, auto rights)
136  * The provided callable is invoked for each unique left.
137  *
138  */
139  template <typename A, typename B, typename D, typename F>
140  void
142  {
143  for_each_pair(assns, [&func](auto rng) {
144  auto rights = rng | ::ranges::views::values;
145  auto lefts = rng | ::ranges::views::keys;
146  auto const& left = **::ranges::begin(lefts);
147  func(left, rights);
148  });
149  }
150 
151  template <typename A, typename B, typename D, typename F>
152  void
153  for_each_pair(art::Assns<A, B, D> const& assns, F func)
154  {
155  ::ranges::for_each(assns | ::ranges::views::all |
156  ::ranges::views::chunk_by([](auto a1, auto a2) {
157  return a1.first == a2.first;
158  }),
159  func);
160  }
161 
162 } // namespace art
163 
164 #endif /* canvas_Persistency_Common_AssnsAlgorithms_h */
165 
166 // Local Variables:
167 // mode: c++
168 // End:
#define a2
void for_each_group_with_left(art::Assns< A, B, D > const &assns, F func)
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
void for_each_group(art::Assns< A, B, D > const &assns, F func)
Helper functions to access associations in order.
void for_each_pair(art::Assns< A, B, D > const &assns, F func)
Definition: MVAAlg.h:12
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
#define a1