diff --git a/bin/main.cpp b/bin/main.cpp index d97657c..7608b25 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -13,29 +13,34 @@ auto main(int argc, char *argv[]) -> int // patterns::ring_buffer { hack::patterns::ring_buffer 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 v { 4, 4, 6, 1, 4, 3, 2 }; - std::forward_list 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 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 v { 4, 4, 6, 1, 4, 3, 2 }; + // std::forward_list 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)); + // } } diff --git a/src/hack/exception/title.hpp b/src/hack/exception/title.hpp index aaa7d09..6303d9b 100644 --- a/src/hack/exception/title.hpp +++ b/src/hack/exception/title.hpp @@ -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" }; } diff --git a/src/hack/patterns/ring_buffer.hpp b/src/hack/patterns/ring_buffer.hpp index 67a4c6b..dbfe127 100644 --- a/src/hack/patterns/ring_buffer.hpp +++ b/src/hack/patterns/ring_buffer.hpp @@ -5,6 +5,8 @@ #include #include +#include "hack/exception/exception.hpp" + namespace hack::patterns { template @@ -31,6 +33,15 @@ namespace hack::patterns // т.к. может нужно положить только часть из переданного массива 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]; @@ -41,12 +52,7 @@ namespace hack::patterns // если знаем, что нужно класть весь массив void put(const std::vector& 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 get() noexcept @@ -62,11 +68,30 @@ namespace hack::patterns return val; } + void peek(std::vector& 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& 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 m_data; };