From 72e20c74aeda082ea64032d34a0ae0cbd3e48545 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Wed, 22 Dec 2021 15:06:57 +0800 Subject: [PATCH] no message --- general/src/pattern/ringbuffer.hpp | 62 +++++++++++++++--------------- test/src/cpp11/template.cpp | 1 + 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/general/src/pattern/ringbuffer.hpp b/general/src/pattern/ringbuffer.hpp index 28385db..c04d1ce 100644 --- a/general/src/pattern/ringbuffer.hpp +++ b/general/src/pattern/ringbuffer.hpp @@ -130,6 +130,7 @@ int RingBuffer::Add(T *data,uint64_t len){ } if(tmp == m_head) { (*tail) = data[i]; + i++; break; } (*tail) = data[i]; @@ -139,6 +140,10 @@ int RingBuffer::Add(T *data,uint64_t len){ return i; } + +/* + +*/ template int RingBuffer::TakeBack(T *data,uint64_t len){ if(data == nullptr) @@ -147,34 +152,33 @@ int RingBuffer::TakeBack(T *data,uint64_t len){ if(m_len == 0) return 0; if((m_head + m_len) > m_data_end){ + std::cout<< 1; tail = m_head + m_len - m_size; }else{ tail = m_head + m_len - 1; } 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; - } - // next is head stop - if((tmp == m_head) || (m_head == tail)) { - data[len - 1 - i] = (*tail); + for(i = 0;i < len;i++) { + data[len - 1 - i] = (*tail) ; + if((tail == m_head)) { + i++; break; } - data[len - 1 - i] = (*tail) ; - tail = tmp; + if(tail > m_data) { + tail--; + } + if(tail == m_data) { + tail = m_data_end; + } + // next is head stop } - m_len-=i; - return i; + m_len -= i; + return i ; } template -int TakeFront(T *data,uint64_t len){ +int RingBuffer::TakeFront(T *data,uint64_t len){ if(data == nullptr) return -1; T *tail = nullptr; @@ -186,23 +190,21 @@ int TakeFront(T *data,uint64_t len){ tail = m_head + m_len - 1; } int i = 0; - for(i = 0;i < len;i++){ - T* tmp = m_head; - if(m_head < m_data_end) { - tmp++; - } - if(m_head == m_data_end) { - tmp = m_data; - } - // next is head stop - if((tmp == tail) || (m_head == tail)) { - data[i] = (*tail); + for(i = 0;i < len;i++) { + data[i] = (*m_head); + if(m_head == tail) { + std::cout<<"equal"<