add patterns ring buffer peek
This commit is contained in:
parent
f7e7716df9
commit
2a01785775
47
bin/main.cpp
47
bin/main.cpp
@ -13,29 +13,34 @@ auto main(int argc, char *argv[]) -> int
|
||||
// patterns::ring_buffer
|
||||
{
|
||||
hack::patterns::ring_buffer<int> rb(10);
|
||||
for (int i = 1; i < 12; ++i) rb.put(i);
|
||||
for (int i = 0; i < 10; ++i) rb.put(i);
|
||||
hack::log()(rb);
|
||||
}
|
||||
|
||||
// mt::sort
|
||||
{
|
||||
std::vector<int> v { 4, 4, 6, 1, 4, 3, 2 };
|
||||
std::forward_list<int> l { 8, 7, 5, 9, 0, 1, 3, 2, 6, 4 };
|
||||
|
||||
hack::mt::algorithms::sort(v);
|
||||
hack::mt::algorithms::sort(l);
|
||||
|
||||
hack::log()(rb.size());
|
||||
rb.skip(3);
|
||||
hack::log()(rb.get().value());
|
||||
hack::log()(rb.size());
|
||||
std::vector<int> v(3);
|
||||
rb.peek(v, 3);
|
||||
hack::log()(v);
|
||||
hack::log()(l);
|
||||
}
|
||||
|
||||
// mt::max
|
||||
{
|
||||
int a = 4, b = 5;
|
||||
int& c = a;
|
||||
hack::log()(hack::mt::algorithms::max(4, 5));
|
||||
hack::log()(hack::mt::algorithms::max(c, b));
|
||||
}
|
||||
|
||||
hack::error()("asdfJK");
|
||||
// // mt::sort
|
||||
// {
|
||||
// std::vector<int> v { 4, 4, 6, 1, 4, 3, 2 };
|
||||
// std::forward_list<int> l { 8, 7, 5, 9, 0, 1, 3, 2, 6, 4 };
|
||||
//
|
||||
// hack::mt::algorithms::sort(v);
|
||||
// hack::mt::algorithms::sort(l);
|
||||
//
|
||||
// hack::log()(v);
|
||||
// hack::log()(l);
|
||||
// }
|
||||
//
|
||||
// // mt::max
|
||||
// {
|
||||
// int a = 4, b = 5;
|
||||
// int& c = a;
|
||||
// hack::log()(hack::mt::algorithms::max(4, 5));
|
||||
// hack::log()(hack::mt::algorithms::max(c, b));
|
||||
// }
|
||||
}
|
||||
|
@ -5,5 +5,6 @@
|
||||
namespace hack::exception_title
|
||||
{
|
||||
const std::string NO_VALID_DATA { "no valid data" };
|
||||
const std::string NO_VALID_SIZE { "no valid size" };
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include "hack/exception/exception.hpp"
|
||||
|
||||
namespace hack::patterns
|
||||
{
|
||||
template<typename T>
|
||||
@ -31,6 +33,15 @@ namespace hack::patterns
|
||||
// т.к. может нужно положить только часть из переданного массива
|
||||
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];
|
||||
@ -41,12 +52,7 @@ namespace hack::patterns
|
||||
// если знаем, что нужно класть весь массив
|
||||
void put(const std::vector<T>& source)
|
||||
{
|
||||
MUTEX lock(m_mutex);
|
||||
for (std::size_t i = 0; i < source.size(); ++i)
|
||||
{
|
||||
m_data[m_head] = source[i];
|
||||
head_refresh();
|
||||
}
|
||||
put(source, source.size());
|
||||
}
|
||||
|
||||
std::optional<T> get() noexcept
|
||||
@ -62,11 +68,30 @@ namespace hack::patterns
|
||||
return val;
|
||||
}
|
||||
|
||||
void peek(std::vector<T>& d, int n)
|
||||
{
|
||||
if (empty()) return;
|
||||
|
||||
int c = m_tail;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
c = (c + 1) % m_size;;
|
||||
d[i] = m_data[c];
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<T>& get_src() const noexcept
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
void skip(int n)
|
||||
{
|
||||
m_tail += n;
|
||||
while (m_tail >= m_size) m_tail -= m_size;
|
||||
}
|
||||
|
||||
|
||||
bool empty() const noexcept
|
||||
{
|
||||
MUTEX lock(m_mutex);
|
||||
@ -75,13 +100,11 @@ namespace hack::patterns
|
||||
|
||||
std::size_t size() const noexcept
|
||||
{
|
||||
MUTEX lock(m_mutex);
|
||||
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;
|
||||
if (!m_full) s = (m_head >= m_tail) ? m_head - m_tail : m_size - (m_tail - m_head);
|
||||
else s = m_size;
|
||||
|
||||
return s;
|
||||
}
|
||||
@ -113,11 +136,11 @@ namespace hack::patterns
|
||||
|
||||
private:
|
||||
std::size_t m_head{ 0 };
|
||||
bool m_full{ false };
|
||||
std::size_t m_tail{ 0 };
|
||||
int m_size;
|
||||
bool m_full{ false };
|
||||
|
||||
private:
|
||||
int m_size;
|
||||
mutable std::recursive_mutex m_mutex;
|
||||
mutable std::vector<T> m_data;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user