完善ringbuffer
parent
4f742a96df
commit
df54fd094b
Binary file not shown.
|
@ -6,6 +6,7 @@
|
|||
#define GENERAL_SORTER_H
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
template <typename T>
|
||||
class CompareHanlder{
|
||||
|
@ -147,11 +148,7 @@ void PreOrderTraverse(BSTNode<T> *data)
|
|||
template <typename T>
|
||||
int heapsort(T *data,uint64_t len,CompareHanlder<T> handle){
|
||||
buildMaxHeap(data, len); //先建立最大堆,最大堆只能保证根节点比左右两个子树还大
|
||||
|
||||
|
||||
|
||||
// todo
|
||||
}
|
||||
|
||||
|
||||
#endif //GENERAL_SORTER_H
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef __RINGBUFFER__
|
||||
#define __RINGBUFFER__
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
* author : caiyuzheng
|
||||
|
@ -14,6 +15,8 @@ public:
|
|||
int Take(T *data,uint64_t len);
|
||||
uint32_t CanReadCount();
|
||||
uint32_t CanWriteCount();
|
||||
uint32_t Size();
|
||||
|
||||
~RingBuffer();
|
||||
|
||||
protected:
|
||||
|
@ -21,6 +24,7 @@ protected:
|
|||
uint64_t mSize;
|
||||
uint64_t mCurrentTail;
|
||||
uint64_t mCurrentHead;
|
||||
uint16_t mRemain;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -35,12 +39,19 @@ RingBuffer<T>::RingBuffer(uint64_t size) :
|
|||
mCurrentHead(0) {
|
||||
mSize = size;
|
||||
mData = new T[size];
|
||||
mRemain = mSize;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
RingBuffer<T>::~RingBuffer(){
|
||||
delete[] mData;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint32_t RingBuffer<T>::Size(){
|
||||
return mSize;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T RingBuffer<T>::At(uint64_t pos){
|
||||
return mData[pos];
|
||||
|
@ -51,18 +62,57 @@ int RingBuffer<T>::Add(T *data,uint64_t len){
|
|||
if(data == nullptr){
|
||||
return -1;
|
||||
}
|
||||
/// 0->n |start|head|.....|tail|
|
||||
if(mCurrentTail > mCurrentHead){
|
||||
if(mCurrentTail + len > mSize - 1){
|
||||
uint64_t tmp = mCurrentTail + len - mSize;
|
||||
for(uint32_t i = 0;i < len;i++){
|
||||
if(mCurrentTail < mSize){
|
||||
data[mCurrentTail++] = data[len];
|
||||
}
|
||||
// buffer already full
|
||||
if(mRemain == 0){
|
||||
return -2;
|
||||
}
|
||||
uint16_t bytes_write;
|
||||
if(len > mRemain){
|
||||
bytes_write = mRemain;
|
||||
}else{
|
||||
bytes_write = len;
|
||||
}
|
||||
|
||||
if(mCurrentTail == mCurrentHead){
|
||||
/// |start also head also tail|...|end|
|
||||
if(mCurrentHead == 0){
|
||||
memcpy(mData,data,bytes_write);
|
||||
}else{
|
||||
/// |start |...| head also tail|...|end|
|
||||
if(mCurrentTail + bytes_write < mSize){
|
||||
memcpy(&mData[mCurrentTail],data,(bytes_write)*sizeof(T));
|
||||
mCurrentTail += bytes_write;
|
||||
}else{
|
||||
memcpy(&mData[mCurrentTail],data,(mSize - mCurrentTail)*sizeof(T));
|
||||
memcpy(&mData[0],data,(mCurrentTail + bytes_write)%mSize);
|
||||
mCurrentTail = (mCurrentTail + bytes_write)%mSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// |start|head|.....|tail|end|
|
||||
if(mCurrentTail > mCurrentHead){
|
||||
uint16_t off = mCurrentTail + bytes_write;
|
||||
if(off > mSize){
|
||||
memcpy(&mData[mCurrentTail],data,
|
||||
sizeof(T)*(mSize - mCurrentTail));
|
||||
memcpy(mData,data + sizeof(T)*(mSize - mCurrentTail),
|
||||
(bytes_write - (mSize - mCurrentTail))*sizeof(T));
|
||||
}
|
||||
if(off <= mSize){
|
||||
memcpy(&mData[mCurrentTail],data,
|
||||
sizeof(T)*bytes_write);
|
||||
}
|
||||
}
|
||||
/// |start|...|tail|.....|head|...|end|
|
||||
if(mCurrentHead > mCurrentTail){
|
||||
memcpy(&mData[mCurrentTail],data,sizeof(T)*(bytes_write));
|
||||
}
|
||||
mRemain -= bytes_write;
|
||||
return bytes_write;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
int RingBuffer<T>::Take(T *data,uint64_t len){
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ using namespace std;
|
|||
#include "thread_usage.h"
|
||||
#include "threadpool.h"
|
||||
#include "json.h"
|
||||
#include "template.h"
|
||||
|
||||
extern "C"{
|
||||
#include<stdio.h>
|
||||
|
@ -14,11 +15,12 @@ extern "C"{
|
|||
}
|
||||
|
||||
int main(){
|
||||
std::cout<<"test start"<<endl;
|
||||
try{
|
||||
std::cout<<"cpu count is "<<CoreCount()<<std::endl;
|
||||
TestThreadPool();
|
||||
}catch( std::exception e){
|
||||
std::cout<<"exception"<<e.what();
|
||||
}
|
||||
// std::cout<<"test start"<<endl;
|
||||
// try{
|
||||
// std::cout<<"cpu count is "<<CoreCount()<<std::endl;
|
||||
// TestPromiseFutureBefore();
|
||||
// }catch( std::exception e){
|
||||
// std::cout<<"exception"<<e.what();
|
||||
// }
|
||||
TestRingBuffer();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue