VTK-m  2.0
ArrayHandleReverse.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 
11 #ifndef vtk_m_cont_ArrayHandleReverse_h
12 #define vtk_m_cont_ArrayHandleReverse_h
13 
15 #include <vtkm/cont/ArrayHandle.h>
16 #include <vtkm/cont/ErrorBadType.h>
18 
19 namespace vtkm
20 {
21 namespace cont
22 {
23 
24 namespace internal
25 {
26 
27 template <typename PortalType>
28 class VTKM_ALWAYS_EXPORT ArrayPortalReverse
29 {
30  using Writable = vtkm::internal::PortalSupportsSets<PortalType>;
31 
32 public:
33  using ValueType = typename PortalType::ValueType;
34 
36  ArrayPortalReverse()
37  : portal()
38  {
39  }
40 
42  ArrayPortalReverse(const PortalType& p)
43  : portal(p)
44  {
45  }
46 
47  template <typename OtherPortal>
48  VTKM_EXEC_CONT ArrayPortalReverse(const ArrayPortalReverse<OtherPortal>& src)
49  : portal(src.GetPortal())
50  {
51  }
52 
54  vtkm::Id GetNumberOfValues() const { return this->portal.GetNumberOfValues(); }
55 
57  ValueType Get(vtkm::Id index) const
58  {
59  return this->portal.Get(portal.GetNumberOfValues() - index - 1);
60  }
61 
62  template <typename Writable_ = Writable,
63  typename = typename std::enable_if<Writable_::value>::type>
64  VTKM_EXEC_CONT void Set(vtkm::Id index, const ValueType& value) const
65  {
66  this->portal.Set(portal.GetNumberOfValues() - index - 1, value);
67  }
68 
69 private:
70  PortalType portal;
71 };
72 }
73 
74 template <typename StorageTag>
76 {
77 };
78 
79 namespace internal
80 {
81 
82 template <typename T, typename ST>
83 class Storage<T, StorageTagReverse<ST>>
84 {
85  using SourceStorage = Storage<T, ST>;
86 
87 public:
88  using ArrayHandleType = vtkm::cont::ArrayHandle<T, ST>;
89  using ReadPortalType = ArrayPortalReverse<typename ArrayHandleType::ReadPortalType>;
90  using WritePortalType = ArrayPortalReverse<typename ArrayHandleType::WritePortalType>;
91 
92  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
93  {
94  return SourceStorage::CreateBuffers();
95  }
96 
97  VTKM_CONT static void ResizeBuffers(vtkm::Id numValues,
98  const std::vector<vtkm::cont::internal::Buffer>& buffers,
99  vtkm::CopyFlag preserve,
100  vtkm::cont::Token& token)
101  {
102  SourceStorage::ResizeBuffers(numValues, buffers, preserve, token);
103  }
104 
105  VTKM_CONT static vtkm::Id GetNumberOfValues(
106  const std::vector<vtkm::cont::internal::Buffer>& buffers)
107  {
108  return SourceStorage::GetNumberOfValues(buffers);
109  }
110 
111  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
112  const T& fillValue,
113  vtkm::Id startIndex,
114  vtkm::Id endIndex,
115  vtkm::cont::Token& token)
116  {
117  vtkm::Id numValues = GetNumberOfValues(buffers);
118  SourceStorage::Fill(buffers, fillValue, numValues - endIndex, numValues - startIndex, token);
119  }
120 
121  VTKM_CONT static ReadPortalType CreateReadPortal(
122  const std::vector<vtkm::cont::internal::Buffer>& buffers,
124  vtkm::cont::Token& token)
125  {
126  return ReadPortalType(SourceStorage::CreateReadPortal(buffers, device, token));
127  }
128 
129  VTKM_CONT static WritePortalType CreateWritePortal(
130  const std::vector<vtkm::cont::internal::Buffer>& buffers,
132  vtkm::cont::Token& token)
133  {
134  return WritePortalType(SourceStorage::CreateWritePortal(buffers, device, token));
135  }
136 }; // class storage
137 
138 } // namespace internal
139 
146 template <typename ArrayHandleType>
148  : public vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
149  StorageTagReverse<typename ArrayHandleType::StorageTag>>
150 
151 {
152 public:
156  (vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
158 
159 public:
160  ArrayHandleReverse(const ArrayHandleType& handle)
161  : Superclass(handle.GetBuffers())
162  {
163  }
164 
165  VTKM_CONT ArrayHandleType GetSourceArray() const
166  {
168  this->GetBuffers());
169  }
170 };
171 
175 template <typename HandleType>
177 {
178  return ArrayHandleReverse<HandleType>(handle);
179 }
180 
181 namespace internal
182 {
183 
184 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
185 // the sub-storage is inefficient (thus making everything inefficient).
186 template <typename StorageTag>
187 struct ArrayExtractComponentImpl<vtkm::cont::StorageTagReverse<StorageTag>>
188  : vtkm::cont::internal::ArrayExtractComponentImpl<StorageTag>
189 {
190  template <typename T>
191  using StrideArrayType =
193 
194  template <typename T>
195  StrideArrayType<T> operator()(
197  vtkm::IdComponent componentIndex,
198  vtkm::CopyFlag allowCopy) const
199  {
201  StrideArrayType<T> subArray =
202  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
203  // Reverse the array by starting at the end and striding backward
204  return StrideArrayType<T>(subArray.GetBasicArray(),
205  srcArray.GetNumberOfValues(),
206  -subArray.GetStride(),
207  subArray.GetOffset() +
208  (subArray.GetStride() * (subArray.GetNumberOfValues() - 1)),
209  subArray.GetModulo(),
210  subArray.GetDivisor());
211  }
212 };
213 
214 } // namespace internal
215 
216 }
217 } // namespace vtkm::cont
218 
219 //=============================================================================
220 // Specializations of serialization related classes
222 namespace vtkm
223 {
224 namespace cont
225 {
226 
227 template <typename AH>
228 struct SerializableTypeString<vtkm::cont::ArrayHandleReverse<AH>>
229 {
230  static VTKM_CONT const std::string& Get()
231  {
232  static std::string name = "AH_Reverse<" + SerializableTypeString<AH>::Get() + ">";
233  return name;
234  }
235 };
236 
237 template <typename T, typename ST>
238 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagReverse<ST>>>
239  : SerializableTypeString<vtkm::cont::ArrayHandleReverse<vtkm::cont::ArrayHandle<T, ST>>>
240 {
241 };
242 }
243 } // vtkm::cont
244 
245 namespace mangled_diy_namespace
246 {
247 
248 template <typename AH>
249 struct Serialization<vtkm::cont::ArrayHandleReverse<AH>>
250 {
251 private:
254 
255 public:
256  static VTKM_CONT void save(BinaryBuffer& bb, const Type& obj)
257  {
258  vtkmdiy::save(bb, obj.GetSourceArray());
259  }
260 
261  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
262  {
263  AH array;
264  vtkmdiy::load(bb, array);
266  }
267 };
268 
269 template <typename T, typename ST>
270 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagReverse<ST>>>
271  : Serialization<vtkm::cont::ArrayHandleReverse<vtkm::cont::ArrayHandle<T, ST>>>
272 {
273 };
274 
275 } // diy
277 
278 #endif // vtk_m_cont_ArrayHandleReverse_h
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagReverse< ArrayHandleType::StorageTag > >::GetBuffers
const VTKM_CONT std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:696
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
ArrayExtractComponent.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
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
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::ArrayHandleReverse::ArrayHandleReverse
ArrayHandleReverse(const ArrayHandleType &handle)
Definition: ArrayHandleReverse.h:160
vtkm::cont::ArrayHandleReverse::GetSourceArray
VTKM_CONT ArrayHandleType GetSourceArray() const
Definition: ArrayHandleReverse.h:165
mangled_diy_namespace
Definition: Particle.h:331
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::arg::load
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm::cont::ArrayHandleStride
An ArrayHandle that accesses a basic array with strides and offsets.
Definition: ArrayHandleStride.h:251
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::StorageTagReverse
Definition: ArrayHandleReverse.h:75
vtkm::cont::ArrayHandleReverse::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleReverse,(ArrayHandleReverse< ArrayHandleType >),(vtkm::cont::ArrayHandle< typename ArrayHandleType::ValueType, StorageTagReverse< typename ArrayHandleType::StorageTag >>))
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
ErrorBadValue.h
vtkm::CopyFlag
CopyFlag
Definition: Flags.h:16
vtkm::cont::ArrayHandleReverse
Reverse the order of an array, on demand.
Definition: ArrayHandleReverse.h:147
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
ErrorBadType.h
vtkm::cont::make_ArrayHandleReverse
VTKM_CONT ArrayHandleReverse< HandleType > make_ArrayHandleReverse(const HandleType &handle)
make_ArrayHandleReverse is convenience function to generate an ArrayHandleReverse.
Definition: ArrayHandleReverse.h:176