You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
rhalbersma
changed the title
Possible improvement for is_proper_subset
Possible improvement for is_proper_subset_of
Aug 28, 2022
The current implementation for the
is_proper_subset_of
can be slightly improved by breaking out of the loop afterproper
has been set totrue
. 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 ofif
-statements on random data.The code would look like:
If a PR along these lines would be welcome, then I'd be happy to submit one.
The text was updated successfully, but these errors were encountered: