10 #ifndef vtk_m_cont_ArrayHandleTransform_h
11 #define vtk_m_cont_ArrayHandleTransform_h
29 struct NullFunctorType
35 template <
typename ValueType_,
37 typename FunctorType_,
38 typename InverseFunctorType_ = NullFunctorType>
41 template <
typename ValueType_,
typename PortalType_,
typename FunctorType_>
43 ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
46 using PortalType = PortalType_;
47 using ValueType = ValueType_;
48 using FunctorType = FunctorType_;
51 ArrayPortalTransform(
const PortalType& portal = PortalType(),
52 const FunctorType& functor = FunctorType())
62 template <
class OtherV,
class OtherP,
class OtherF>
63 VTKM_EXEC_CONT ArrayPortalTransform(
const ArrayPortalTransform<OtherV, OtherP, OtherF>& src)
64 : Portal(src.GetPortal())
65 , Functor(src.GetFunctor())
70 vtkm::Id GetNumberOfValues()
const {
return this->Portal.GetNumberOfValues(); }
73 ValueType
Get(
vtkm::Id index)
const {
return this->Functor(this->Portal.Get(index)); }
76 const PortalType& GetPortal()
const {
return this->Portal; }
79 const FunctorType& GetFunctor()
const {
return this->Functor; }
86 template <
typename ValueType_,
88 typename FunctorType_,
89 typename InverseFunctorType_>
91 :
public ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
93 using Writable = vtkm::internal::PortalSupportsSets<PortalType_>;
96 using Superclass = ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>;
97 using PortalType = PortalType_;
98 using ValueType = ValueType_;
99 using FunctorType = FunctorType_;
100 using InverseFunctorType = InverseFunctorType_;
103 ArrayPortalTransform(
const PortalType& portal = PortalType(),
104 const FunctorType& functor = FunctorType(),
105 const InverseFunctorType& inverseFunctor = InverseFunctorType())
106 : Superclass(portal, functor)
107 , InverseFunctor(inverseFunctor)
111 template <
class OtherV,
class OtherP,
class OtherF,
class OtherInvF>
113 const ArrayPortalTransform<OtherV, OtherP, OtherF, OtherInvF>& src)
115 , InverseFunctor(src.GetInverseFunctor())
119 template <
typename Writable_ = Writable,
120 typename =
typename std::enable_if<Writable_::value>::type>
123 this->Portal.Set(index, this->InverseFunctor(value));
127 const InverseFunctorType& GetInverseFunctor()
const {
return this->InverseFunctor; }
130 InverseFunctorType InverseFunctor;
143 using NullFunctorType = vtkm::internal::NullFunctorType;
145 template <
typename Prov
idedFunctorType,
typename FunctorIsExecContObject>
146 struct TransformFunctorManagerImpl;
148 template <
typename Prov
idedFunctorType>
149 struct TransformFunctorManagerImpl<ProvidedFunctorType, std::false_type>
152 "Must use an ExecutionAndControlObject instead of an ExecutionObject.");
154 ProvidedFunctorType Functor;
155 using FunctorType = ProvidedFunctorType;
157 TransformFunctorManagerImpl() =
default;
160 TransformFunctorManagerImpl(
const ProvidedFunctorType& functor)
166 ProvidedFunctorType PrepareForControl()
const {
return this->Functor; }
171 return this->Functor;
175 template <
typename Prov
idedFunctorType>
176 struct TransformFunctorManagerImpl<ProvidedFunctorType, std::true_type>
180 ProvidedFunctorType Functor;
183 using FunctorType = vtkm::cont::internal::ControlObjectType<ProvidedFunctorType>;
185 TransformFunctorManagerImpl() =
default;
188 TransformFunctorManagerImpl(
const ProvidedFunctorType& functor)
194 auto PrepareForControl() const
195 -> decltype(
vtkm::cont::internal::CallPrepareForControl(this->Functor))
197 return vtkm::cont::internal::CallPrepareForControl(this->Functor);
202 -> decltype(vtkm::cont::internal::CallPrepareForExecution(this->Functor, device, token))
204 return vtkm::cont::internal::CallPrepareForExecution(this->Functor, device, token);
208 template <
typename Prov
idedFunctorType>
209 struct TransformFunctorManager
210 : TransformFunctorManagerImpl<
212 typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>
214 using Superclass = TransformFunctorManagerImpl<
216 typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>;
217 using FunctorType =
typename Superclass::FunctorType;
219 VTKM_CONT TransformFunctorManager() =
default;
221 VTKM_CONT TransformFunctorManager(
const TransformFunctorManager&) =
default;
223 VTKM_CONT TransformFunctorManager(
const ProvidedFunctorType& functor)
224 : Superclass(functor)
228 template <
typename ValueType>
229 using TransformedValueType = decltype(std::declval<FunctorType>()(ValueType{}));
232 template <
typename ArrayHandleType,
233 typename FunctorType,
234 typename InverseFunctorType = NullFunctorType>
237 using FunctorManager = TransformFunctorManager<FunctorType>;
239 typename FunctorManager::template TransformedValueType<typename ArrayHandleType::ValueType>;
242 template <
typename ArrayHandleType,
typename FunctorType>
243 class Storage<typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
244 StorageTagTransform<ArrayHandleType, FunctorType>>
246 using FunctorManager = TransformFunctorManager<FunctorType>;
247 using ValueType =
typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
249 using SourceStorage =
250 Storage<typename ArrayHandleType::ValueType, typename ArrayHandleType::StorageTag>;
252 static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
253 const std::vector<vtkm::cont::internal::Buffer>& buffers)
255 return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
262 using ReadPortalType =
263 vtkm::internal::ArrayPortalTransform<ValueType,
264 typename ArrayHandleType::ReadPortalType,
265 typename FunctorManager::FunctorType>;
268 const std::vector<vtkm::cont::internal::Buffer>& buffers)
270 return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
273 VTKM_CONT static ReadPortalType CreateReadPortal(
274 const std::vector<vtkm::cont::internal::Buffer>& buffers,
278 if (device == vtkm::cont::DeviceAdapterTagUndefined{})
280 return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
281 buffers[0].GetMetaData<FunctorManager>().PrepareForControl());
285 return ReadPortalType(
286 SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
287 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token));
291 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
292 const ArrayHandleType& handle = ArrayHandleType{},
293 const FunctorType& functor = FunctorType())
295 return vtkm::cont::internal::CreateBuffers(FunctorManager(functor), handle);
298 VTKM_CONT static ArrayHandleType GetArray(
299 const std::vector<vtkm::cont::internal::Buffer>& buffers)
302 typename ArrayHandleType::StorageTag>(SourceBuffers(buffers));
305 VTKM_CONT static FunctorType GetFunctor(
const std::vector<vtkm::cont::internal::Buffer>& buffers)
307 return buffers[0].GetMetaData<FunctorManager>().Functor;
310 VTKM_CONT static NullFunctorType GetInverseFunctor(
311 const std::vector<vtkm::cont::internal::Buffer>&)
313 return NullFunctorType{};
317 template <
typename ArrayHandleType,
typename FunctorType,
typename InverseFunctorType>
319 typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType,
320 StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
322 using FunctorManager = TransformFunctorManager<FunctorType>;
323 using InverseFunctorManager = TransformFunctorManager<InverseFunctorType>;
324 using ValueType =
typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
326 using SourceStorage =
327 Storage<typename ArrayHandleType::ValueType, typename ArrayHandleType::StorageTag>;
329 static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
330 const std::vector<vtkm::cont::internal::Buffer>& buffers)
332 return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 2, buffers.end());
336 using ReadPortalType =
337 vtkm::internal::ArrayPortalTransform<ValueType,
338 typename ArrayHandleType::ReadPortalType,
339 typename FunctorManager::FunctorType,
340 typename InverseFunctorManager::FunctorType>;
341 using WritePortalType =
342 vtkm::internal::ArrayPortalTransform<ValueType,
343 typename ArrayHandleType::WritePortalType,
344 typename FunctorManager::FunctorType,
345 typename InverseFunctorManager::FunctorType>;
348 const std::vector<vtkm::cont::internal::Buffer>& buffers)
350 return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
354 const std::vector<vtkm::cont::internal::Buffer>& buffers,
358 std::vector<vtkm::cont::internal::Buffer> sourceBuffers = SourceBuffers(buffers);
359 SourceStorage::ResizeBuffers(numValues, sourceBuffers, preserve, token);
362 VTKM_CONT static ReadPortalType CreateReadPortal(
363 const std::vector<vtkm::cont::internal::Buffer>& buffers,
367 if (device == vtkm::cont::DeviceAdapterTagUndefined{})
369 return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
370 buffers[0].GetMetaData<FunctorManager>().PrepareForControl(),
371 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForControl());
375 return ReadPortalType(
376 SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
377 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
378 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
382 VTKM_CONT static WritePortalType CreateWritePortal(
383 const std::vector<vtkm::cont::internal::Buffer>& buffers,
387 return WritePortalType(
388 SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
389 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
390 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
393 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
394 const ArrayHandleType& handle = ArrayHandleType{},
395 const FunctorType& functor = FunctorType(),
396 const InverseFunctorType& inverseFunctor = InverseFunctorType())
398 return vtkm::cont::internal::CreateBuffers(
399 FunctorManager(functor), InverseFunctorManager(inverseFunctor), handle);
402 VTKM_CONT static ArrayHandleType GetArray(
403 const std::vector<vtkm::cont::internal::Buffer>& buffers)
406 typename ArrayHandleType::StorageTag>(SourceBuffers(buffers));
409 VTKM_CONT static FunctorType GetFunctor(
const std::vector<vtkm::cont::internal::Buffer>& buffers)
411 return buffers[0].GetMetaData<FunctorManager>().Functor;
414 VTKM_CONT static InverseFunctorType GetInverseFunctor(
415 const std::vector<vtkm::cont::internal::Buffer>& buffers)
417 return buffers[1].GetMetaData<InverseFunctorManager>().Functor;
434 template <
typename ArrayHandleType,
435 typename FunctorType,
436 typename InverseFunctorType = internal::NullFunctorType>
439 template <
typename ArrayHandleType,
typename FunctorType>
442 typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
443 internal::StorageTagTransform<ArrayHandleType, FunctorType>>
454 typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
455 internal::StorageTagTransform<ArrayHandleType, FunctorType>>));
458 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
463 const FunctorType& functor = FunctorType{},
464 internal::NullFunctorType = internal::NullFunctorType{})
465 : Superclass(StorageType::CreateBuffers(handle, functor))
473 template <
typename HandleType,
typename FunctorType>
483 template <
typename ArrayHandleType,
typename FunctorType,
typename InverseFunctorType>
484 class ArrayHandleTransform
486 typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
488 internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
495 (ArrayHandleTransform<ArrayHandleType, FunctorType, InverseFunctorType>),
497 typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
499 internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>));
502 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
506 const FunctorType& functor = FunctorType(),
507 const InverseFunctorType& inverseFunctor = InverseFunctorType())
508 : Superclass(
StorageType::CreateBuffers(handle, functor, inverseFunctor))
525 FunctorType
GetFunctor()
const {
return StorageType::GetFunctor(this->GetBuffers()); }
531 return StorageType::GetInverseFunctor(this->GetBuffers());
535 template <
typename HandleType,
typename FunctorType,
typename InverseFunctorType>
540 handle, functor, inverseFunctor);
554 template <
typename AH,
typename Functor,
typename InvFunctor>
555 struct SerializableTypeString<
vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
566 template <
typename AH,
typename Functor>
567 struct SerializableTypeString<
vtkm::cont::ArrayHandleTransform<AH, Functor>>
577 template <
typename AH,
typename Functor,
typename InvFunctor>
578 struct SerializableTypeString<
vtkm::cont::ArrayHandle<
579 typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
580 vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
581 : SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
590 template <
typename AH,
typename Functor>
591 struct Serialization<
vtkm::cont::ArrayHandleTransform<AH, Functor>>
598 static VTKM_CONT void save(BinaryBuffer& bb,
const BaseType& obj)
600 Type transformedArray = obj;
601 vtkmdiy::save(bb, obj.GetArray());
602 vtkmdiy::save(bb, obj.GetFunctor());
615 template <
typename AH,
typename Functor,
typename InvFunctor>
616 struct Serialization<
vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
623 static VTKM_CONT void save(BinaryBuffer& bb,
const BaseType& obj)
625 Type transformedArray = obj;
626 vtkmdiy::save(bb, transformedArray.GetTransformedArray());
627 vtkmdiy::save(bb, transformedArray.GetFunctor());
628 vtkmdiy::save(bb, transformedArray.GetInverseFunctor());
637 InvFunctor invFunctor;
643 template <
typename AH,
typename Functor,
typename InvFunctor>
644 struct Serialization<
vtkm::cont::ArrayHandle<
645 typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
646 vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
647 : Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
654 #endif //vtk_m_cont_ArrayHandleTransform_h