VTK-m  2.2
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::IdComponent GetNumberOfComponentsFlat(
106  const std::vector<vtkm::cont::internal::Buffer>& buffers)
107  {
108  return SourceStorage::GetNumberOfComponentsFlat(buffers);
109  }
110 
111  VTKM_CONT static vtkm::Id GetNumberOfValues(
112  const std::vector<vtkm::cont::internal::Buffer>& buffers)
113  {
114  return SourceStorage::GetNumberOfValues(buffers);
115  }
116 
117  VTKM_CONT static void Fill(const std::vector<vtkm::cont::internal::Buffer>& buffers,
118  const T& fillValue,
119  vtkm::Id startIndex,
120  vtkm::Id endIndex,
121  vtkm::cont::Token& token)
122  {
123  vtkm::Id numValues = GetNumberOfValues(buffers);
124  SourceStorage::Fill(buffers, fillValue, numValues - endIndex, numValues - startIndex, token);
125  }
126 
127  VTKM_CONT static ReadPortalType CreateReadPortal(
128  const std::vector<vtkm::cont::internal::Buffer>& buffers,
130  vtkm::cont::Token& token)
131  {
132  return ReadPortalType(SourceStorage::CreateReadPortal(buffers, device, token));
133  }
134 
135  VTKM_CONT static WritePortalType CreateWritePortal(
136  const std::vector<vtkm::cont::internal::Buffer>& buffers,
138  vtkm::cont::Token& token)
139  {
140  return WritePortalType(SourceStorage::CreateWritePortal(buffers, device, token));
141  }
142 }; // class storage
143 
144 } // namespace internal
145 
152 template <typename ArrayHandleType>
154  : public vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
155  StorageTagReverse<typename ArrayHandleType::StorageTag>>
156 
157 {
158 public:
162  (vtkm::cont::ArrayHandle<typename ArrayHandleType::ValueType,
164 
165  ArrayHandleReverse(const ArrayHandleType& handle)
166  : Superclass(handle.GetBuffers())
167  {
168  }
169 
170  VTKM_CONT ArrayHandleType GetSourceArray() const
171  {
173  this->GetBuffers());
174  }
175 };
176 
180 template <typename HandleType>
182 {
183  return ArrayHandleReverse<HandleType>(handle);
184 }
185 
186 namespace internal
187 {
188 
189 // Superclass will inherit the ArrayExtractComponentImplInefficient property if
190 // the sub-storage is inefficient (thus making everything inefficient).
191 template <typename StorageTag>
192 struct ArrayExtractComponentImpl<vtkm::cont::StorageTagReverse<StorageTag>>
193  : vtkm::cont::internal::ArrayExtractComponentImpl<StorageTag>
194 {
195  template <typename T>
196  using StrideArrayType =
198 
199  template <typename T>
200  StrideArrayType<T> operator()(
202  vtkm::IdComponent componentIndex,
203  vtkm::CopyFlag allowCopy) const
204  {
206  StrideArrayType<T> subArray =
207  ArrayExtractComponentImpl<StorageTag>{}(srcArray.GetSourceArray(), componentIndex, allowCopy);
208  // Reverse the array by starting at the end and striding backward
209  return StrideArrayType<T>(subArray.GetBasicArray(),
210  srcArray.GetNumberOfValues(),
211  -subArray.GetStride(),
212  subArray.GetOffset() +
213  (subArray.GetStride() * (subArray.GetNumberOfValues() - 1)),
214  subArray.GetModulo(),
215  subArray.GetDivisor());
216  }
217 };
218 
219 } // namespace internal
220 
221 }
222 } // namespace vtkm::cont
223 
224 //=============================================================================
225 // Specializations of serialization related classes
227 namespace vtkm
228 {
229 namespace cont
230 {
231 
232 template <typename AH>
233 struct SerializableTypeString<vtkm::cont::ArrayHandleReverse<AH>>
234 {
235  static VTKM_CONT const std::string& Get()
236  {
237  static std::string name = "AH_Reverse<" + SerializableTypeString<AH>::Get() + ">";
238  return name;
239  }
240 };
241 
242 template <typename T, typename ST>
243 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagReverse<ST>>>
244  : SerializableTypeString<vtkm::cont::ArrayHandleReverse<vtkm::cont::ArrayHandle<T, ST>>>
245 {
246 };
247 }
248 } // vtkm::cont
249 
250 namespace mangled_diy_namespace
251 {
252 
253 template <typename AH>
254 struct Serialization<vtkm::cont::ArrayHandleReverse<AH>>
255 {
256 private:
259 
260 public:
261  static VTKM_CONT void save(BinaryBuffer& bb, const Type& obj)
262  {
263  vtkmdiy::save(bb, obj.GetSourceArray());
264  }
265 
266  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
267  {
268  AH array;
269  vtkmdiy::load(bb, array);
271  }
272 };
273 
274 template <typename T, typename ST>
275 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagReverse<ST>>>
276  : Serialization<vtkm::cont::ArrayHandleReverse<vtkm::cont::ArrayHandle<T, ST>>>
277 {
278 };
279 
280 } // diy
282 
283 #endif // vtk_m_cont_ArrayHandleReverse_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
ArrayExtractComponent.h
vtkm::cont::ArrayHandle< ArrayHandleType::ValueType, StorageTagReverse< ArrayHandleType::StorageTag > >::GetBuffers
const std::vector< vtkm::cont::internal::Buffer > & GetBuffers() const
Returns the internal Buffer structures that hold the data.
Definition: ArrayHandle.h:721
vtkm::exec::arg::load
T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::ArrayHandleReverse::ArrayHandleReverse
ArrayHandleReverse(const ArrayHandleType &handle)
Definition: ArrayHandleReverse.h:165
vtkm::cont::make_ArrayHandleReverse
ArrayHandleReverse< HandleType > make_ArrayHandleReverse(const HandleType &handle)
make_ArrayHandleReverse is convenience function to generate an ArrayHandleReverse.
Definition: ArrayHandleReverse.h:181
vtkm::cont::ArrayHandleReverse::GetSourceArray
ArrayHandleType GetSourceArray() const
Definition: ArrayHandleReverse.h:170
mangled_diy_namespace
Definition: Particle.h:351
vtkm::cont::ArrayHandleStride
An ArrayHandle that accesses a basic array with strides and offsets.
Definition: ArrayHandleStride.h:332
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
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
vtkm::cont::ArrayHandleReverse::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< typename ArrayHandleType::ValueType, StorageTagReverse< typename ArrayHandleType::StorageTag > >) >::type Superclass
Definition: ArrayHandleReverse.h:163
ErrorBadValue.h
vtkm::CopyFlag
CopyFlag
Identifier used to specify whether a function should deep copy data.
Definition: Flags.h:17
vtkm::cont::ArrayHandleReverse
Reverse the order of an array, on demand.
Definition: ArrayHandleReverse.h:153
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
ErrorBadType.h