no message
parent
c8da1b73b6
commit
bc75e7267d
|
@ -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<typename T>
|
|||
RingBuffer<T>::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<typename T>
|
||||
|
@ -48,14 +48,13 @@ RingBuffer<T>::~RingBuffer(){
|
|||
|
||||
template<typename T>
|
||||
void RingBuffer<T>::SetEmpty(){
|
||||
this->m_head = this->m_tail;
|
||||
m_len = 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int RingBuffer<T>::Copy(T *data,uint64_t len){
|
||||
if(data == nullptr)
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -65,20 +64,12 @@ uint32_t RingBuffer<T>::Size(){
|
|||
|
||||
template<typename T>
|
||||
uint32_t RingBuffer<T>::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<typename T>
|
||||
T RingBuffer<T>::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<T>::Add(T *data,uint64_t len){
|
|||
if(data == nullptr){
|
||||
return -1;
|
||||
}
|
||||
int i = 0;
|
||||
std::cout<<"123 "<<m_tail<<" " << m_head <<std::endl;
|
||||
if(m_tail == m_head){
|
||||
std::cout<<"123"<<std::endl;
|
||||
*m_tail = data[0];
|
||||
for(i = 1;i < len;i++){
|
||||
T* tmp = m_tail;
|
||||
if(m_tail < m_data_end) {
|
||||
tmp++;
|
||||
}
|
||||
if(m_tail == m_data_end) {
|
||||
tmp = m_data;
|
||||
}
|
||||
if(tmp == m_head) {
|
||||
break;
|
||||
}
|
||||
(*tmp) = data[i];
|
||||
m_tail = tmp;
|
||||
}
|
||||
T *tail = nullptr;
|
||||
if((m_head + m_len) > m_data_end){
|
||||
tail = m_head + m_len - m_size;
|
||||
}else{
|
||||
std::cout<<"123"<<std::endl;
|
||||
for(i = 0;i < len;i++){
|
||||
T* tmp = m_tail;
|
||||
if(m_tail < m_data_end) {
|
||||
tmp++;
|
||||
}
|
||||
if(m_tail == m_data_end) {
|
||||
tmp = m_data;
|
||||
}
|
||||
if(tmp == m_head) {
|
||||
break;
|
||||
}
|
||||
(*tmp) = data[i];
|
||||
m_tail = tmp;
|
||||
}
|
||||
tail = m_head + m_len;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for(i = 0;i < len;i++){
|
||||
T* tmp = tail;
|
||||
if(tail < m_data_end) {
|
||||
tmp++;
|
||||
}
|
||||
if(tail == m_data_end) {
|
||||
tmp = m_data;
|
||||
}
|
||||
if(tmp == m_head) {
|
||||
(*tail) = data[i];
|
||||
break;
|
||||
}
|
||||
(*tail) = data[i];
|
||||
tail = tmp;
|
||||
}
|
||||
m_len += i;
|
||||
return i;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int RingBuffer<T>::Take(T *data,uint64_t len){
|
||||
int RingBuffer<T>::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<typename T>
|
||||
|
@ -147,6 +147,6 @@ uint32_t RingBuffer<T>::CanReadCount(){
|
|||
|
||||
template<typename T>
|
||||
uint32_t RingBuffer<T>::CanWriteCount(){
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -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----------------------------------"<<std::endl;
|
||||
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]);
|
||||
}
|
||||
ret = x.Add(in,516);
|
||||
std::cout<<"add "<<ret<<"\r\n"<<std::endl;
|
||||
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<<"\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<<"\r\n----------------------------------"<<std::endl;
|
||||
clearBuffer(in,1024);
|
||||
|
||||
ret = x.Copy(in,516);
|
||||
std::cout<<"\r\ncopy "<<ret<<"\r\n"<<std::endl;
|
||||
std::cout<<"\r\n----------------------------------"<<std::endl;
|
||||
for(uint32_t i = 0;i < ret;i++){
|
||||
printf("%d ",in[i]);
|
||||
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<<"\r\n----------------------------------"<<std::endl;
|
||||
ret = x.Take(in,516);
|
||||
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<<"\r\n----------------------------------"<<std::endl;
|
||||
ret = x.Take(in,20);
|
||||
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));
|
||||
}
|
||||
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<<"\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));
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
Loading…
Reference in New Issue