Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
iterators.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2017-2019 Intel Corporation
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 
17 
18 
19 */
20 
21 #ifndef __TBB_iterators_H
22 #define __TBB_iterators_H
23 
24 #include <iterator>
25 #include <limits>
26 
27 #include "tbb_config.h"
28 #include "tbb_stddef.h"
29 
30 #if __TBB_CPP11_PRESENT
31 
32 #include <type_traits>
33 
34 namespace tbb {
35 
36 template <typename IntType>
37 class counting_iterator {
38  __TBB_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer, "Cannot instantiate counting_iterator with a non-integer type");
39 public:
40  typedef typename std::make_signed<IntType>::type difference_type;
41  typedef IntType value_type;
42  typedef const IntType* pointer;
43  typedef const IntType& reference;
44  typedef std::random_access_iterator_tag iterator_category;
45 
46  counting_iterator(): my_counter() {}
47  explicit counting_iterator(IntType init): my_counter(init) {}
48 
49  reference operator*() const { return my_counter; }
50  value_type operator[](difference_type i) const { return *(*this + i); }
51 
52  difference_type operator-(const counting_iterator& it) const { return my_counter - it.my_counter; }
53 
54  counting_iterator& operator+=(difference_type forward) { my_counter += forward; return *this; }
55  counting_iterator& operator-=(difference_type backward) { return *this += -backward; }
56  counting_iterator& operator++() { return *this += 1; }
57  counting_iterator& operator--() { return *this -= 1; }
58 
59  counting_iterator operator++(int) {
60  counting_iterator it(*this);
61  ++(*this);
62  return it;
63  }
64  counting_iterator operator--(int) {
65  counting_iterator it(*this);
66  --(*this);
67  return it;
68  }
69 
70  counting_iterator operator-(difference_type backward) const { return counting_iterator(my_counter - backward); }
71  counting_iterator operator+(difference_type forward) const { return counting_iterator(my_counter + forward); }
72  friend counting_iterator operator+(difference_type forward, const counting_iterator it) { return it + forward; }
73 
74  bool operator==(const counting_iterator& it) const { return *this - it == 0; }
75  bool operator!=(const counting_iterator& it) const { return !(*this == it); }
76  bool operator<(const counting_iterator& it) const {return *this - it < 0; }
77  bool operator>(const counting_iterator& it) const { return it < *this; }
78  bool operator<=(const counting_iterator& it) const { return !(*this > it); }
79  bool operator>=(const counting_iterator& it) const { return !(*this < it); }
80 
81 private:
82  IntType my_counter;
83 };
84 } //namespace tbb
85 
86 
87 #include <tuple>
88 
89 #include "internal/_template_helpers.h" // index_sequence, make_index_sequence
90 
91 namespace tbb {
92 namespace internal {
93 
94 template<size_t N>
95 struct tuple_util {
96  template<typename TupleType, typename DifferenceType>
97  static void increment(TupleType& it, DifferenceType forward) {
98  std::get<N-1>(it) += forward;
99  tuple_util<N-1>::increment(it, forward);
100  }
101  template<typename TupleType, typename DifferenceType>
102  static bool check_sync(const TupleType& it1, const TupleType& it2, DifferenceType val) {
103  if(std::get<N-1>(it1) - std::get<N-1>(it2) != val)
104  return false;
105  return tuple_util<N-1>::check_sync(it1, it2, val);
106  }
107 };
108 
109 template<>
110 struct tuple_util<0> {
111  template<typename TupleType, typename DifferenceType>
112  static void increment(TupleType&, DifferenceType) {}
113  template<typename TupleType, typename DifferenceType>
114  static bool check_sync(const TupleType&, const TupleType&, DifferenceType) { return true;}
115 };
116 
117 template <typename TupleReturnType>
118 struct make_references {
119  template <typename TupleType, std::size_t... Is>
120  TupleReturnType operator()(const TupleType& t, tbb::internal::index_sequence<Is...>) {
121  return std::tie((*std::get<Is>(t))...);
122  }
123 };
124 
125 } //namespace internal
126 
127 template <typename... Types>
128 class zip_iterator {
129  __TBB_STATIC_ASSERT(sizeof...(Types), "Cannot instantiate zip_iterator with empty template parameter pack");
130  static const std::size_t num_types = sizeof...(Types);
131  typedef typename std::tuple<Types...> it_types;
132 public:
133  typedef typename std::make_signed<std::size_t>::type difference_type;
134  typedef std::tuple<typename std::iterator_traits<Types>::value_type...> value_type;
135  typedef std::tuple<typename std::iterator_traits<Types>::reference...> reference;
136  typedef std::tuple<typename std::iterator_traits<Types>::pointer...> pointer;
137  typedef std::random_access_iterator_tag iterator_category;
138 
139  zip_iterator(): my_it() {}
140  explicit zip_iterator(Types... args): my_it(std::make_tuple(args...)) {}
141  zip_iterator(const zip_iterator& input) : my_it(input.my_it) {}
142  zip_iterator& operator=(const zip_iterator& input) {
143  my_it = input.my_it;
144  return *this;
145  }
146 
147  reference operator*() const {
148  return tbb::internal::make_references<reference>()(my_it, tbb::internal::make_index_sequence<num_types>());
149  }
150  reference operator[](difference_type i) const { return *(*this + i); }
151 
152  difference_type operator-(const zip_iterator& it) const {
153  __TBB_ASSERT(internal::tuple_util<num_types>::check_sync(my_it, it.my_it, std::get<0>(my_it) - std::get<0>(it.my_it)),
154  "Components of zip_iterator are not synchronous");
155  return std::get<0>(my_it) - std::get<0>(it.my_it);
156  }
157 
158  zip_iterator& operator+=(difference_type forward) {
159  internal::tuple_util<num_types>::increment(my_it, forward);
160  return *this;
161  }
162  zip_iterator& operator-=(difference_type backward) { return *this += -backward; }
163  zip_iterator& operator++() { return *this += 1; }
164  zip_iterator& operator--() { return *this -= 1; }
165 
166  zip_iterator operator++(int) {
167  zip_iterator it(*this);
168  ++(*this);
169  return it;
170  }
171  zip_iterator operator--(int) {
172  zip_iterator it(*this);
173  --(*this);
174  return it;
175  }
176 
177  zip_iterator operator-(difference_type backward) const {
178  zip_iterator it(*this);
179  return it -= backward;
180  }
181  zip_iterator operator+(difference_type forward) const {
182  zip_iterator it(*this);
183  return it += forward;
184  }
185  friend zip_iterator operator+(difference_type forward, const zip_iterator& it) { return it + forward; }
186 
187  bool operator==(const zip_iterator& it) const {
188  return *this - it == 0;
189  }
190  bool operator!=(const zip_iterator& it) const { return !(*this == it); }
191  bool operator<(const zip_iterator& it) const { return *this - it < 0; }
192  bool operator>(const zip_iterator& it) const { return it < *this; }
193  bool operator<=(const zip_iterator& it) const { return !(*this > it); }
194  bool operator>=(const zip_iterator& it) const { return !(*this < it); }
195 
196 private:
197  it_types my_it;
198 };
199 
200 template<typename... T>
201 zip_iterator<T...> make_zip_iterator(T... args) { return zip_iterator<T...>(args...); }
202 
203 template <typename UnaryFunc, typename Iter>
204 class transform_iterator {
205 public:
206  typedef typename std::iterator_traits<Iter>::value_type value_type;
207  typedef typename std::iterator_traits<Iter>::difference_type difference_type;
208 #if __TBB_CPP17_INVOKE_RESULT_PRESENT
209  typedef typename std::invoke_result<UnaryFunc, typename std::iterator_traits<Iter>::reference>::type reference;
210 #else
211  typedef typename std::result_of<UnaryFunc(typename std::iterator_traits<Iter>::reference)>::type reference;
212 #endif
213  typedef typename std::iterator_traits<Iter>::pointer pointer;
214  typedef typename std::random_access_iterator_tag iterator_category;
215 
216  transform_iterator(Iter it, UnaryFunc unary_func): my_it(it), my_unary_func(unary_func) {
217  __TBB_STATIC_ASSERT((std::is_same<typename std::iterator_traits<Iter>::iterator_category,
218  std::random_access_iterator_tag>::value), "Random access iterator required.");
219  }
220  transform_iterator(const transform_iterator& input) : my_it(input.my_it), my_unary_func(input.my_unary_func) { }
221  transform_iterator& operator=(const transform_iterator& input) {
222  my_it = input.my_it;
223  return *this;
224  }
225  reference operator*() const {
226  return my_unary_func(*my_it);
227  }
228  reference operator[](difference_type i) const {
229  return *(*this + i);
230  }
231  transform_iterator& operator++() {
232  ++my_it;
233  return *this;
234  }
235  transform_iterator& operator--() {
236  --my_it;
237  return *this;
238  }
239  transform_iterator operator++(int) {
240  transform_iterator it(*this);
241  ++(*this);
242  return it;
243  }
244  transform_iterator operator--(int) {
245  transform_iterator it(*this);
246  --(*this);
247  return it;
248  }
249  transform_iterator operator+(difference_type forward) const {
250  return { my_it + forward, my_unary_func };
251  }
252  transform_iterator operator-(difference_type backward) const {
253  return { my_it - backward, my_unary_func };
254  }
255  transform_iterator& operator+=(difference_type forward) {
256  my_it += forward;
257  return *this;
258  }
259  transform_iterator& operator-=(difference_type backward) {
260  my_it -= backward;
261  return *this;
262  }
263  friend transform_iterator operator+(difference_type forward, const transform_iterator& it) {
264  return it + forward;
265  }
266  difference_type operator-(const transform_iterator& it) const {
267  return my_it - it.my_it;
268  }
269  bool operator==(const transform_iterator& it) const { return *this - it == 0; }
270  bool operator!=(const transform_iterator& it) const { return !(*this == it); }
271  bool operator<(const transform_iterator& it) const { return *this - it < 0; }
272  bool operator>(const transform_iterator& it) const { return it < *this; }
273  bool operator<=(const transform_iterator& it) const { return !(*this > it); }
274  bool operator>=(const transform_iterator& it) const { return !(*this < it); }
275 
276  Iter base() const { return my_it; }
277 private:
278  Iter my_it;
279  const UnaryFunc my_unary_func;
280 };
281 
282 template<typename UnaryFunc, typename Iter>
283 transform_iterator<UnaryFunc, Iter> make_transform_iterator(Iter it, UnaryFunc unary_func) {
284  return transform_iterator<UnaryFunc, Iter>(it, unary_func);
285 }
286 
287 } //namespace tbb
288 
289 #endif //__TBB_CPP11_PRESENT
290 
291 #endif /* __TBB_iterators_H */
bool operator<=(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
bool operator==(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
bool operator>=(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:536
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
bool operator<(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
tick_count::interval_t operator-(const tick_count &t1, const tick_count &t0)
Definition: tick_count.h:130
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type type
bool operator>(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
vector_iterator< Container, T > operator+(ptrdiff_t offset, const vector_iterator< Container, T > &v)
The graph class.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
bool operator!=(const cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
STL namespace.
typename tbb::internal::make_index_sequence_impl< N >::type make_index_sequence

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.