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

Possible improvement for is_proper_subset_of #72

Open
rhalbersma opened this issue Aug 28, 2022 · 0 comments
Open

Possible improvement for is_proper_subset_of #72

rhalbersma opened this issue Aug 28, 2022 · 0 comments

Comments

@rhalbersma
Copy link

rhalbersma commented Aug 28, 2022

The current implementation for the is_proper_subset_of can be slightly improved by breaking out of the loop after proper has been set to true. From that point forward, the remaining words only have to be checked for the subset-relation.

If the proper part is on average found after half the number of currently used words, then in the remaining half of the words only half the number tests need to be done. So the savings would on average amount to a quarter of the number of if-statements on random data.

The code would look like:

template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::
is_proper_subset_of(const dynamic_bitset<Block, Allocator>& a) const
{
    assert(size() == a.size());
    assert(num_blocks() == a.num_blocks());

    size_type i = 0;
    for (/* init-statement before loop */; i < num_blocks(); ++i) {
        if (m_bits[i] & ~a.m_bits[i])
            return false;   // not a subset at all
        if (a.m_bits[i] & ~m_bits[i])
            break;          // proper
    }
    if (i == num_blocks)
        return false;       // not proper, because break-statement not hit
    ++i;                    // the break-statement short-circuited the increment
    for (/* re-use i from previous loop */; i < num_blocks(); ++i)
        if (m_bits[i] & ~a.m_bits[i])
            return false;   // not a subset
    return true;
}

If a PR along these lines would be welcome, then I'd be happy to submit one.

@rhalbersma rhalbersma changed the title Possible improvement for is_proper_subset Possible improvement for is_proper_subset_of Aug 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant