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