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,34 +152,33 @@ 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;
} }
int i = 0; int i = 0;
for(i = 0;i < len;i++){ for(i = 0;i < len;i++) {
T* tmp = tail; data[len - 1 - i] = (*tail) ;
if(tail > m_data) { if((tail == m_head)) {
tmp--; i++;
}
if(tail == m_data) {
tmp = m_data_end;
}
// next is head stop
if((tmp == m_head) || (m_head == tail)) {
data[len - 1 - i] = (*tail);
break; break;
} }
data[len - 1 - i] = (*tail) ; if(tail > m_data) {
tail = tmp; tail--;
} }
m_len-=i; if(tail == m_data) {
return i; tail = m_data_end;
}
// next is head stop
}
m_len -= 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;
@ -186,23 +190,21 @@ int TakeFront(T *data,uint64_t len){
tail = m_head + m_len - 1; tail = m_head + m_len - 1;
} }
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++;
} }
m_len-=i; if(m_head == m_data_end) {
m_head = m_data;
}
}
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;
} }