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>()(std::declval<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 =
typename ArrayHandleType::StorageType;
251 static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
252 const std::vector<vtkm::cont::internal::Buffer>& buffers)
254 return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 1, buffers.end());
261 using ReadPortalType =
262 vtkm::internal::ArrayPortalTransform<ValueType,
263 typename ArrayHandleType::ReadPortalType,
264 typename FunctorManager::FunctorType>;
267 const std::vector<vtkm::cont::internal::Buffer>&)
273 const std::vector<vtkm::cont::internal::Buffer>& buffers)
275 return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
278 VTKM_CONT static ReadPortalType CreateReadPortal(
279 const std::vector<vtkm::cont::internal::Buffer>& buffers,
285 return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
286 buffers[0].GetMetaData<FunctorManager>().PrepareForControl());
290 return ReadPortalType(
291 SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
292 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token));
296 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
297 const ArrayHandleType& handle = ArrayHandleType{},
298 const FunctorType& functor = FunctorType())
300 return vtkm::cont::internal::CreateBuffers(FunctorManager(functor), handle);
303 VTKM_CONT static ArrayHandleType GetArray(
304 const std::vector<vtkm::cont::internal::Buffer>& buffers)
307 typename ArrayHandleType::StorageTag>(SourceBuffers(buffers));
310 VTKM_CONT static FunctorType GetFunctor(
const std::vector<vtkm::cont::internal::Buffer>& buffers)
312 return buffers[0].GetMetaData<FunctorManager>().Functor;
315 VTKM_CONT static NullFunctorType GetInverseFunctor(
316 const std::vector<vtkm::cont::internal::Buffer>&)
318 return NullFunctorType{};
322 template <
typename ArrayHandleType,
typename FunctorType,
typename InverseFunctorType>
324 typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType,
325 StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
327 using FunctorManager = TransformFunctorManager<FunctorType>;
328 using InverseFunctorManager = TransformFunctorManager<InverseFunctorType>;
329 using ValueType =
typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
331 using SourceStorage =
typename ArrayHandleType::StorageType;
333 static std::vector<vtkm::cont::internal::Buffer> SourceBuffers(
334 const std::vector<vtkm::cont::internal::Buffer>& buffers)
336 return std::vector<vtkm::cont::internal::Buffer>(buffers.begin() + 2, buffers.end());
340 using ReadPortalType =
341 vtkm::internal::ArrayPortalTransform<ValueType,
342 typename ArrayHandleType::ReadPortalType,
343 typename FunctorManager::FunctorType,
344 typename InverseFunctorManager::FunctorType>;
345 using WritePortalType =
346 vtkm::internal::ArrayPortalTransform<ValueType,
347 typename ArrayHandleType::WritePortalType,
348 typename FunctorManager::FunctorType,
349 typename InverseFunctorManager::FunctorType>;
352 const std::vector<vtkm::cont::internal::Buffer>&)
358 const std::vector<vtkm::cont::internal::Buffer>& buffers)
360 return SourceStorage::GetNumberOfValues(SourceBuffers(buffers));
364 const std::vector<vtkm::cont::internal::Buffer>& buffers,
368 std::vector<vtkm::cont::internal::Buffer> sourceBuffers = SourceBuffers(buffers);
369 SourceStorage::ResizeBuffers(numValues, sourceBuffers, preserve, token);
372 VTKM_CONT static ReadPortalType CreateReadPortal(
373 const std::vector<vtkm::cont::internal::Buffer>& buffers,
379 return ReadPortalType(SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
380 buffers[0].GetMetaData<FunctorManager>().PrepareForControl(),
381 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForControl());
385 return ReadPortalType(
386 SourceStorage::CreateReadPortal(SourceBuffers(buffers), device, token),
387 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
388 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
392 VTKM_CONT static WritePortalType CreateWritePortal(
393 const std::vector<vtkm::cont::internal::Buffer>& buffers,
397 return WritePortalType(
398 SourceStorage::CreateWritePortal(SourceBuffers(buffers), device, token),
399 buffers[0].GetMetaData<FunctorManager>().PrepareForExecution(device, token),
400 buffers[1].GetMetaData<InverseFunctorManager>().PrepareForExecution(device, token));
403 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers(
404 const ArrayHandleType& handle = ArrayHandleType{},
405 const FunctorType& functor = FunctorType(),
406 const InverseFunctorType& inverseFunctor = InverseFunctorType())
408 return vtkm::cont::internal::CreateBuffers(
409 FunctorManager(functor), InverseFunctorManager(inverseFunctor), handle);
412 VTKM_CONT static ArrayHandleType GetArray(
413 const std::vector<vtkm::cont::internal::Buffer>& buffers)
416 typename ArrayHandleType::StorageTag>(SourceBuffers(buffers));
419 VTKM_CONT static FunctorType GetFunctor(
const std::vector<vtkm::cont::internal::Buffer>& buffers)
421 return buffers[0].GetMetaData<FunctorManager>().Functor;
424 VTKM_CONT static InverseFunctorType GetInverseFunctor(
425 const std::vector<vtkm::cont::internal::Buffer>& buffers)
427 return buffers[1].GetMetaData<InverseFunctorManager>().Functor;
444 template <
typename ArrayHandleType,
445 typename FunctorType,
446 typename InverseFunctorType = internal::NullFunctorType>
449 template <
typename ArrayHandleType,
typename FunctorType>
452 typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
453 internal::StorageTagTransform<ArrayHandleType, FunctorType>>
464 typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
465 internal::StorageTagTransform<ArrayHandleType, FunctorType>>));
469 const FunctorType& functor = FunctorType{},
470 internal::NullFunctorType = internal::NullFunctorType{})
471 :
Superclass(StorageType::CreateBuffers(handle, functor))
479 template <
typename HandleType,
typename FunctorType>
489 template <
typename ArrayHandleType,
typename FunctorType,
typename InverseFunctorType>
490 class ArrayHandleTransform
492 typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
494 internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
501 (ArrayHandleTransform<ArrayHandleType, FunctorType, InverseFunctorType>),
503 typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
505 internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>));
508 const FunctorType& functor = FunctorType(),
509 const InverseFunctorType& inverseFunctor = InverseFunctorType())
527 FunctorType
GetFunctor()
const {
return StorageType::GetFunctor(this->GetBuffers()); }
533 return StorageType::GetInverseFunctor(this->GetBuffers());
537 template <
typename HandleType,
typename FunctorType,
typename InverseFunctorType>
542 handle, functor, inverseFunctor);
556 template <
typename AH,
typename Functor,
typename InvFunctor>
557 struct SerializableTypeString<
vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
568 template <
typename AH,
typename Functor>
569 struct SerializableTypeString<
vtkm::cont::ArrayHandleTransform<AH, Functor>>
579 template <
typename AH,
typename Functor,
typename InvFunctor>
580 struct SerializableTypeString<
vtkm::cont::ArrayHandle<
581 typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
582 vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
583 : SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
592 template <
typename AH,
typename Functor>
593 struct Serialization<
vtkm::cont::ArrayHandleTransform<AH, Functor>>
600 static VTKM_CONT void save(BinaryBuffer& bb,
const BaseType& obj)
602 Type transformedArray = obj;
603 vtkmdiy::save(bb, obj.GetArray());
604 vtkmdiy::save(bb, obj.GetFunctor());
617 template <
typename AH,
typename Functor,
typename InvFunctor>
618 struct Serialization<
vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
625 static VTKM_CONT void save(BinaryBuffer& bb,
const BaseType& obj)
627 Type transformedArray = obj;
628 vtkmdiy::save(bb, transformedArray.GetTransformedArray());
629 vtkmdiy::save(bb, transformedArray.GetFunctor());
630 vtkmdiy::save(bb, transformedArray.GetInverseFunctor());
639 InvFunctor invFunctor;
645 template <
typename AH,
typename Functor,
typename InvFunctor>
646 struct Serialization<
vtkm::cont::ArrayHandle<
647 typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
648 vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
649 : Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
656 #endif //vtk_m_cont_ArrayHandleTransform_h