Skip to content

Commit

Permalink
ENH: Fix compile warnings in FilterUtilities. Init array in DataStore…
Browse files Browse the repository at this point in the history
… when resizing

Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Jul 29, 2024
1 parent c3be139 commit bbc909f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/simplnx/DataStructure/DataStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ class DataStore : public AbstractDataStore<T>
, m_TupleShape(tupleShape)
, m_NumComponents(std::accumulate(m_ComponentShape.cbegin(), m_ComponentShape.cend(), static_cast<size_t>(1), std::multiplies<>()))
, m_NumTuples(std::accumulate(m_TupleShape.cbegin(), m_TupleShape.cend(), static_cast<size_t>(1), std::multiplies<>()))
, m_InitValue(initValue)
{
resizeTuples(m_TupleShape);
if(initValue.has_value())
if(m_InitValue.has_value())
{
std::fill_n(data(), this->getSize(), *initValue);
std::fill_n(data(), this->getSize(), *m_InitValue);
}
}

Expand All @@ -82,6 +83,47 @@ class DataStore : public AbstractDataStore<T>
, m_NumComponents(std::accumulate(m_ComponentShape.cbegin(), m_ComponentShape.cend(), static_cast<size_t>(1), std::multiplies<>()))
, m_NumTuples(std::accumulate(m_TupleShape.cbegin(), m_TupleShape.cend(), static_cast<size_t>(1), std::multiplies<>()))
{
// Because no init value is passed into the constructor, we will use a "mudflap" style value that is easy to debug.
if constexpr(sizeof(T) == 1)
{
m_InitValue = 0xAB;
}
if constexpr(sizeof(T) == 2)
{
m_InitValue = 0xABAB;
}
if constexpr(sizeof(T) == 4)
{
union InitValue {
uint32 i32;
float32 f32;
} initValue;
initValue.i32 = 0xABABABAB;
if constexpr(std::is_floating_point_v<T>)
{
m_InitValue = initValue.f32;
}
else
{
m_InitValue = initValue.i32;
}
}
if constexpr(sizeof(T) == 8)
{
union InitValue {
uint64 i64;
float64 f64;
} initValue;
initValue.i64 = 0xABABABABABABABAB;
if constexpr(std::is_floating_point_v<T>)
{
m_InitValue = initValue.f64;
}
else
{
m_InitValue = initValue.i64;
}
}
}

/**
Expand All @@ -94,6 +136,7 @@ class DataStore : public AbstractDataStore<T>
, m_TupleShape(other.m_TupleShape)
, m_NumComponents(other.m_NumComponents)
, m_NumTuples(other.m_NumTuples)
, m_InitValue(other.m_InitValue)
{
const usize count = other.getSize();
auto* data = new value_type[count];
Expand All @@ -112,6 +155,7 @@ class DataStore : public AbstractDataStore<T>
, m_Data(std::move(other.m_Data))
, m_NumComponents(std::move(other.m_NumComponents))
, m_NumTuples(std::move(other.m_NumTuples))
, m_InitValue(other.m_InitValue)
{
}

Expand Down Expand Up @@ -244,6 +288,17 @@ class DataStore : public AbstractDataStore<T>
{
data[i] = m_Data.get()[i];
}

// If we are sizing to a larger number of tuples, initialize the leftover array with the init
// value that was passed in during construction.
if(m_InitValue.has_value())
{
for(usize i = oldSize; i < newSize; i++)
{
data[i] = *m_InitValue;
}
}

m_Data.reset(data);
}

Expand Down Expand Up @@ -364,6 +419,7 @@ class DataStore : public AbstractDataStore<T>
std::unique_ptr<value_type[]> m_Data = nullptr;
size_t m_NumComponents = {0};
size_t m_NumTuples = {0};
std::optional<T> m_InitValue;
};

// Declare aliases
Expand Down
4 changes: 4 additions & 0 deletions src/simplnx/Utilities/FilterUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ParallelAlgorithmUtilities.hpp"
#include "simplnx/Common/Result.hpp"
#include "simplnx/Common/TypeTraits.hpp"
#include "simplnx/Common/Types.hpp"
#include "simplnx/Filter/IFilter.hpp"
#include "simplnx/Filter/Output.hpp"
Expand Down Expand Up @@ -94,6 +95,9 @@ auto RunTemplateClass(DataType dataType, ArgsT&&... args)
return ClassT<float64>(std::forward<ArgsT>(args)...)();
}
}
std::stringstream ss;
ss << "FilterUtilities::RunTemplateClass<> Error: dataType did not match any known type. DataType was " << to_underlying(dataType);
throw std::runtime_error(ss.str());
}

template <class FuncT, class... ArgsT>
Expand Down

0 comments on commit bbc909f

Please sign in to comment.