From afcaf475fd12a3ad7577da133d3aca0b1716f029 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Mon, 15 Sep 2025 11:01:47 +0300 Subject: [PATCH] remove old ring buffer --- src/hack/patterns/ring_buffer.OLD.hpp | 155 -------------------------- 1 file changed, 155 deletions(-) delete mode 100644 src/hack/patterns/ring_buffer.OLD.hpp diff --git a/src/hack/patterns/ring_buffer.OLD.hpp b/src/hack/patterns/ring_buffer.OLD.hpp deleted file mode 100644 index 832a7b9..0000000 --- a/src/hack/patterns/ring_buffer.OLD.hpp +++ /dev/null @@ -1,155 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "hack/exception/exception.hpp" - -namespace hack::patterns -{ - // Колцевой буфер. - template - class ring_buffer - { - using MUTEX = std::lock_guard; - - public: - ring_buffer() = default; - explicit ring_buffer(int s) - { - m_size = s; - m_data.resize(m_size); - }; - - public: - void create(int s) - { - if (m_size > 0) return; - m_size = s; - m_data.resize(m_size); - } - - void put(T item) noexcept - { - MUTEX lock(m_mutex); - m_data[m_head] = item; - head_refresh(); - } - - // указываем размер, который хотим положить - // т.к. может нужно положить только часть из переданного массива - void put(const std::vector& source, std::size_t size) - { - if (source.size() < size) - { - hack::exception ex; - ex.title(exception_title::NO_VALID_SIZE); - ex.description("data size is not equal source"); - throw ex; - } - - MUTEX lock(m_mutex); - for (std::size_t i = 0; i < size; ++i) - { - m_data[m_head] = source[i]; - head_refresh(); - } - } - - // если знаем, что нужно класть весь массив - void put(const std::vector& source) - { - put(source, source.size()); - } - - std::optional get() noexcept - { - MUTEX lock(m_mutex); - if(empty()) return std::nullopt; - - auto val = m_data[m_tail]; - m_tail = (m_tail + 1) % m_size; - m_full = false; - - return val; - } - - void get(std::vector& d, int n) - { - if (empty()) return; - - int c = m_tail; - for (int i = 0; i < n; ++i) - { - d[i] = m_data[c]; - c = (c + 1) % m_size; - } - } - - const std::vector& get_logger_data() const noexcept - { - return m_data; - } - - void skip(std::size_t n) - { - m_tail += n; - while (m_tail >= m_size) m_tail -= m_size; - } - - - bool empty() const noexcept - { - MUTEX lock(m_mutex); - return (!m_full && (m_head == m_tail)); - } - - std::size_t size() const noexcept - { - MUTEX lock(m_mutex); - - std::size_t s; - if (!m_full) s = (m_head >= m_tail) ? m_head - m_tail : m_size - (m_tail - m_head); - else s = m_size; - - return s; - } - - void reset() noexcept - { - MUTEX lock(m_mutex); - m_head = m_tail; - m_full = false; - } - - bool full() const noexcept - { - return m_full; - } - - std::size_t capacity() const noexcept - { - return m_size; - } - - private: - void head_refresh() - { - if (m_full) m_tail = (m_tail + 1) % m_size; - m_head = (m_head + 1) % m_size; - m_full = m_head == m_tail; - } - - private: - std::size_t m_head = 0; - std::size_t m_tail = 0; - std::size_t m_size = 0; - bool m_full = false; - - private: - mutable std::recursive_mutex m_mutex; - mutable std::vector m_data; - }; -}