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-2018 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 
26 #include "tbb_config.h"
27 #include "tbb_stddef.h"
28 
29 #if __TBB_CPP11_DECLTYPE_PRESENT
30 
31 namespace tbb {
32 
33 template <typename IntType>
35 public:
36  typedef decltype(IntType()-IntType()) difference_type;
37  typedef IntType value_type;
38  typedef const IntType* pointer;
39  typedef const IntType& reference;
40  typedef std::random_access_iterator_tag iterator_category;
41 
42  explicit counting_iterator(IntType init): my_counter(init) { __TBB_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer, "Integer required."); }
43 
44  reference operator*() const { return my_counter; }
45  value_type operator[](difference_type i) const { return *(*this + i); }
46 
48 
49  counting_iterator& operator+=(difference_type forward) { my_counter += forward; return *this; }
50  counting_iterator& operator-=(difference_type backward) { return *this += -backward; }
51  counting_iterator& operator++() { return *this += 1; }
52  counting_iterator& operator--() { return *this -= 1; }
53 
55  counting_iterator it(*this);
56  ++(*this);
57  return it;
58  }
60  counting_iterator it(*this);
61  --(*this);
62  return it;
63  }
64 
67  friend counting_iterator operator+(difference_type forward, const counting_iterator it) { return it + forward; }
68 
69  bool operator==(const counting_iterator& it) const { return *this - it == 0; }
70  bool operator!=(const counting_iterator& it) const { return !(*this == it); }
71  bool operator<(const counting_iterator& it) const {return *this - it < 0; }
72  bool operator>(const counting_iterator& it) const { return it < *this; }
73  bool operator<=(const counting_iterator& it) const { return !(*this > it); }
74  bool operator>=(const counting_iterator& it) const { return !(*this < it); }
75 
76 private:
77  IntType my_counter;
78 };
79 } //namespace tbb
80 
81 #endif //__TBB_CPP11_DECLTYPE_PRESENT
82 
83 #if __TBB_CPP11_PRESENT
84 
85 #include <type_traits>
86 #include <tuple>
87 
88 #include "internal/_template_helpers.h" // index_sequence, make_index_sequence
89 
90 namespace tbb {
91 namespace internal {
92 
93 template<size_t N>
94 struct tuple_util {
95  template<typename TupleType, typename DifferenceType>
96  static void increment(TupleType& it, DifferenceType forward) {
97  std::get<N-1>(it) += forward;
98  tuple_util<N-1>::increment(it, forward);
99  }
100  template<typename TupleType, typename DifferenceType>
101  static bool check_sync(const TupleType& it1, const TupleType& it2, DifferenceType val) {
102  if(std::get<N-1>(it1) - std::get<N-1>(it2) != val)
103  return false;
104  return tuple_util<N-1>::check_sync(it1, it2, val);
105  }
106 };
107 
108 template<>
109 struct tuple_util<0> {
110  template<typename TupleType, typename DifferenceType>
111  static void increment(TupleType&, DifferenceType) {}
112  template<typename TupleType, typename DifferenceType>
113  static bool check_sync(const TupleType&, const TupleType&, DifferenceType) { return true;}
114 };
115 
116 template <typename TupleReturnType>
117 struct make_references {
118  template <typename TupleType, std::size_t... Is>
119  TupleReturnType operator()(const TupleType& t, tbb::internal::index_sequence<Is...>) {
120  return std::tie((*std::get<Is>(t))...);
121  }
122 };
123 
124 } //namespace internal
125 
126 template <typename... Types>
127 class zip_iterator {
128  static const std::size_t num_types = sizeof...(Types);
129  typedef typename std::tuple<Types...> it_types;
130 public:
131  typedef typename std::make_signed<std::size_t>::type difference_type;
132  typedef std::tuple<typename std::iterator_traits<Types>::value_type...> value_type;
133  typedef std::tuple<typename std::iterator_traits<Types>::reference...> reference;
134  typedef std::tuple<typename std::iterator_traits<Types>::pointer...> pointer;
135  typedef std::random_access_iterator_tag iterator_category;
136 
137  explicit zip_iterator(Types... args): my_it(std::make_tuple(args...)) {}
138 
139  reference operator*() {
140  return tbb::internal::make_references<reference>()(my_it, tbb::internal::make_index_sequence<num_types>());
141  }
142  reference operator[](difference_type i) const { return *(*this + i); }
143 
144  difference_type operator-(const zip_iterator& it) const {
145  __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)),
146  "Components of zip_iterator are not synchronous");
147  return std::get<0>(my_it) - std::get<0>(it.my_it);
148  }
149 
150  zip_iterator& operator+=(difference_type forward) {
151  internal::tuple_util<num_types>::increment(my_it, forward);
152  return *this;
153  }
154  zip_iterator& operator-=(difference_type backward) { return *this += -backward; }
155  zip_iterator& operator++() { return *this += 1; }
156  zip_iterator& operator--() { return *this -= 1; }
157 
158  zip_iterator operator++(int) {
159  zip_iterator it(*this);
160  ++(*this);
161  return it;
162  }
163  zip_iterator operator--(int) {
164  zip_iterator it(*this);
165  --(*this);
166  return it;
167  }
168 
169  zip_iterator operator-(difference_type backward) const {
170  zip_iterator it(*this);
171  return it -= backward;
172  }
173  zip_iterator operator+(difference_type forward) const {
174  zip_iterator it(*this);
175  return it += forward;
176  }
177  friend zip_iterator operator+(difference_type forward, const zip_iterator& it) { return it + forward; }
178 
179  bool operator==(const zip_iterator& it) const {
180  return *this - it == 0;
181  }
182  bool operator!=(const zip_iterator& it) const { return !(*this == it); }
183  bool operator<(const zip_iterator& it) const { return *this - it < 0; }
184  bool operator>(const zip_iterator& it) const { return it < *this; }
185  bool operator<=(const zip_iterator& it) const { return !(*this > it); }
186  bool operator>=(const zip_iterator& it) const { return !(*this < it); }
187 
188 private:
189  it_types my_it;
190 };
191 
192 template<typename... T>
193 zip_iterator<T...> make_zip_iterator(T... args) { return zip_iterator<T...>(args...); }
194 
195 } //namespace tbb
196 
197 #endif //__TBB_CPP11_PRESENT
198 
199 #endif /* __TBB_iterators_H */
reference operator *() const
Definition: iterators.h:44
bool operator<(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
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 cache_aligned_allocator< T > &, const cache_aligned_allocator< U > &)
counting_iterator operator+(difference_type forward) const
Definition: iterators.h:66
value_type operator[](difference_type i) const
Definition: iterators.h:45
#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)
counting_iterator operator++(int)
Definition: iterators.h:54
counting_iterator & operator-=(difference_type backward)
Definition: iterators.h:50
counting_iterator & operator+=(difference_type forward)
Definition: iterators.h:49
bool operator>(const counting_iterator &it) const
Definition: iterators.h:72
const IntType & reference
Definition: iterators.h:39
counting_iterator(IntType init)
Definition: iterators.h:42
counting_iterator operator--(int)
Definition: iterators.h:59
bool operator==(const counting_iterator &it) const
Definition: iterators.h:69
bool operator!=(const counting_iterator &it) const
Definition: iterators.h:70
friend counting_iterator operator+(difference_type forward, const counting_iterator it)
Definition: iterators.h:67
bool operator<=(const counting_iterator &it) const
Definition: iterators.h:73
std::random_access_iterator_tag iterator_category
Definition: iterators.h:40
The graph class.
const IntType * pointer
Definition: iterators.h:38
counting_iterator operator-(difference_type backward) const
Definition: iterators.h:65
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 > &)
decltype(IntType() -IntType()) typedef difference_type
Definition: iterators.h:36
vector_iterator< Container, T > operator+(ptrdiff_t offset, const vector_iterator< Container, T > &v)
counting_iterator & operator++()
Definition: iterators.h:51
bool operator>=(const concurrent_vector< T, A1 > &a, const concurrent_vector< T, A2 > &b)
bool operator>=(const counting_iterator &it) const
Definition: iterators.h:74
tick_count::interval_t operator-(const tick_count &t1, const tick_count &t0)
Definition: tick_count.h:130
typename tbb::internal::make_index_sequence_impl< N >::type make_index_sequence
counting_iterator & operator--()
Definition: iterators.h:52
bool operator<(const counting_iterator &it) const
Definition: iterators.h:71
#define __TBB_STATIC_ASSERT(condition, msg)
Definition: tbb_stddef.h:545
difference_type operator-(const counting_iterator &it) const
Definition: iterators.h:47

Copyright © 2005-2018 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.