Boost logo

Boost :

Subject: Re: [boost] [utility] new auto_buffer class --- RFC
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-03-15 14:34:04


Scott McMurray skrev:
> On 2009-03-15, Thorsten Ottosen <thorsten.ottosen_at_[hidden]> wrote:
>> Scott McMurray skrev:
>>> Is there really a cost to allowing it, though? I'd assume it would
>>> have a vector-like operator= that'd be something like this:
>>>
>>> copy(other.begin(), other.begin() + size(), begin());
>>> if (size() > other.size()) erase(begin()+other.size(), end());
>>> else insert(end(), other.begin()+size(), other.end());
>> That is not quite how I implemented it, but for operator= the extra
>> check is not that important, so I can add it,
>>
>
> I agree that self-assignment is uncommon enough for an explicit check
> to be a pessimization, though. Is the algorithm you have ( the
> attachment to the first post in this thread doesn't have operator= )
> really more efficient than one that doesn't need the explicit check?

Here's the current version. I guess I would have to do some test to make
100% sure it's faster.

-Thorsten

        auto_buffer& operator=( const auto_buffer& r ) // basic
         {
             BOOST_ASSERT( this != &r );
             difference_type diff = size_ - r.size_;
             if( diff >= 0 )
             {
                 pop_back_n( static_cast<size_type>(diff) );
                 assign_impl( r.begin(), r.end(), begin() );
             }
             else
             {
                 if( capacity_ >= r.size() )
                 {
                     unchecked_push_back_n( static_cast<size_type>(-diff) );
                     assign_impl( r.begin(), r.end(), begin() );
                 }
                 else
                 {
                     pointer new_buffer = allocate( r.size() );
                     (*this).~auto_buffer();
                     buffer_ = new_buffer;
                     capacity_ = r.size();
                     copy_impl( r.begin(), r.end(), buffer_ );
                     size_ = capacity_;
                 }
             }

             BOOST_ASSERT( size() == r.size() );
             BOOST_ASSERT( is_valid() );
             return *this;
         }


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