no message

master
zcy 2021-12-22 15:06:57 +08:00
parent 5e4796836c
commit 72e20c74ae
2 changed files with 33 additions and 30 deletions

View File

@ -130,6 +130,7 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
} }
if(tmp == m_head) { if(tmp == m_head) {
(*tail) = data[i]; (*tail) = data[i];
i++;
break; break;
} }
(*tail) = data[i]; (*tail) = data[i];
@ -139,6 +140,10 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
return i; return i;
} }
/*
*/
template<typename T> template<typename T>
int RingBuffer<T>::TakeBack(T *data,uint64_t len){ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
if(data == nullptr) if(data == nullptr)
@ -147,6 +152,7 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
if(m_len == 0) if(m_len == 0)
return 0; return 0;
if((m_head + m_len) > m_data_end){ if((m_head + m_len) > m_data_end){
std::cout<< 1;
tail = m_head + m_len - m_size; tail = m_head + m_len - m_size;
}else{ }else{
tail = m_head + m_len - 1; tail = m_head + m_len - 1;
@ -154,27 +160,25 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
int i = 0; int i = 0;
for(i = 0;i < len;i++) { 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) ; data[len - 1 - i] = (*tail) ;
if((tail == m_head)) {
i++;
break; break;
} }
data[len - 1 - i] = (*tail) ; if(tail > m_data) {
tail = tmp; tail--;
}
if(tail == m_data) {
tail = m_data_end;
}
// next is head stop
} }
m_len -= i; m_len -= i;
return i ; return i ;
} }
template<typename T> template<typename T>
int TakeFront(T *data,uint64_t len){ int RingBuffer<T>::TakeFront(T *data,uint64_t len){
if(data == nullptr) if(data == nullptr)
return -1; return -1;
T *tail = nullptr; T *tail = nullptr;
@ -187,20 +191,18 @@ int TakeFront(T *data,uint64_t len){
} }
int i = 0; int i = 0;
for(i = 0;i < len;i++) { for(i = 0;i < len;i++) {
T* tmp = m_head; data[i] = (*m_head);
if(m_head < m_data_end) { if(m_head == tail) {
tmp++; std::cout<<"equal"<<std::endl;
} i++;
if(m_head == m_data_end) {
tmp = m_data;
}
// next is head stop
if((tmp == tail) || (m_head == tail)) {
data[i] = (*tail);
break; break;
} }
data[i] = (*tail) ; if(m_head < m_data_end) {
m_head = tmp; m_head++;
}
if(m_head == m_data_end) {
m_head = m_data;
}
} }
m_len -= i; m_len -= i;
return i; return i;

View File

@ -18,6 +18,7 @@ void clearBuffer(int *dat,int len){
} }
void TestRingBuffer(){ void TestRingBuffer(){
int in[1024]; int in[1024];
for(int i = 0;i < 1024;i ++){ for(int i = 0;i < 1024;i ++){
in[i] = i; in[i] = i;
} }