remove old ring buffer
This commit is contained in:
@@ -1,155 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <mutex>
|
|
||||||
#include <optional>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "hack/exception/exception.hpp"
|
|
||||||
|
|
||||||
namespace hack::patterns
|
|
||||||
{
|
|
||||||
// Колцевой буфер.
|
|
||||||
template<typename T>
|
|
||||||
class ring_buffer
|
|
||||||
{
|
|
||||||
using MUTEX = std::lock_guard<std::recursive_mutex>;
|
|
||||||
|
|
||||||
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<T>& 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<T>& source)
|
|
||||||
{
|
|
||||||
put(source, source.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<T> 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<T>& 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<T>& 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<T> m_data;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user