Viskores  1.0
cont/internal/IteratorFromArrayPortal.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_internal_IteratorFromArrayPortal_h
19 #define viskores_cont_internal_IteratorFromArrayPortal_h
20 
21 #include <viskores/Assert.h>
24 
25 namespace viskores
26 {
27 namespace cont
28 {
29 namespace internal
30 {
31 
32 template <class ArrayPortalType>
33 class IteratorFromArrayPortal
34 {
35 public:
36  using value_type = typename std::remove_const<typename ArrayPortalType::ValueType>::type;
37  using reference = viskores::internal::ArrayPortalValueReference<ArrayPortalType>;
38  using pointer = typename std::add_pointer<value_type>::type;
39 
40  using difference_type = std::ptrdiff_t;
41 
42  using iterator_category = std::random_access_iterator_tag;
43 
44  using iter = IteratorFromArrayPortal<ArrayPortalType>;
45 
48  IteratorFromArrayPortal()
49  : Portal()
50  , Index(0)
51  {
52  }
53 
56  explicit IteratorFromArrayPortal(const ArrayPortalType& portal, viskores::Id index = 0)
57  : Portal(portal)
58  , Index(index)
59  {
60  VISKORES_ASSERT(index >= 0);
61  VISKORES_ASSERT(index <= portal.GetNumberOfValues());
62  }
63 
65  reference operator*() const { return reference(this->Portal, this->Index); }
66 
68  reference operator->() const { return reference(this->Portal, this->Index); }
69 
71  reference operator[](difference_type idx) const
72  {
73  return reference(this->Portal, this->Index + static_cast<viskores::Id>(idx));
74  }
75 
77  iter& operator++()
78  {
79  this->Index++;
80  VISKORES_ASSERT(this->Index <= this->Portal.GetNumberOfValues());
81  return *this;
82  }
83 
85  iter operator++(int) { return iter(this->Portal, this->Index++); }
86 
88  iter& operator--()
89  {
90  this->Index--;
91  VISKORES_ASSERT(this->Index >= 0);
92  return *this;
93  }
94 
96  iter operator--(int) { return iter(this->Portal, this->Index--); }
97 
99  iter& operator+=(difference_type n)
100  {
101  this->Index += static_cast<viskores::Id>(n);
102  VISKORES_ASSERT(this->Index <= this->Portal.GetNumberOfValues());
103  return *this;
104  }
105 
107  iter& operator-=(difference_type n)
108  {
109  this->Index -= static_cast<viskores::Id>(n);
110  VISKORES_ASSERT(this->Index >= 0);
111  return *this;
112  }
113 
115  iter operator-(difference_type n) const
116  {
117  return iter(this->Portal, this->Index - static_cast<viskores::Id>(n));
118  }
119 
120  ArrayPortalType Portal;
122 };
123 
124 template <class ArrayPortalType>
125 VISKORES_EXEC_CONT IteratorFromArrayPortal<ArrayPortalType> make_IteratorBegin(
126  const ArrayPortalType& portal)
127 {
128  return IteratorFromArrayPortal<ArrayPortalType>(portal, 0);
129 }
130 
131 template <class ArrayPortalType>
132 VISKORES_EXEC_CONT IteratorFromArrayPortal<ArrayPortalType> make_IteratorEnd(
133  const ArrayPortalType& portal)
134 {
135  return IteratorFromArrayPortal<ArrayPortalType>(portal, portal.GetNumberOfValues());
136 }
137 
138 template <typename PortalType>
140  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
141  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
142 {
143  return lhs.Index == rhs.Index;
144 }
145 
146 template <typename PortalType>
148  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
149  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
150 {
151  return lhs.Index != rhs.Index;
152 }
153 
154 template <typename PortalType>
155 VISKORES_EXEC_CONT bool operator<(
156  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
157  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
158 {
159  return lhs.Index < rhs.Index;
160 }
161 
162 template <typename PortalType>
163 VISKORES_EXEC_CONT bool operator<=(
164  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
165  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
166 {
167  return lhs.Index <= rhs.Index;
168 }
169 
170 template <typename PortalType>
171 VISKORES_EXEC_CONT bool operator>(
172  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
173  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
174 {
175  return lhs.Index > rhs.Index;
176 }
177 
178 template <typename PortalType>
179 VISKORES_EXEC_CONT bool operator>=(
180  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
181  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
182 {
183  return lhs.Index >= rhs.Index;
184 }
185 
186 template <typename PortalType>
187 VISKORES_EXEC_CONT std::ptrdiff_t operator-(
188  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& lhs,
189  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& rhs)
190 {
191  return lhs.Index - rhs.Index;
192 }
193 
194 template <typename PortalType>
195 VISKORES_EXEC_CONT viskores::cont::internal::IteratorFromArrayPortal<PortalType> operator+(
196  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& iter,
197  std::ptrdiff_t n)
198 {
199  return viskores::cont::internal::IteratorFromArrayPortal<PortalType>(
200  iter.Portal, iter.Index + static_cast<viskores::Id>(n));
201 }
202 
203 template <typename PortalType>
204 VISKORES_EXEC_CONT viskores::cont::internal::IteratorFromArrayPortal<PortalType> operator+(
205  std::ptrdiff_t n,
206  viskores::cont::internal::IteratorFromArrayPortal<PortalType> const& iter)
207 {
208  return viskores::cont::internal::IteratorFromArrayPortal<PortalType>(
209  iter.Portal, iter.Index + static_cast<viskores::Id>(n));
210 }
211 }
212 }
213 } // namespace viskores::cont::internal
214 
215 #endif //viskores_cont_internal_IteratorFromArrayPortal_h
viskores::cont::operator==
bool operator==(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:182
VISKORES_SUPPRESS_EXEC_WARNINGS
#define VISKORES_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:61
viskores::operator+
VISKORES_EXEC_CONT viskores::Vec< T, Size > operator+(viskores::Vec< T, Size > a, const viskores::Vec< T, Size > &b)
Definition: VecOperators.h:100
Assert.h
viskores::operator*
VISKORES_EXEC_CONT viskores::Vec< T, Size > operator*(viskores::Vec< T, Size > a, const viskores::Vec< T, Size > &b)
Definition: VecOperators.h:193
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::cont::operator!=
bool operator!=(const viskores::cont::Token &token, viskores::cont::Token::Reference ref)
Definition: Token.h:187
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
ArrayPortal.h
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
ArrayPortalValueReference.h
Index
int Index
Definition: ChooseCudaDevice.h:95
viskores::operator-
VISKORES_EXEC_CONT std::enable_if<(std::is_floating_point< T >::value||std::is_signed< T >::value), viskores::Vec< T, Size > >::type operator-(viskores::Vec< T, Size > x)
Definition: VecOperators.h:49
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51