Boost logo

Boost :

Subject: Re: [boost] [dynamic_bitset] intersects
From: Joachim Faulhaber (afojgo_at_[hidden])
Date: 2009-03-08 16:10:56


2009/3/8 Steven Watanabe <watanabesj_at_[hidden]>:
> AMDG
>
> Joachim Faulhaber wrote:
>>
>> I think that the assumption of Vicente ...
>>
>> dynamic_bitset<> bs1, bs2;
>> // ... expecting ...
>> intersetcs(bs1, bs2)?
>>
>> is kind of justified, because function intersects could be implemented
>> not only by memberfunctions of dynamic_bitset but even with non member
>> functions, in more than one way,
>>
>> e.g.
>> bool intersects(const dynamic_bitset<Block, Allocator>& a,
>>               const dynamic_bitset<Block, Allocator>& b)
>> // nonmembers & and member empty()
>> { return !(a & b).empty(); }
>>
>>
>> this would be consistent with the coding standard to keep class
>> interfaces minimal and to implement namespace global functions
>> on the bases of class memberfunctions where ever this is possible.
>>
>
> This would be less efficient because of the temporary.
>

Thank you both (this also refers to Vicente since he had the same
argument).

Yes, but this is not the only way to implement the function.

I think this implementation is as efficient as the member function:

template <typename Block, typename Allocator>
bool intersects(const dynamic_bitset<Block, Allocator>& a,
                const dynamic_bitset<Block, Allocator>& b)
{
    typedef typename
        dynamic_bitset<Block, Allocator>::size_type size_type;
    size_type common_blocks = a.num_blocks() < b.num_blocks()
                              ? a.num_blocks() : b.num_blocks();

    for(size_type i = 0; i < common_blocks; ++i) {
        if(a.test(i) & b.test(i))
            return true;
    }
    return false;
}

best regards, Joachim


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk