Commit 1759d172 authored by Jan Koniarik's avatar Jan Koniarik
Browse files

slight improvements to circular buffer

parent 37b5d1b1
Pipeline #105449 passed with stage
in 49 seconds
...@@ -49,33 +49,28 @@ public: ...@@ -49,33 +49,28 @@ public:
static_circular_buffer() = default; static_circular_buffer() = default;
static_circular_buffer( const static_circular_buffer& other ) static_circular_buffer( const static_circular_buffer& other )
{ {
for ( size_type i = 0; i < other.size(); ++i ) { copy_from( other );
push_back( other[i] );
}
} }
static_circular_buffer( static_circular_buffer&& other ) noexcept static_circular_buffer( static_circular_buffer&& other ) noexcept
{ {
while ( !other.empty() ) { move_from( std::move( other ) );
push_back( other.pop_front() );
}
} }
static_circular_buffer& operator=( const static_circular_buffer& other ) static_circular_buffer& operator=( const static_circular_buffer& other )
{ {
if ( this != &other ) { if ( this != &other ) {
this->~static_circular_buffer(); clear();
::new ( this ) static_circular_buffer( other ); copy_from( other );
} }
return *this; return *this;
} }
static_circular_buffer& operator=( static_circular_buffer&& other ) noexcept static_circular_buffer& operator=( static_circular_buffer&& other ) noexcept
{ {
if ( this != &other ) { if ( this != &other ) {
this->~static_circular_buffer(); clear();
::new ( this ) static_circular_buffer( std::move( other ) ); move_from( std::move( other ) );
} }
return *this; return *this;
} }
// methods for handling the front side of the circular buffer // methods for handling the front side of the circular buffer
[[nodiscard]] iterator begin() [[nodiscard]] iterator begin()
...@@ -252,6 +247,20 @@ private: ...@@ -252,6 +247,20 @@ private:
} }
} }
void copy_from( const static_circular_buffer& other )
{
for ( size_type i = 0; i < other.size(); ++i ) {
emplace_back( other[i] );
}
}
void move_from( const static_circular_buffer& other )
{
for ( size_type i = 0; i < other.size(); ++i ) {
emplace_back( std::move( other[i] ) );
}
}
// Use this only when moving the indexes in the circular buffer - bullet-proof. // Use this only when moving the indexes in the circular buffer - bullet-proof.
[[nodiscard]] constexpr auto next( size_type i ) const [[nodiscard]] constexpr auto next( size_type i ) const
{ {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment