diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-09-30 15:42:50 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-09-30 15:42:50 +0200 |
| commit | 3d2b027d86ca65975c19a26968851dc50e142849 (patch) | |
| tree | 5b878d30fcf7dc84979208da956247c66f3cff89 /libs/kstd | |
| parent | d8b4ae2ddffa7ff9613d7a6416f3a405d81dad43 (diff) | |
| download | kernel-3d2b027d86ca65975c19a26968851dc50e142849.tar.xz kernel-3d2b027d86ca65975c19a26968851dc50e142849.zip | |
kstd: ring_buffer: implement size()
Diffstat (limited to 'libs/kstd')
| -rw-r--r-- | libs/kstd/kstd/ring_buffer.hpp | 13 | ||||
| -rw-r--r-- | libs/kstd/kstd/ring_buffer.tests.cpp | 5 |
2 files changed, 18 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp index a25e37b7..0e54c070 100644 --- a/libs/kstd/kstd/ring_buffer.hpp +++ b/libs/kstd/kstd/ring_buffer.hpp @@ -34,6 +34,19 @@ namespace kstd return m_head == m_tail; } + //! Get the size of this ring buffer. + //! + //! @return The number of elements currently stored in this ring buffer. + [[nodiscard]] constexpr auto size() const noexcept -> size_type + { + if (m_head >= m_tail) + { + return m_head - m_tail; + } + + return capacity() - m_tail + m_head; + } + //! 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 diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp index 37243014..6e936248 100644 --- a/libs/kstd/kstd/ring_buffer.tests.cpp +++ b/libs/kstd/kstd/ring_buffer.tests.cpp @@ -20,6 +20,11 @@ SCENARIO("Ring Buffer initialization and construction", "[ring_buffer]") REQUIRE(buffer.empty()); } + THEN("the size is 0") + { + REQUIRE(buffer.size() == 0); + } + THEN("the maximum size is equal to the capacity") { REQUIRE(buffer.max_size() == buffer.capacity()); |
