Skip to content

Commit

Permalink
ScalarField: optimize computeMinAndMax
Browse files Browse the repository at this point in the history
Also properly clears min and max if the field is not empty, but all
points are invalid.
  • Loading branch information
tpwrules committed May 4, 2024
1 parent 3d69d9b commit e03f7db
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions include/ScalarField.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,36 @@ namespace CCCoreLib

inline void ScalarField::computeMinAndMax()
{
if (!empty())
ScalarType minVal, maxVal;

bool minMaxInitialized = false;
for (std::size_t i = 0; i < size(); ++i)
{
bool minMaxInitialized = false;
for (std::size_t i = 0; i < size(); ++i)
const ScalarType& val = at(i);
if (ValidValue(val))
{
const ScalarType& val = at(i);
if (ValidValue(val))
if (minMaxInitialized)
{
if (val < minVal)
minVal = val;
else if (val > maxVal)
maxVal = val;
}
else
{
if (minMaxInitialized)
{
if (val < m_minVal)
m_minVal = val;
else if (val > m_maxVal)
m_maxVal = val;
}
else
{
//first valid value is used to init min and max
m_minVal = m_maxVal = val;
minMaxInitialized = true;
}
//first valid value is used to init min and max
minVal = maxVal = val;
minMaxInitialized = true;
}
}
}
else //particular case: no value

if (minMaxInitialized)
{
m_minVal = minVal;
m_maxVal = maxVal;
}
else //particular case: zero valid values
{
m_minVal = m_maxVal = 0;
}
Expand Down

0 comments on commit e03f7db

Please sign in to comment.