From ffc605fe86b9ad63abf4ad7bd397e049dc3a70f1 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 6 Feb 2024 16:36:05 +0100 Subject: [PATCH] Add `PointData::convertInternalData(elementTypeSpecifier)` Allows converting the internal data from one data type to another. Jeroen Eggermont suggested that it could be useful to convert data to a smaller data type, if possible. Example: // Converts internal data to std::vector pointData->convertInternalData(PointData::ElementTypeSpecifier::int8); Which is equivalent to: pointData->convertInternalData(); --- HDPS/src/plugins/PointData/src/PointData.h | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/HDPS/src/plugins/PointData/src/PointData.h b/HDPS/src/plugins/PointData/src/PointData.h index f0379c1fd..05a940a28 100644 --- a/HDPS/src/plugins/PointData/src/PointData.h +++ b/HDPS/src/plugins/PointData/src/PointData.h @@ -179,6 +179,24 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData } + /// Converts the internal data if the specified `Index` has the same numerical value as the specified `elementTypeSpecifier`. + template + void convertInternalDataIfIndexEqualsElementTypeSpecifier(const ElementTypeSpecifier elementTypeSpecifier) + { + if (static_cast(Index) == elementTypeSpecifier) + { + convertInternalData::value_type>(); + } + } + + /// Converts the internal data as specified by the `elementTypeSpecifier`, using an `std::index_sequence`. + template + void convertInternalDataUsingIndexSequence(const std::index_sequence, const ElementTypeSpecifier elementTypeSpecifier) + { + (convertInternalDataIfIndexEqualsElementTypeSpecifier(elementTypeSpecifier), ...); + } + + template void CheckDimensionIndex(const DimensionIndex& dimensionIndex) const { @@ -373,6 +391,34 @@ class POINTDATA_EXPORT PointData : public mv::plugin::RawData _numDimensions = static_cast(numDimensions); } + /// Converts the internal data to the specified element data element type, by static_cast. + template + void convertInternalData() + { + if (!std::holds_alternative>(_variantOfVectors)) + { + std::vector convertedData(getSizeOfVector()); + + std::visit([&convertedData](const auto& vec) + { + std::transform(vec.cbegin(), vec.cend(), convertedData.begin(), [](const auto elem) + { + return static_cast(elem); + }); + }, + _variantOfVectors); + + _variantOfVectors = std::move(convertedData); + } + } + + + void convertInternalData(const ElementTypeSpecifier elementTypeSpecifier) + { + convertInternalDataUsingIndexSequence(std::make_index_sequence>{}, elementTypeSpecifier); + } + + /// Copies the specified data into the internal data, sets the number of /// dimensions as specified, and sets the selected internal data type /// according to the specified data type T.