From d8b4ae2ddffa7ff9613d7a6416f3a405d81dad43 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 30 Sep 2026 15:35:29 +0200 Subject: kstd: ring_buffer: implement empty() --- libs/kstd/kstd/ring_buffer.hpp | 13 +++++++++++++ libs/kstd/kstd/ring_buffer.tests.cpp | 5 +++++ 2 files changed, 18 insertions(+) (limited to 'libs') diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp index 2949988e..a25e37b7 100644 --- a/libs/kstd/kstd/ring_buffer.hpp +++ b/libs/kstd/kstd/ring_buffer.hpp @@ -26,6 +26,14 @@ namespace kstd return Capacity; } + //! Check if this ring buffer is empty. + //! + //! @return @p true iff. this ring buffer is empty, @p false otherwise. + [[nodiscard]] constexpr auto empty() const noexcept -> bool + { + return m_head == m_tail; + } + //! Get the maximum number of elements this ring buffer can hold. //! //! Due to ring_buffer being a fixed-size container, this function will return the same value as capacity. This @@ -39,6 +47,11 @@ namespace kstd //! @} + private: + //! The index of the element that was most recently added to this ring buffer. + size_type m_head{}; + //! The index of the element that was least recently added to this ring buffer. + size_type m_tail{}; }; } // namespace kstd diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp index f693b0a8..37243014 100644 --- a/libs/kstd/kstd/ring_buffer.tests.cpp +++ b/libs/kstd/kstd/ring_buffer.tests.cpp @@ -15,6 +15,11 @@ SCENARIO("Ring Buffer initialization and construction", "[ring_buffer]") REQUIRE(buffer.capacity() == 5); } + THEN("the buffer is empty") + { + REQUIRE(buffer.empty()); + } + THEN("the maximum size is equal to the capacity") { REQUIRE(buffer.max_size() == buffer.capacity()); -- cgit v1.2.3