aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp13
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp5
2 files changed, 18 insertions, 0 deletions
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());