GrPPI  0.3.1
Generic and Reusable Parallel Pattern Interface
iterator.h
Go to the documentation of this file.
1 
21 #ifndef GRPPI_COMMON_ITERATOR_H
22 #define GRPPI_COMMON_ITERATOR_H
23 
24 #include <utility>
25 #include <tuple>
26 
27 namespace grppi{
28 
29 namespace internal {
30 
31 template <typename F, typename ... Iterators, template<typename...> class T, std::size_t ... I>
32 decltype(auto) apply_deref_increment(
33  F && f,
34  T<Iterators...> & iterators,
35  std::index_sequence<I...>)
36 {
37  return std::forward<F>(f)(*std::get<I>(iterators)++...);
38 }
39 
40 } // namespace internal
41 
62 template <typename F, typename ... Iterators, template <typename ...> class T>
63 decltype(auto) apply_deref_increment(
64  F && f,
65  T<Iterators...> & iterators)
66 {
67  constexpr std::size_t size = sizeof...(Iterators);
68  return internal::apply_deref_increment(std::forward<F>(f), iterators,
69  std::make_index_sequence<size>());
70 }
71 
72 namespace internal {
73 
74 template <typename F, typename ... Iterators, template <typename...> class T,
75  std::size_t ... I>
76 decltype(auto) apply_increment(
77  F && f,
78  T<Iterators...> & iterators,
79  std::index_sequence<I...>)
80 {
81  return std::forward<F>(f)(std::get<I>(iterators)++...);
82 }
83 
84 }
85 
105 template <typename F, typename ... Iterators, template <typename...> class T>
106 decltype(auto) apply_increment(
107  F && f,
108  T<Iterators...> & iterators)
109 {
110  constexpr std::size_t size = sizeof...(Iterators);
111  return internal::apply_increment(std::forward<F>(f), iterators,
112  std::make_index_sequence<size>());
113 }
114 
115 namespace internal {
116 
117 template <typename F, typename T, std::size_t ... I>
118 decltype(auto) apply_iterators_indexed_impl(F && f, T && t, std::size_t i,
119  std::index_sequence<I...>)
120 {
121  return std::forward<F>(f)(std::get<I>(t)[i]...);
122 }
123 
124 } // namespace internal
125 
146 template <typename F, typename T>
147 decltype(auto) apply_iterators_indexed(F && f, T && t, std::size_t i)
148 {
149  using tuple_raw_type = std::decay_t<T>;
150  constexpr std::size_t size = std::tuple_size<tuple_raw_type>::value;
151  return internal::apply_iterators_indexed_impl(std::forward<F>(f),
152  std::forward<T>(t), i, std::make_index_sequence<size>());
153 }
154 
155 namespace internal {
156 
157 template <typename T, std::size_t ... I>
158 auto iterators_next_impl(T && t, int n, std::index_sequence<I...>) {
159  return make_tuple(
160  std::next(std::get<I>(t), n)...
161  );
162 }
163 
164 } // namespace internal
165 
174 template <typename T>
175 auto iterators_next(T && t, int n) {
176  using tuple_raw_type = std::decay_t<T>;
177  constexpr std::size_t size = std::tuple_size<tuple_raw_type>::value;
178  return internal::iterators_next_impl(std::forward<T>(t), n,
179  std::make_index_sequence<size>());
180 }
181 
182 
183 
184 
187 template <typename ... InputIt>
188 void advance_iterators(size_t delta, InputIt & ... in) {
189  // This hack can be done in C++14.
190  // It can be simplified in C++17 with folding expressions.
191  using type = int[];
192  type { 0, (in += delta, 0) ...};
193 }
194 
197 template <typename ... InputIt>
198 void advance_iterators(InputIt & ... in) {
199  // This hack can be done in C++14.
200  // It can be simplified in C++17 with folding expressions.
201  using type = int[];
202  type { 0, (in++, 0) ...};
203 }
204 
205 
206 } // namespace grppi
207 
208 #endif
Definition: callable_traits.h:26
STL namespace.
auto iterators_next(T &&t, int n)
Computes next n steps from a tuple of iterators.
Definition: iterator.h:175
decltype(auto) apply_iterators_indexed(F &&f, T &&t, std::size_t i)
Applies a callable object to the values obtained from the iterators in a tuple by indexing...
Definition: iterator.h:147
decltype(auto) apply_increment(F &&f, T< Iterators... > &iterators, std::index_sequence< I... >)
Definition: iterator.h:76
decltype(auto) apply_deref_increment(F &&f, T< Iterators... > &iterators, std::index_sequence< I... >)
Definition: iterator.h:32
void advance_iterators(size_t delta, InputIt &...in)
Definition: iterator.h:188
decltype(auto) apply_iterators_indexed_impl(F &&f, T &&t, std::size_t i, std::index_sequence< I... >)
Definition: iterator.h:118
auto iterators_next_impl(T &&t, int n, std::index_sequence< I... >)
Definition: iterator.h:158