From ba2cbded0fb9e118bdd4b8a0069c89928716c342 Mon Sep 17 00:00:00 2001 From: Iskandar Safarov Date: Wed, 21 Jun 2023 12:26:13 +1000 Subject: [PATCH 1/2] Added multiple pop_front as an atomic operation --- circular_buffer.h | 24 +++++++++++++++++++----- tests/member_func_test.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/circular_buffer.h b/circular_buffer.h index 0c3beec..b250585 100644 --- a/circular_buffer.h +++ b/circular_buffer.h @@ -85,6 +85,7 @@ class CircularBuffer { void push_back(const value_type& data); void push_back(value_type&& data) noexcept; void pop_front(); + void pop_front(size_t count); reference front(); reference back(); const_reference front() const; @@ -119,7 +120,7 @@ class CircularBuffer { private: void _increment_bufferstate(); - void _decrement_bufferstate(); + void _decrement_bufferstate(size_t count); mutable std::mutex _mtx; std::unique_ptr _buff; size_type _head = 0; @@ -366,14 +367,27 @@ void CircularBuffer::pop_front(){ std::lock_guard _lck(_mtx); if(empty()) throw std::length_error("pop_front called on empty buffer"); - _decrement_bufferstate(); + _decrement_bufferstate(1); +} + +template +inline +void CircularBuffer::pop_front(size_t count){ + if(count > 0) { + if(empty()) + throw std::length_error("pop_front called on empty buffer"); + if (count > _size) { + count = _size; + } + _decrement_bufferstate(count); + } } template inline -void CircularBuffer::_decrement_bufferstate(){ - --_size; - _tail = (_tail + 1)%_max_size; +void CircularBuffer::_decrement_bufferstate(size_t count){ + _size -= count; + _tail = (_tail + count)%_max_size; } template diff --git a/tests/member_func_test.cpp b/tests/member_func_test.cpp index e429f44..70e9c0e 100644 --- a/tests/member_func_test.cpp +++ b/tests/member_func_test.cpp @@ -148,6 +148,34 @@ TEST_F(CircularBufferTest, PopFrontTest){ } +TEST_F(CircularBufferTest, MultiPopFrontTest){ + // push back & pop individual elements + test_buff.push_back("string1"); + test_buff.push_back("string2"); + test_buff.push_back("string3"); + EXPECT_EQ(test_buff.size(), 3); + test_buff.pop_front(2); + EXPECT_EQ(test_buff.size(), 1); + EXPECT_EQ(test_buff.front(), "string3"); + + //create full buffer + for(int i=0; i Date: Wed, 21 Jun 2023 12:49:42 +1000 Subject: [PATCH 2/2] Fixed whitespaces --- circular_buffer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/circular_buffer.h b/circular_buffer.h index b250585..0264535 100644 --- a/circular_buffer.h +++ b/circular_buffer.h @@ -85,7 +85,7 @@ class CircularBuffer { void push_back(const value_type& data); void push_back(value_type&& data) noexcept; void pop_front(); - void pop_front(size_t count); + void pop_front(size_t count); reference front(); reference back(); const_reference front() const; @@ -120,7 +120,7 @@ class CircularBuffer { private: void _increment_bufferstate(); - void _decrement_bufferstate(size_t count); + void _decrement_bufferstate(size_t count); mutable std::mutex _mtx; std::unique_ptr _buff; size_type _head = 0;