10 #ifndef vtk_m_cont_ArrayHandleRecombineVec_h
11 #define vtk_m_cont_ArrayHandleRecombineVec_h
27 template <
typename PortalType>
34 using ComponentType =
typename std::remove_const<typename PortalType::ValueType>::type;
36 RecombineVec(
const RecombineVec&) =
default;
50 vtkm::internal::ArrayPortalValueReference<PortalType> operator[](
vtkm::IdComponent cIndex)
const
52 return vtkm::internal::ArrayPortalValueReference<PortalType>(this->Portals[cIndex],
56 template <
typename T, vtkm::IdComponent DestSize>
59 vtkm::IdComponent numComponents = vtkm::Min(DestSize, this->GetNumberOfComponents());
62 dest[cIndex] = this->Portals[cIndex].Get(this->Index);
79 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
86 VTKM_EXEC_CONT operator ComponentType()
const {
return this->Portals[0].Get(this->Index); }
88 template <vtkm::IdComponent N>
92 this->CopyInto(result);
96 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
100 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
101 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
103 (*this)[cIndex] += VTraits::GetComponent(src, cIndex);
107 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
111 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
112 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
114 (*this)[cIndex] -= VTraits::GetComponent(src, cIndex);
118 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
122 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
123 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
125 (*this)[cIndex] *= VTraits::GetComponent(src, cIndex);
129 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
133 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
134 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
136 (*this)[cIndex] /= VTraits::GetComponent(src, cIndex);
140 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
144 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
145 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
147 (*this)[cIndex] %= VTraits::GetComponent(src, cIndex);
151 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
155 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
156 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
158 (*this)[cIndex] &= VTraits::GetComponent(src, cIndex);
162 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
166 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
167 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
169 (*this)[cIndex] |= VTraits::GetComponent(src, cIndex);
173 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
177 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
178 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
180 (*this)[cIndex] ^= VTraits::GetComponent(src, cIndex);
184 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
188 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
189 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
191 (*this)[cIndex] >>= VTraits::GetComponent(src, cIndex);
195 template <typename T, typename = typename std::enable_if<vtkm::HasVecTraits<T>::value>::type>
199 VTKM_ASSERT(this->GetNumberOfComponents() == VTraits::GetNumberOfComponents(src));
200 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
202 (*this)[cIndex] <<= VTraits::GetComponent(src, cIndex);
208 template <
typename T>
213 if (numComponents > 1)
215 if (numComponents > this->GetNumberOfComponents())
217 numComponents = this->GetNumberOfComponents();
221 this->Portals[cIndex].Set(this->Index,
222 static_cast<ComponentType
>(VTraits::GetComponent(src, cIndex)));
228 for (
vtkm::IdComponent cIndex = 0; cIndex < this->GetNumberOfComponents(); ++cIndex)
230 this->Portals[cIndex].Set(this->Index,
231 static_cast<ComponentType
>(VTraits::GetComponent(src, 0)));
239 template <
typename PortalType>
240 struct TypeTraits<
vtkm::internal::RecombineVec<PortalType>>
243 using VecType = vtkm::internal::RecombineVec<PortalType>;
244 using ComponentType =
typename VecType::ComponentType;
253 return vtkm::internal::RecombineVec<PortalType>{};
257 template <
typename PortalType>
258 struct VecTraits<
vtkm::internal::RecombineVec<PortalType>>
260 using VecType = vtkm::internal::RecombineVec<PortalType>;
268 return vector.GetNumberOfComponents();
274 return vector[componentIndex];
281 vector[componentIndex] = component;
284 template <vtkm::IdComponent destSize>
294 template <
typename SourcePortalType>
295 class ArrayPortalRecombineVec
301 const SourcePortalType* Portals;
305 using ValueType = vtkm::internal::RecombineVec<SourcePortalType>;
307 ArrayPortalRecombineVec() =
default;
308 ArrayPortalRecombineVec(
const SourcePortalType* portals,
vtkm::IdComponent numComponents)
310 , NumberOfComponents(numComponents)
318 return ValueType({ this->Portals, this->NumberOfComponents }, index);
328 template <
typename T>
332 VTKM_ASSERT(Traits::GetNumberOfComponents(value) == this->NumberOfComponents);
335 this->Portals[cIndex].Set(index, Traits::GetComponent(value, cIndex));
351 struct StorageTagRecombineVec
358 struct RecombineVecMetaData
360 mutable std::vector<vtkm::cont::internal::Buffer> PortalBuffers;
361 std::vector<std::size_t> ArrayBufferOffsets;
363 RecombineVecMetaData() =
default;
365 RecombineVecMetaData(
const RecombineVecMetaData& src) { *
this = src; }
367 RecombineVecMetaData& operator=(
const RecombineVecMetaData& src)
369 this->ArrayBufferOffsets = src.ArrayBufferOffsets;
371 this->PortalBuffers.clear();
378 template <
typename T>
379 using RecombinedPortalType = vtkm::internal::ArrayPortalMultiplexer<
380 typename vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagStride>::ReadPortalType,
381 typename vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagStride>::WritePortalType>;
383 template <
typename T>
384 using RecombinedValueType = vtkm::internal::RecombineVec<RecombinedPortalType<T>>;
388 template <
typename ReadWritePortal>
389 class Storage<
vtkm::internal::RecombineVec<ReadWritePortal>,
390 vtkm::cont::internal::StorageTagRecombineVec>
392 using ComponentType =
typename ReadWritePortal::ValueType;
393 using SourceStorage = vtkm::cont::internal::Storage<ComponentType, vtkm::cont::StorageTagStride>;
397 (std::is_same<ReadWritePortal, detail::RecombinedPortalType<ComponentType>>::value));
399 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> BuffersForComponent(
400 const std::vector<vtkm::cont::internal::Buffer>& buffers,
403 auto& metaData = buffers[0].GetMetaData<detail::RecombineVecMetaData>();
404 std::size_t index =
static_cast<std::size_t
>(componentIndex);
405 return std::vector<vtkm::cont::internal::Buffer>(
406 buffers.begin() + metaData.ArrayBufferOffsets[index],
407 buffers.begin() + metaData.ArrayBufferOffsets[index + 1]);
413 using ReadPortalType = vtkm::internal::ArrayPortalRecombineVec<ReadWritePortal>;
414 using WritePortalType = vtkm::internal::ArrayPortalRecombineVec<ReadWritePortal>;
417 const std::vector<vtkm::cont::internal::Buffer>& buffers)
420 buffers[0].GetMetaData<detail::RecombineVecMetaData>().ArrayBufferOffsets.size() - 1);
424 const std::vector<vtkm::cont::internal::Buffer>& buffers)
426 return SourceStorage::GetNumberOfValues(BuffersForComponent(buffers, 0));
429 VTKM_CONT static void Fill(
const std::vector<vtkm::cont::internal::Buffer>&,
430 const vtkm::internal::RecombineVec<ReadWritePortal>&,
438 VTKM_CONT static ReadPortalType CreateReadPortal(
439 const std::vector<vtkm::cont::internal::Buffer>& buffers,
448 vtkm::cont::internal::Buffer portalBuffer;
458 buffers[0].GetMetaData<detail::RecombineVecMetaData>().PortalBuffers.push_back(portalBuffer);
461 ReadWritePortal* portals =
462 reinterpret_cast<ReadWritePortal*
>(portalBuffer.WritePointerHost(token));
465 portals[cIndex] = ReadWritePortal(
466 SourceStorage::CreateReadPortal(BuffersForComponent(buffers, cIndex), device, token));
471 return ReadPortalType(
472 reinterpret_cast<const ReadWritePortal*
>(portalBuffer.ReadPointerDevice(device, token)),
476 VTKM_CONT static WritePortalType CreateWritePortal(
477 const std::vector<vtkm::cont::internal::Buffer>& buffers,
486 vtkm::cont::internal::Buffer portalBuffer;
496 buffers[0].GetMetaData<detail::RecombineVecMetaData>().PortalBuffers.push_back(portalBuffer);
499 ReadWritePortal* portals =
500 reinterpret_cast<ReadWritePortal*
>(portalBuffer.WritePointerHost(token));
503 portals[cIndex] = ReadWritePortal(
504 SourceStorage::CreateWritePortal(BuffersForComponent(buffers, cIndex), device, token));
509 return WritePortalType(
510 reinterpret_cast<const ReadWritePortal*
>(portalBuffer.ReadPointerDevice(device, token)),
514 VTKM_CONT static ArrayType ArrayForComponent(
515 const std::vector<vtkm::cont::internal::Buffer>& buffers,
518 return ArrayType(BuffersForComponent(buffers, componentIndex));
521 VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
523 detail::RecombineVecMetaData metaData;
524 metaData.ArrayBufferOffsets.push_back(1);
525 return vtkm::cont::internal::CreateBuffers(metaData);
528 VTKM_CONT static void AppendComponent(std::vector<vtkm::cont::internal::Buffer>& buffers,
529 const ArrayType& array)
532 buffers.insert(buffers.end(), array.GetBuffers().begin(), array.GetBuffers().end());
534 buffers[0].GetMetaData<detail::RecombineVecMetaData>().ArrayBufferOffsets.push_back(
556 template <
typename ComponentType>
559 vtkm::cont::internal::StorageTagRecombineVec>
566 vtkm::cont::internal::StorageTagRecombineVec>));
569 using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
574 return StorageType::NumberOfComponents(this->
GetBuffers());
580 return StorageType::ArrayForComponent(this->
GetBuffers(), componentIndex);
586 std::vector<vtkm::cont::internal::Buffer> buffers = this->
GetBuffers();
587 StorageType::AppendComponent(buffers, array);
596 struct ArrayExtractComponentImpl<
vtkm::cont::internal::StorageTagRecombineVec>
598 template <
typename RecombineVec>
606 using ComponentType =
typename RecombineVec::ComponentType;
610 array.GetComponentArray(componentIndex / subComponents),
611 componentIndex % subComponents,
625 #endif //vtk_m_cont_ArrayHandleRecombineVec_h