Bootstrap

循环缓冲区C++的一种实现

之前去腾讯面试的时候被问到的一道题目:实现一个循环缓冲区(不带互斥锁)。仔细一想,其实和循环队列的思想一模一样,还是怪自己数据结构没学好阿(其实我是学通信的,所以最近在恶补)。还是先上代码

头文件如下,CircleBuffer.h

#ifndef _CIRCLE_BUFFER_H_
#define _CIRCLE_BUFFER_H_

//basic data type rename
typedef unsigned char uchar8;
typedef char char8;
typedef unsigned int uint32;
typedef int int32;

//definition of CircleBuffer
class CircleBuffer
{
public:
	CircleBuffer(int sz);//constructor
	~CircleBuffer();//destructor
	void ClearBuffer();//clear the buffer to init state, just modify the indicator
	bool IsBufferFull();//to judge if the buffer is full
	bool IsBufferEmpty();//to judge if the buffer is empty
	uint32 BufferLength();//calculate buffer's length
	uint32 ReadBuffer(uchar8 *data, uint32 len);//read data from the buffer, return the actual data length
	uint32 WriteBuffer(uchar8 *data, uint32 len);//write data into the buffer, return the actual written-in da
;