LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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/group_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::view::all |
114  ranges::view::group_by([](auto a1, auto a2) {
115  return a1.first == a2.first;
116  }) |
117  ranges::view::transform([](auto pairs) {
118  return pairs | ranges::view::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::view::values;
145  auto lefts = rng | ranges::view::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::view::all |
156  ranges::view::group_by(
157  [](auto a1, auto a2) { return a1.first == a2.first; }),
158  func);
159  }
160 
161 } // namespace art
162 
163 #endif /* canvas_Persistency_Common_AssnsAlgorithms_h */
164 
165 // Local Variables:
166 // mode: c++
167 // End:
#define a2
void for_each_group_with_left(art::Assns< A, B, D > const &assns, F func)
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
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)
HLT enums.
#define a1