Viskores  1.0
ArrayPortalFromIterators.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_ArrayPortalFromIterators_h
19 #define viskores_cont_internal_ArrayPortalFromIterators_h
20 
21 #include <viskores/Assert.h>
22 #include <viskores/Types.h>
26 
27 #include <iterator>
28 #include <limits>
29 
30 #include <type_traits>
31 
32 namespace viskores
33 {
34 namespace cont
35 {
36 namespace internal
37 {
38 
39 template <typename IteratorT, typename Enable = void>
40 class ArrayPortalFromIterators;
41 
45 template <class IteratorT>
46 class ArrayPortalFromIterators<IteratorT,
47  typename std::enable_if<!std::is_const<
48  typename std::remove_pointer<IteratorT>::type>::value>::type>
49 {
50 public:
51  using ValueType = typename std::iterator_traits<IteratorT>::value_type;
52  using IteratorType = IteratorT;
53 
54  ArrayPortalFromIterators() = default;
55 
58  ArrayPortalFromIterators(IteratorT begin, IteratorT end)
59  : BeginIterator(begin)
60  {
61  typename std::iterator_traits<IteratorT>::difference_type numberOfValues =
62  std::distance(begin, end);
63  VISKORES_ASSERT(numberOfValues >= 0);
64 #ifndef VISKORES_USE_64BIT_IDS
65  if (numberOfValues > (std::numeric_limits<viskores::Id>::max)())
66  {
68  "Distance of iterators larger than maximum array size. "
69  "To support larger arrays, try turning on VISKORES_USE_64BIT_IDS.");
70  }
71 #endif // !VISKORES_USE_64BIT_IDS
72  this->NumberOfValues = static_cast<viskores::Id>(numberOfValues);
73  }
74 
79  template <class OtherIteratorT>
80  VISKORES_EXEC_CONT ArrayPortalFromIterators(const ArrayPortalFromIterators<OtherIteratorT>& src)
81  : BeginIterator(src.GetIteratorBegin())
82  , NumberOfValues(src.GetNumberOfValues())
83  {
84  }
85 
88  viskores::Id GetNumberOfValues() const { return this->NumberOfValues; }
89 
92  ValueType Get(viskores::Id index) const { return *this->IteratorAt(index); }
93 
96  void Set(viskores::Id index, const ValueType& value) const
97  {
98  *(this->BeginIterator + index) = value;
99  }
100 
103  IteratorT GetIteratorBegin() const { return this->BeginIterator; }
104 
107  IteratorT GetIteratorEnd() const
108  {
109  IteratorType iterator = this->BeginIterator;
110  using difference_type = typename std::iterator_traits<IteratorType>::difference_type;
111  iterator += static_cast<difference_type>(this->NumberOfValues);
112  return iterator;
113  }
114 
115 private:
116  IteratorT BeginIterator;
117  viskores::Id NumberOfValues;
118 
121  IteratorT IteratorAt(viskores::Id index) const
122  {
123  VISKORES_ASSERT(index >= 0);
124  VISKORES_ASSERT(index < this->GetNumberOfValues());
125 
126  return this->BeginIterator + index;
127  }
128 };
129 
130 template <class IteratorT>
131 class ArrayPortalFromIterators<IteratorT,
132  typename std::enable_if<std::is_const<
133  typename std::remove_pointer<IteratorT>::type>::value>::type>
134 {
135 public:
136  using ValueType = typename std::iterator_traits<IteratorT>::value_type;
137  using IteratorType = IteratorT;
138 
141  ArrayPortalFromIterators()
142  : BeginIterator(nullptr)
143  , NumberOfValues(0)
144  {
145  }
146 
149  ArrayPortalFromIterators(IteratorT begin, IteratorT end)
150  : BeginIterator(begin)
151  {
152  typename std::iterator_traits<IteratorT>::difference_type numberOfValues =
153  std::distance(begin, end);
154  VISKORES_ASSERT(numberOfValues >= 0);
155 #ifndef VISKORES_USE_64BIT_IDS
156  if (numberOfValues > (std::numeric_limits<viskores::Id>::max)())
157  {
159  "Distance of iterators larger than maximum array size. "
160  "To support larger arrays, try turning on VISKORES_USE_64BIT_IDS.");
161  }
162 #endif // !VISKORES_USE_64BIT_IDS
163  this->NumberOfValues = static_cast<viskores::Id>(numberOfValues);
164  }
165 
171  template <class OtherIteratorT>
172  VISKORES_EXEC_CONT ArrayPortalFromIterators(const ArrayPortalFromIterators<OtherIteratorT>& src)
173  : BeginIterator(src.GetIteratorBegin())
174  , NumberOfValues(src.GetNumberOfValues())
175  {
176  }
177 
180  viskores::Id GetNumberOfValues() const { return this->NumberOfValues; }
181 
184  ValueType Get(viskores::Id index) const { return *this->IteratorAt(index); }
185 
188  IteratorT GetIteratorBegin() const { return this->BeginIterator; }
189 
192  IteratorT GetIteratorEnd() const
193  {
194  using difference_type = typename std::iterator_traits<IteratorType>::difference_type;
195  IteratorType iterator = this->BeginIterator;
196  iterator += static_cast<difference_type>(this->NumberOfValues);
197  return iterator;
198  }
199 
200 private:
201  IteratorT BeginIterator;
202  viskores::Id NumberOfValues;
203 
206  IteratorT IteratorAt(viskores::Id index) const
207  {
208  VISKORES_ASSERT(index >= 0);
209  VISKORES_ASSERT(index < this->GetNumberOfValues());
210 
211  return this->BeginIterator + index;
212  }
213 };
214 }
215 }
216 } // namespace viskores::cont::internal
217 
218 #endif //viskores_cont_internal_ArrayPortalFromIterators_h
ErrorBadAllocation.h
viskores::cont::ErrorBadAllocation
This class is thrown when Viskores attempts to manipulate memory that it should not.
Definition: ErrorBadAllocation.h:33
Types.h
ArrayPortalToIterators.h
VISKORES_SUPPRESS_EXEC_WARNINGS
#define VISKORES_SUPPRESS_EXEC_WARNINGS
Definition: ExportMacros.h:61
Assert.h
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
ArrayPortal.h
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::Get
auto Get(const viskores::Tuple< Ts... > &tuple)
Retrieve the object from a viskores::Tuple at the given index.
Definition: Tuple.h:89