llvm-project
llvm-project copied to clipboard
Small issue in constructor of `std::discrete_distribution`
Hi
I happens to find a potential small issue (or at least something I don't understand) when I read through libcxx/include/__random/discrete_distribution.h:
template<class _IntType>
template<class _UnaryOperation>
discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
double __xmin,
double __xmax,
_UnaryOperation __fw)
{
if (__nw > 1)
{
__p_.reserve(__nw - 1);
double __d = (__xmax - __xmin) / __nw;
double __d2 = __d / 2;
for (size_t __k = 0; __k < __nw; ++__k)
__p_.push_back(__fw(__xmin + __k * __d + __d2));
__init();
}
}
Here we first call __p_.reserve(__nw - 1) to reserve size of __nw - 1 for __p_, but later we loop for __nw times to push values into __p_.
It doesn't effect the functionality, but shouldn't we just reserve __nw instead of __nw - 1?
Ideally it should reserve __nw, and if the __nw is 0 then then it should be assigned to 1.