no message

master
zcy 2021-12-22 13:44:54 +08:00
parent 676dc61a0a
commit 5e4796836c
2 changed files with 95 additions and 20 deletions

View File

@ -10,7 +10,8 @@ public:
RingBuffer(uint64_t size);
int Add(T *data,uint64_t len);
int TakeBack(T *data,uint64_t len);
int Copy(T *data,uint64_t len);
int TakeFront(T *data,uint64_t len);
int CopyBack(T *data,uint64_t len);
void SetEmpty();
uint32_t CanReadCount();
@ -52,9 +53,35 @@ void RingBuffer<T>::SetEmpty(){
}
template<typename T>
int RingBuffer<T>::Copy(T *data,uint64_t len){
int RingBuffer<T>::CopyBack(T *data,uint64_t len){
if(data == nullptr)
return -1;
T *tail = nullptr;
if(m_len == 0)
return 0;
if((m_head + m_len) > m_data_end){
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) {
data[i] = (*tail);
break;
}
data[i] = (*tail) ;
tail = tmp;
}
}
template<typename T>
@ -122,7 +149,7 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
if((m_head + m_len) > m_data_end){
tail = m_head + m_len - m_size;
}else{
tail = m_head + m_len;
tail = m_head + m_len - 1;
}
int i = 0;
@ -134,23 +161,58 @@ int RingBuffer<T>::TakeBack(T *data,uint64_t len){
if(tail == m_data) {
tmp = m_data_end;
}
if(tmp == m_head) {
(*tail) = data[i];
// next is head stop
if((tmp == m_head) || (m_head == tail)) {
data[len - 1 - i] = (*tail);
break;
}
data[len - 1 - i] = (*tail) ;
tail = tmp;
}
m_len-=i;
return i;
}
template<typename T>
int TakeFront(T *data,uint64_t len){
if(data == nullptr)
return -1;
T *tail = nullptr;
if(m_len == 0)
return 0;
if((m_head + m_len) > m_data_end){
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 = 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);
break;
}
data[i] = (*tail) ;
tail = tmp;
}
m_head = tmp;
}
m_len-=i;
return i;
}
template<typename T>
uint32_t RingBuffer<T>::CanReadCount(){
return m_len;
}
template<typename T>
uint32_t RingBuffer<T>::CanWriteCount(){
return m_size - m_len;
}
#endif

View File

@ -25,61 +25,74 @@ void TestRingBuffer(){
int ret = x.Add(in,512);
std::cout<<"add "<<ret<<" x.Length():" << x.Length() << " "<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<x.At(i) <<" ";
}
std::cout<<"\r\n clear x----------------------------------"<<std::endl;
x.SetEmpty();
std::cout<<"\r\n ----------------------------------"<<std::endl;
std::cout<<"add data :"<<std::endl;
for(uint32_t i = 0;i < 516;i++){
printf("%d ",in[i]);
std::cout<<in[i] <<" ";
}
ret = x.Add(in,516);
std::cout<<"after add "<<ret << " length: "<< x.Length()<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<x.At(i) <<" ";
}
std::cout<<"\r\n----------------------------------"<<std::endl;
clearBuffer(in,1024);
ret = x.Add(in,20);
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<x.At(i) <<" ";
}
std::cout<<"\r\n----------------------------------"<<std::endl;
clearBuffer(in,1024);
std::cout<<"before take x: "<< x.Length()<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<x.At(i) <<" ";
}
std::cout<<"\r\n----------------------------------"<<std::endl;
ret = x.TakeBack(in,20);
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < ret;i++){
printf("%d ",in[i]);
std::cout<<in[i] <<" ";
}
std::cout<<"\r\n----------------------------------"<<std::endl;
std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<x.At(i) <<" ";
}
clearBuffer(in,1024);
std::cout<<"\r\n----------------------------------"<<std::endl;
ret = x.TakeBack(in,20);
std::cout<<"take "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < ret;i++){
printf("%d ",in[i]);
std::cout<<in[i] <<" ";
}
std::cout<<"\r\n----------------------------------"<<std::endl;
std::cout<<"after take x: "<<x.Length()<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < x.Length();i++){
printf("%d ",x.At(i));
std::cout<<"----------------------------------"<<"\r\n";
for(int z = 0;z < x.Length();z++){
std::cout<<x.At(z) <<" ";
}
clearBuffer(in,1024);
ret = x.TakeFront(in,496);
std::cout<<"\r\ntake from front "<<ret<<"\r\n"<<std::endl;
for(uint32_t i = 0;i < ret;i++){
std::cout<<in[i] <<" ";
}
std::cout<<"\r\nafter take x: "<<x.Length()<<"\r\n"<<std::endl;
std::cout<<"----------------------------------"<<"\r\n";
for(int z = 0;z < x.Length();z++){
std::cout<<x.At(z) <<" ";
}
fflush(stdout);
getchar();
}
int main() {
std::cout<<"main"<<std::endl;
TestRingBuffer();
}