From bc75e7267db7947c67dcccc5187431f8819d1d80 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Tue, 21 Dec 2021 17:30:11 +0800 Subject: [PATCH] no message --- general/src/pattern/ringbuffer.hpp | 110 ++++++++++++++--------------- test/src/cpp11/template.cpp | 41 ++++++++--- 2 files changed, 85 insertions(+), 66 deletions(-) diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index a609757..b62caa6 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -9,7 +9,7 @@ public: T At(uint64_t pos); RingBuffer(uint64_t size); int Add(T *data,uint64_t len); - int Take(T *data,uint64_t len); + int TakeBack(T *data,uint64_t len); int Copy(T *data,uint64_t len); void SetEmpty(); @@ -24,7 +24,7 @@ protected: T *m_data; T *m_data_end; uint64_t m_size; - T *m_tail; + uint32_t m_len; T *m_head; }; @@ -32,13 +32,13 @@ template RingBuffer::RingBuffer(uint64_t size) : m_data(nullptr), m_size(0), - m_head(0), - m_tail(0) + m_head(0) { m_size = size; m_data = new T[size]; m_data_end = &m_data[size - 1]; - m_head = m_tail = m_data; + m_head = m_data; + m_len = 0; } template @@ -48,14 +48,13 @@ RingBuffer::~RingBuffer(){ template void RingBuffer::SetEmpty(){ - this->m_head = this->m_tail; + m_len = 0; } template int RingBuffer::Copy(T *data,uint64_t len){ if(data == nullptr) return -1; - } template @@ -65,20 +64,12 @@ uint32_t RingBuffer::Size(){ template uint32_t RingBuffer::Length(){ - if(m_tail > m_head){ - return uint32_t(m_tail - m_head + 1); - } - if(m_tail == m_head){ - return 0; - } - if(m_head > m_tail){ - return uint32_t(m_data_end - m_head + 1) + uint32_t(m_tail - m_data); // todo unsure - } + return m_len; } template T RingBuffer::At(uint64_t pos){ - if(pos > (this->Length() - 1)){ + if(pos > (m_len - 1)){ return T{}; } T* p = m_head + pos; @@ -93,51 +84,60 @@ int RingBuffer::Add(T *data,uint64_t len){ if(data == nullptr){ return -1; } - int i = 0; - std::cout<<"123 "< m_data_end){ + tail = m_head + m_len - m_size; }else{ - std::cout<<"123"< -int RingBuffer::Take(T *data,uint64_t len){ +int RingBuffer::TakeBack(T *data,uint64_t len){ if(data == nullptr) return -1; - + T *tail = nullptr; + if((m_head + m_len) > m_data_end){ + tail = m_head + m_len - m_size; + }else{ + tail = m_head + m_len; + } + + int i = 0; + for(i = 0;i < len;i++){ + T* tmp = tail; + if(tail > m_data) { + tmp--; + } + if(tail == m_data) { + tmp = m_data_end; + } + if(tmp == m_head) { + (*tail) = data[i]; + break; + } + data[i] = (*tail) ; + tail = tmp; + } + return i; } template @@ -147,6 +147,6 @@ uint32_t RingBuffer::CanReadCount(){ template uint32_t RingBuffer::CanWriteCount(){ - + } #endif diff --git a/test/src/cpp11/template.cpp b/test/src/cpp11/template.cpp index 0b2c600..5ce2296 100644 --- a/test/src/cpp11/template.cpp +++ b/test/src/cpp11/template.cpp @@ -11,6 +11,11 @@ #include "pattern/ringbuffer.hpp" using namespace std; +void clearBuffer(int *dat,int len){ + for(int i = 0;i < len;i++){ + dat[i] = 0; + } +} void TestRingBuffer(){ int in[1024]; for(int i = 0;i < 1024;i ++){ @@ -22,41 +27,55 @@ void TestRingBuffer(){ for(uint32_t i = 0;i < x.Length();i++){ printf("%d ",x.At(i)); } - std::cout<<"\r\n----------------------------------"<