Skip to content

Possible improvement for is_proper_subset_of #72

Open
@rhalbersma

Description

@rhalbersma

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions