Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Fix compile warnings in FilterUtilities. Init array in DataStore when resizing #1026

Merged
39 changes: 39 additions & 0 deletions src/simplnx/Common/TypesUtility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,51 @@
#include "simplnx/Common/TypeTraits.hpp"
#include "simplnx/Common/Types.hpp"

#if defined(__clang__) && defined(__clang_major__) && defined(__APPLE__)
#if __clang_major__ > 14
#include <bit>
namespace bs = std;
#else
#include "Bit.hpp"
namespace bs = nx::core;
#endif
#else
#include <bit>
namespace bs = std;
#endif

#include <optional>
#include <stdexcept>
#include <vector>

imikejackson marked this conversation as resolved.
Show resolved Hide resolved
namespace nx::core
{
/**
* @brief Returns the templated value for the byte pattern 0xAB based on the byte count of the template parameter
* @tparam T
* @return
*/
template <typename T>
constexpr T GetMudflap() noexcept
{
if constexpr(sizeof(T) == 1)
{
return bs::bit_cast<T>(static_cast<uint8>(0xAB));
}
if constexpr(sizeof(T) == 2)
{
return bs::bit_cast<T>(static_cast<uint16>(0xABAB));
}
if constexpr(sizeof(T) == 4)
{
return bs::bit_cast<T>(static_cast<uint32>(0xABABABAB));
}
if constexpr(sizeof(T) == 8)
{
return bs::bit_cast<T>(static_cast<uint64>(0xABABABABABABABAB));
}
}

/**
* @brief Returns the NumericType associated with T.
* @tparam T
Expand Down
19 changes: 17 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,8 @@ 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.
m_InitValue = GetMudflap<T>();
}

/**
Expand All @@ -94,6 +97,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 +116,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 +249,15 @@ 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.
T initValue = m_InitValue.has_value() ? *m_InitValue : GetMudflap<T>();
for(usize i = oldSize; i < newSize; i++)
{
data[i] = initValue;
}

m_Data.reset(data);
}

Expand Down Expand Up @@ -364,6 +378,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
3 changes: 3 additions & 0 deletions src/simplnx/Utilities/FilterUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "ParallelAlgorithmUtilities.hpp"
#include "simplnx/Common/Result.hpp"
#include "simplnx/Common/TypeTraits.hpp"
imikejackson marked this conversation as resolved.
Show resolved Hide resolved
#include "simplnx/Common/Types.hpp"
#include "simplnx/Common/TypesUtility.hpp"
#include "simplnx/Filter/IFilter.hpp"
#include "simplnx/Filter/Output.hpp"
#include "simplnx/simplnx_export.hpp"
Expand Down Expand Up @@ -94,6 +96,7 @@ auto RunTemplateClass(DataType dataType, ArgsT&&... args)
return ClassT<float64>(std::forward<ArgsT>(args)...)();
}
}
throw std::runtime_error(fmt::format("FilterUtilities::RunTemplateClass<> Error: dataType did not match any known type. DataType was {}", DataTypeToString(dataType)));
}

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