Fix low quality code in Qualtile (found by PVS-Studio)

This commit is contained in:
Alexey Milovidov 2021-05-08 17:05:58 +03:00
parent 125b2ab214
commit 78a2df5900

View File

@ -263,7 +263,7 @@ struct QuantileExactLow : public QuantileExactBase<Value, QuantileExactLow<Value
{ {
// sort inputs in ascending order // sort inputs in ascending order
std::sort(array.begin(), array.end()); std::sort(array.begin(), array.end());
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
// if level is 0.5 then compute the "low" median of the sorted array // if level is 0.5 then compute the "low" median of the sorted array
// by the method of rounding. // by the method of rounding.
if (level == 0.5) if (level == 0.5)
@ -278,10 +278,14 @@ struct QuantileExactLow : public QuantileExactBase<Value, QuantileExactLow<Value
return array[static_cast<size_t>((floor(s / 2)) - 1)]; return array[static_cast<size_t>((floor(s / 2)) - 1)];
} }
} }
// else quantile is the nth index of the sorted array obtained by multiplying else
// level and size of array. Example if level = 0.1 and size of array is 10, {
// then return array[1]. // else quantile is the nth index of the sorted array obtained by multiplying
return array[n]; // level and size of array. Example if level = 0.1 and size of array is 10,
// then return array[1].
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
return array[n];
}
} }
return std::numeric_limits<Value>::quiet_NaN(); return std::numeric_limits<Value>::quiet_NaN();
} }
@ -295,7 +299,7 @@ struct QuantileExactLow : public QuantileExactBase<Value, QuantileExactLow<Value
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
auto level = levels[indices[i]]; auto level = levels[indices[i]];
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
// if level is 0.5 then compute the "low" median of the sorted array // if level is 0.5 then compute the "low" median of the sorted array
// by the method of rounding. // by the method of rounding.
if (level == 0.5) if (level == 0.5)
@ -310,9 +314,13 @@ struct QuantileExactLow : public QuantileExactBase<Value, QuantileExactLow<Value
result[indices[i]] = array[static_cast<size_t>(floor((s / 2) - 1))]; result[indices[i]] = array[static_cast<size_t>(floor((s / 2) - 1))];
} }
} }
// else quantile is the nth index of the sorted array obtained by multiplying else
// level and size of array. Example if level = 0.1 and size of array is 10. {
result[indices[i]] = array[n]; // else quantile is the nth index of the sorted array obtained by multiplying
// level and size of array. Example if level = 0.1 and size of array is 10.
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
result[indices[i]] = array[n];
}
} }
} }
else else
@ -337,7 +345,7 @@ struct QuantileExactHigh : public QuantileExactBase<Value, QuantileExactHigh<Val
{ {
// sort inputs in ascending order // sort inputs in ascending order
std::sort(array.begin(), array.end()); std::sort(array.begin(), array.end());
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
// if level is 0.5 then compute the "high" median of the sorted array // if level is 0.5 then compute the "high" median of the sorted array
// by the method of rounding. // by the method of rounding.
if (level == 0.5) if (level == 0.5)
@ -345,9 +353,13 @@ struct QuantileExactHigh : public QuantileExactBase<Value, QuantileExactHigh<Val
auto s = array.size(); auto s = array.size();
return array[static_cast<size_t>(floor(s / 2))]; return array[static_cast<size_t>(floor(s / 2))];
} }
// else quantile is the nth index of the sorted array obtained by multiplying else
// level and size of array. Example if level = 0.1 and size of array is 10. {
return array[n]; // else quantile is the nth index of the sorted array obtained by multiplying
// level and size of array. Example if level = 0.1 and size of array is 10.
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
return array[n];
}
} }
return std::numeric_limits<Value>::quiet_NaN(); return std::numeric_limits<Value>::quiet_NaN();
} }
@ -361,7 +373,7 @@ struct QuantileExactHigh : public QuantileExactBase<Value, QuantileExactHigh<Val
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
auto level = levels[indices[i]]; auto level = levels[indices[i]];
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
// if level is 0.5 then compute the "high" median of the sorted array // if level is 0.5 then compute the "high" median of the sorted array
// by the method of rounding. // by the method of rounding.
if (level == 0.5) if (level == 0.5)
@ -369,9 +381,13 @@ struct QuantileExactHigh : public QuantileExactBase<Value, QuantileExactHigh<Val
auto s = array.size(); auto s = array.size();
result[indices[i]] = array[static_cast<size_t>(floor(s / 2))]; result[indices[i]] = array[static_cast<size_t>(floor(s / 2))];
} }
// else quantile is the nth index of the sorted array obtained by multiplying else
// level and size of array. Example if level = 0.1 and size of array is 10. {
result[indices[i]] = array[n]; // else quantile is the nth index of the sorted array obtained by multiplying
// level and size of array. Example if level = 0.1 and size of array is 10.
size_t n = level < 1 ? level * array.size() : (array.size() - 1);
result[indices[i]] = array[n];
}
} }
} }
else else