add new type for ring buffer

This commit is contained in:
chatlanin 2025-01-04 11:41:10 +03:00
parent bb94945fa7
commit bc04dc09e9
4 changed files with 25 additions and 16 deletions

View File

@ -36,4 +36,6 @@ auto main(int argc, char *argv[]) -> int
hack::log()(hack::mt::algorithms::max(4, 5)); hack::log()(hack::mt::algorithms::max(4, 5));
hack::log()(hack::mt::algorithms::max(c, b)); hack::log()(hack::mt::algorithms::max(c, b));
} }
hack::error()("asdfJK");
} }

View File

@ -5,6 +5,8 @@
#include "hack/utils/color.hpp" #include "hack/utils/color.hpp"
#include "hack/exception/title.hpp" #include "hack/exception/title.hpp"
// NOTE
// вмесо nlohman::json можно поробовать прикрутить https://jqlang.github.io/jq
namespace hack namespace hack
{ {
class exception class exception

View File

@ -1,19 +1,23 @@
#pragma once #pragma once
#include <array> #include <vector>
#include <mutex> #include <mutex>
#include <optional> #include <optional>
#include <vector> #include <vector>
namespace hack::patterns namespace hack::patterns
{ {
template<typename T, std::size_t BUFFER_SIZE> template<typename T>
class ring_buffer class ring_buffer
{ {
using MUTEX = std::lock_guard<std::recursive_mutex>; using MUTEX = std::lock_guard<std::recursive_mutex>;
public: public:
explicit ring_buffer() = default; explicit ring_buffer(int s)
{
m_size = s;
m_data.resize(m_size);
};
public: public:
void put(T item) noexcept void put(T item) noexcept
@ -52,13 +56,13 @@ namespace hack::patterns
if(empty()) return std::nullopt; if(empty()) return std::nullopt;
auto val = m_data[m_tail]; auto val = m_data[m_tail];
m_tail = (m_tail + 1) % BUFFER_SIZE; m_tail = (m_tail + 1) % m_size;
m_full = false; m_full = false;
return val; return val;
} }
std::array<T, BUFFER_SIZE>& get_src() const noexcept std::vector<T>& get_src() const noexcept
{ {
return m_data; return m_data;
} }
@ -73,13 +77,13 @@ namespace hack::patterns
{ {
MUTEX lock(m_mutex); MUTEX lock(m_mutex);
std::size_t size; std::size_t s;
if (!m_full) if (!m_full)
size = (m_head >= m_tail) ? m_head - m_tail : BUFFER_SIZE - (m_tail - m_head); s = (m_head >= m_tail) ? m_head - m_tail : m_size - (m_tail - m_head);
else else
size = BUFFER_SIZE; s = m_size;
return size; return s;
} }
void reset() noexcept void reset() noexcept
@ -96,14 +100,14 @@ namespace hack::patterns
std::size_t capacity() const noexcept std::size_t capacity() const noexcept
{ {
return BUFFER_SIZE; return m_size;
} }
private: private:
void head_refresh() void head_refresh()
{ {
if (m_full) m_tail = (m_tail + 1) % BUFFER_SIZE; if (m_full) m_tail = (m_tail + 1) % m_size;
m_head = (m_head + 1) % BUFFER_SIZE; m_head = (m_head + 1) % m_size;
m_full = m_head == m_tail; m_full = m_head == m_tail;
} }
@ -113,7 +117,8 @@ namespace hack::patterns
std::size_t m_tail{ 0 }; std::size_t m_tail{ 0 };
private: private:
int m_size;
mutable std::recursive_mutex m_mutex; mutable std::recursive_mutex m_mutex;
mutable std::array<T, BUFFER_SIZE> m_data; mutable std::vector<T> m_data;
}; };
} }

View File

@ -7,7 +7,7 @@ TEST_CASE("patterns")
SECTION("ring buffer") SECTION("ring buffer")
{ {
// single value // single value
hack::patterns::ring_buffer<int, 10> rb1; hack::patterns::ring_buffer<int> rb1(10);
REQUIRE(rb1.empty() == true); REQUIRE(rb1.empty() == true);
for (int i = 1; i < 13; ++i) rb1.put(i); for (int i = 1; i < 13; ++i) rb1.put(i);
@ -36,7 +36,7 @@ TEST_CASE("patterns")
// vector value // vector value
std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
hack::patterns::ring_buffer<int, 10> rb2; hack::patterns::ring_buffer<int> rb2(10);
REQUIRE(rb2.empty() == true); REQUIRE(rb2.empty() == true);
rb2.put(v); rb2.put(v);