GrPPI  1.0
Generic and Reusable Parallel Pattern Interface
iterator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Universidad Carlos III de Madrid
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef GRPPI_COMMON_ITERATOR_H
17 #define GRPPI_COMMON_ITERATOR_H
18 
19 #include <utility>
20 #include <tuple>
21 
22 namespace grppi{
23 
24 namespace internal {
25 
26 template <typename F, typename ... Iterators, template<typename...> class T, std::size_t ... I>
27 decltype(auto) apply_deref_increment(
28  F && f,
29  T<Iterators...> & iterators,
30  std::index_sequence<I...>)
31 {
32  return std::forward<F>(f)(*std::get<I>(iterators)++...);
33 }
34 
35 } // namespace internal
36 
57 template <typename F, typename ... Iterators, template <typename ...> class T>
58 decltype(auto) apply_deref_increment(
59  F && f,
60  T<Iterators...> & iterators)
61 {
62  constexpr std::size_t size = sizeof...(Iterators);
63  return internal::apply_deref_increment(std::forward<F>(f), iterators,
64  std::make_index_sequence<size>());
65 }
66 
67 namespace internal {
68 
69 template <typename F, typename ... Iterators, template <typename...> class T,
70  std::size_t ... I>
71 decltype(auto) apply_increment(
72  F && f,
73  T<Iterators...> & iterators,
74  std::index_sequence<I...>)
75 {
76  return std::forward<F>(f)(std::get<I>(iterators)++...);
77 }
78 
79 }
80 
100 template <typename F, typename ... Iterators, template <typename...> class T>
101 decltype(auto) apply_increment(
102  F && f,
103  T<Iterators...> & iterators)
104 {
105  constexpr std::size_t size = sizeof...(Iterators);
106  return internal::apply_increment(std::forward<F>(f), iterators,
107  std::make_index_sequence<size>());
108 }
109 
110 namespace internal {
111 
112 template <typename F, typename T, std::size_t ... I>
113 decltype(auto) apply_iterators_indexed_impl(F && f, T && t, std::size_t i,
114  std::index_sequence<I...>)
115 {
116  return std::forward<F>(f)(std::get<I>(t)[i]...);
117 }
118 
119 } // namespace internal
120 
141 template <typename F, typename T>
142 decltype(auto) apply_iterators_indexed(F && f, T && t, std::size_t i)
143 {
144  using tuple_raw_type = std::decay_t<T>;
145  constexpr std::size_t size = std::tuple_size<tuple_raw_type>::value;
146  return internal::apply_iterators_indexed_impl(std::forward<F>(f),
147  std::forward<T>(t), i, std::make_index_sequence<size>());
148 }
149 
150 namespace internal {
151 
152 template <typename T, std::size_t ... I>
153 auto iterators_next_impl(T && t, int n, std::index_sequence<I...>) {
154  return make_tuple(
155  std::next(std::get<I>(t), n)...
156  );
157 }
158 
159 } // namespace internal
160 
169 template <typename T>
170 auto iterators_next(T && t, int n) {
171  using tuple_raw_type = std::decay_t<T>;
172  constexpr std::size_t size = std::tuple_size<tuple_raw_type>::value;
173  return internal::iterators_next_impl(std::forward<T>(t), n,
174  std::make_index_sequence<size>());
175 }
176 
177 
178 
179 
182 template <typename ... InputIt>
183 void advance_iterators(size_t delta, InputIt & ... in) {
184  // This hack can be done in C++14.
185  // It can be simplified in C++17 with folding expressions.
186  using type = int[];
187  type { 0, (in += delta, 0) ...};
188 }
189 
192 template <typename ... InputIt>
193 void advance_iterators(InputIt & ... in) {
194  // This hack can be done in C++14.
195  // It can be simplified in C++17 with folding expressions.
196  using type = int[];
197  type { 0, (in++, 0) ...};
198 }
199 
200 
201 } // namespace grppi
202 
203 #endif
decltype(auto) apply_deref_increment(F &&f, T< Iterators... > &iterators, std::index_sequence< I... >)
Definition: iterator.h:27
decltype(auto) apply_increment(F &&f, T< Iterators... > &iterators, std::index_sequence< I... >)
Definition: iterator.h:71
auto iterators_next_impl(T &&t, int n, std::index_sequence< I... >)
Definition: iterator.h:153
decltype(auto) apply_iterators_indexed_impl(F &&f, T &&t, std::size_t i, std::index_sequence< I... >)
Definition: iterator.h:113
Definition: callable_traits.h:21
decltype(auto) apply_increment(F &&f, T< Iterators... > &iterators)
Applies a callable object to the iterators in a tuple like-object and the increments those iterators....
Definition: iterator.h:101
auto iterators_next(T &&t, int n)
Computes next n steps from a tuple of iterators.
Definition: iterator.h:170
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:142
void advance_iterators(size_t delta, InputIt &... in)
Definition: iterator.h:183
decltype(auto) apply_deref_increment(F &&f, T< Iterators... > &iterators)
Applies a callable object to the values obtained from the iterators in a tuple-like object....
Definition: iterator.h:58