VTK-m  2.0
ArrayPortalFromIterators.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_internal_ArrayPortalFromIterators_h
11 #define vtk_m_cont_internal_ArrayPortalFromIterators_h
12 
13 #include <vtkm/Assert.h>
14 #include <vtkm/Types.h>
15 #include <vtkm/cont/ArrayPortal.h>
18 
19 #include <iterator>
20 #include <limits>
21 
22 #include <type_traits>
23 
24 namespace vtkm
25 {
26 namespace cont
27 {
28 namespace internal
29 {
30 
31 template <typename IteratorT, typename Enable = void>
32 class ArrayPortalFromIterators;
33 
37 template <class IteratorT>
38 class ArrayPortalFromIterators<IteratorT,
39  typename std::enable_if<!std::is_const<
40  typename std::remove_pointer<IteratorT>::type>::value>::type>
41 {
42 public:
43  using ValueType = typename std::iterator_traits<IteratorT>::value_type;
44  using IteratorType = IteratorT;
45 
46  ArrayPortalFromIterators() = default;
47 
49  VTKM_CONT
50  ArrayPortalFromIterators(IteratorT begin, IteratorT end)
51  : BeginIterator(begin)
52  {
53  typename std::iterator_traits<IteratorT>::difference_type numberOfValues =
54  std::distance(begin, end);
55  VTKM_ASSERT(numberOfValues >= 0);
56 #ifndef VTKM_USE_64BIT_IDS
57  if (numberOfValues > (std::numeric_limits<vtkm::Id>::max)())
58  {
60  "Distance of iterators larger than maximum array size. "
61  "To support larger arrays, try turning on VTKM_USE_64BIT_IDS.");
62  }
63 #endif // !VTKM_USE_64BIT_IDS
64  this->NumberOfValues = static_cast<vtkm::Id>(numberOfValues);
65  }
66 
71  template <class OtherIteratorT>
72  VTKM_EXEC_CONT ArrayPortalFromIterators(const ArrayPortalFromIterators<OtherIteratorT>& src)
73  : BeginIterator(src.GetIteratorBegin())
74  , NumberOfValues(src.GetNumberOfValues())
75  {
76  }
77 
80  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
81 
84  ValueType Get(vtkm::Id index) const { return *this->IteratorAt(index); }
85 
88  void Set(vtkm::Id index, const ValueType& value) const { *(this->BeginIterator + index) = value; }
89 
92  IteratorT GetIteratorBegin() const { return this->BeginIterator; }
93 
96  IteratorT GetIteratorEnd() const
97  {
98  IteratorType iterator = this->BeginIterator;
99  using difference_type = typename std::iterator_traits<IteratorType>::difference_type;
100  iterator += static_cast<difference_type>(this->NumberOfValues);
101  return iterator;
102  }
103 
104 private:
105  IteratorT BeginIterator;
106  vtkm::Id NumberOfValues;
107 
110  IteratorT IteratorAt(vtkm::Id index) const
111  {
112  VTKM_ASSERT(index >= 0);
113  VTKM_ASSERT(index < this->GetNumberOfValues());
114 
115  return this->BeginIterator + index;
116  }
117 };
118 
119 template <class IteratorT>
120 class ArrayPortalFromIterators<IteratorT,
121  typename std::enable_if<std::is_const<
122  typename std::remove_pointer<IteratorT>::type>::value>::type>
123 {
124 public:
125  using ValueType = typename std::iterator_traits<IteratorT>::value_type;
126  using IteratorType = IteratorT;
127 
130  ArrayPortalFromIterators()
131  : BeginIterator(nullptr)
132  , NumberOfValues(0)
133  {
134  }
135 
137  VTKM_CONT
138  ArrayPortalFromIterators(IteratorT begin, IteratorT end)
139  : BeginIterator(begin)
140  {
141  typename std::iterator_traits<IteratorT>::difference_type numberOfValues =
142  std::distance(begin, end);
143  VTKM_ASSERT(numberOfValues >= 0);
144 #ifndef VTKM_USE_64BIT_IDS
145  if (numberOfValues > (std::numeric_limits<vtkm::Id>::max)())
146  {
148  "Distance of iterators larger than maximum array size. "
149  "To support larger arrays, try turning on VTKM_USE_64BIT_IDS.");
150  }
151 #endif // !VTKM_USE_64BIT_IDS
152  this->NumberOfValues = static_cast<vtkm::Id>(numberOfValues);
153  }
154 
160  template <class OtherIteratorT>
161  VTKM_EXEC_CONT ArrayPortalFromIterators(const ArrayPortalFromIterators<OtherIteratorT>& src)
162  : BeginIterator(src.GetIteratorBegin())
163  , NumberOfValues(src.GetNumberOfValues())
164  {
165  }
166 
169  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
170 
173  ValueType Get(vtkm::Id index) const { return *this->IteratorAt(index); }
174 
177  IteratorT GetIteratorBegin() const { return this->BeginIterator; }
178 
181  IteratorT GetIteratorEnd() const
182  {
183  using difference_type = typename std::iterator_traits<IteratorType>::difference_type;
184  IteratorType iterator = this->BeginIterator;
185  iterator += static_cast<difference_type>(this->NumberOfValues);
186  return iterator;
187  }
188 
189 private:
190  IteratorT BeginIterator;
191  vtkm::Id NumberOfValues;
192 
195  IteratorT IteratorAt(vtkm::Id index) const
196  {
197  VTKM_ASSERT(index >= 0);
198  VTKM_ASSERT(index < this->GetNumberOfValues());
199 
200  return this->BeginIterator + index;
201  }
202 };
203 }
204 }
205 } // namespace vtkm::cont::internal
206 
207 #endif //vtk_m_cont_internal_ArrayPortalFromIterators_h
ErrorBadAllocation.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
ArrayPortalToIterators.h
Assert.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ArrayPortal.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::ErrorBadAllocation
This class is thrown when VTK-m attempts to manipulate memory that it should not.
Definition: ErrorBadAllocation.h:25
VTKM_SUPPRESS_EXEC_WARNINGS
#define VTKM_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:53