Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Small issue in constructor of std::discrete_distribution #58832

Copy link
Copy link
Open
@zhqrbitee

Description

@zhqrbitee
Issue body actions

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.performancerandom

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.