循環隊列

        在WINCE開發中很多時候需要用到循環隊列來緩存數據,比如在串口通信中,可以將接收到的數據流先緩存到循環隊列中,然後再從循環隊列中取出數據做下一步的處理。這樣可以有效的避免解析數據幀時繁瑣的拼接處理。

        爲了方便使用,封裝了一個循環隊列類,具體代碼如下:

        頭文件:

/********************************************************************
filename: 	CircularQueue.h
created:	2011-08-23
author:		firehood

purpose:	實現循環隊列
            隊列可以緩存的數據大小 = 隊列長度 -1
			插入數據時在隊列滿的情況下會自動覆蓋隊列頭部數據
*********************************************************************/
#pragma once

#define DEFAULT_QUEUE_MAX_SIZE  512
class CCircularQueue
{
public:
	// 無參構造函數,默認創建隊列的大小爲512字節
	CCircularQueue(void);
	// 帶參構造函數,創建指定大小的隊列
	CCircularQueue(int nQueueSize);
	~CCircularQueue(void);
public:
	// 向隊列尾部插入一個字節,隊列滿時會自動覆蓋隊列頭部數據
	bool PushBack(unsigned char data);

	// 從隊列頭部取出一個字節
	bool PopFront(unsigned char &data);

	// 向隊列尾部插入指定長度數據,隊列滿時會自動覆蓋隊列頭部數據
	bool PushBack(unsigned char *pData, int size);
	
	// 從隊列頭部取出指定長度數據
	// 返回值:>0 返回實際取出的數據長度; <=0 失敗
	int PopFront(unsigned char *pData, int size);
	
	// 重新調整隊列大小
	bool Resize(int nQueueSize);
	
	// 當前隊列緩存的數據大小
	int Size();
	// 隊列當前剩餘空間大小
	int RemainSize();
	// 隊列是否爲空
	bool IsEmpty();
	// 隊列是否滿
	bool IsFull();
    // 打印數據(測試使用)
	void PrintData();
private:
	unsigned char *m_pQueueBase; // 隊列基址
	int m_QueueSize;             // 隊列大小
	int m_Head, m_Tail;          // 隊列頭尾指針
	CRITICAL_SECTION  m_csRW;    // 讀寫鎖
};

        源文件:

/********************************************************************
filename: 	CircularQueue.cpp
created:	2011-08-23
author:		firehood

purpose:	實現循環隊列
            隊列可以緩存的數據大小 = 隊列長度 -1
			插入數據時在隊列滿的情況下會自動覆蓋隊列頭部數據
*********************************************************************/
#include "stdafx.h"
#include "CircularQueue.h"

CCircularQueue::CCircularQueue(void):
m_pQueueBase(NULL),
m_Head(0),
m_Tail(0),
m_QueueSize(DEFAULT_QUEUE_MAX_SIZE)
{
	m_pQueueBase = new unsigned char[m_QueueSize];
	ASSERT(m_pQueueBase);
	memset(m_pQueueBase, 0, m_QueueSize);
	InitializeCriticalSection(&m_csRW);
}

CCircularQueue::CCircularQueue(int nQueueSize):
m_pQueueBase(NULL),
m_Head(0),
m_Tail(0),
m_QueueSize(nQueueSize)
{
	m_pQueueBase = new unsigned char[m_QueueSize];
	ASSERT(m_pQueueBase);
	memset(m_pQueueBase, 0, m_QueueSize);
	InitializeCriticalSection(&m_csRW);
}

CCircularQueue::~CCircularQueue(void)
{
	if(m_pQueueBase)
	{
		delete[] m_pQueueBase;
		m_pQueueBase = NULL;
	}
	DeleteCriticalSection (&m_csRW);
}

// 向隊列尾部插入一個字節,隊列滿時會自動覆蓋隊列頭部數據
bool CCircularQueue::PushBack(unsigned char data)
{
	if(m_pQueueBase == NULL)
	{
		return false;
	}
	EnterCriticalSection(&m_csRW);

	m_pQueueBase[m_Tail]= data;
	m_Tail =(m_Tail+1) % m_QueueSize;
	if(m_Tail == m_Head)
	{
		m_Head = m_Tail+1;
		printf("Warning:lost data, Queue is full..\n");
	}

	LeaveCriticalSection(&m_csRW);
	return true;
}

// 從隊列頭部取出一個字節
bool CCircularQueue::PopFront(unsigned char &data)
{
	if(m_pQueueBase == NULL)
	{
		return false;
	}
	if(IsEmpty())
	{
		return false;
	}
    EnterCriticalSection(&m_csRW);

	data = m_pQueueBase[m_Head];
	m_Head=(m_Head+1) % m_QueueSize;

	LeaveCriticalSection(&m_csRW);
	return true;
}

// 向隊列尾部插入指定長度數據,隊列滿時會自動覆蓋隊列頭部數據
bool CCircularQueue::PushBack(unsigned char *pData, int size)
{
	if(m_pQueueBase == NULL || pData == NULL)
	{
		return false;
	}
	if(size >= m_QueueSize)
	{
		printf("PushBack failed: DataSize[%d] > MAX_QUEUE_SIZE[%d]\n" , size, (m_QueueSize-1));
		return false;
	}
	EnterCriticalSection(&m_csRW);
	int nRemianSize = RemainSize();
	int nTailToEndSize = m_QueueSize - m_Tail;
	if(size <= nTailToEndSize)
	{
		memcpy(m_pQueueBase+m_Tail, pData, size);
	}
	else
	{
		memcpy(m_pQueueBase+m_Tail, pData, nTailToEndSize);
		memcpy(m_pQueueBase,pData+nTailToEndSize, size-nTailToEndSize);
	}
	m_Tail = (m_Tail+size) % m_QueueSize;
	if(size >= nRemianSize)
	{
		printf("Warning:lost data, Queue is full..\n");
		m_Head = m_Tail+1;
	}
	LeaveCriticalSection(&m_csRW);
	return true;
}

// 從隊列頭部取出指定長度數據
// 返回值:>0 返回實際取出的數據長度; <=0 失敗
int CCircularQueue::PopFront(unsigned char *pData, int size)
{
	if(pData == NULL || m_pQueueBase == NULL)
		return -1;
	
	EnterCriticalSection(&m_csRW);
	int nReadSize = (this->Size()>size) ? size : this->Size();
	int nHeadToEndSize = m_QueueSize - m_Head;
	if(nReadSize <= nHeadToEndSize)
	{
		memcpy(pData, m_pQueueBase+m_Head, nReadSize);
	}
	else
	{
		memcpy(pData,m_pQueueBase+m_Head, nHeadToEndSize);
		memcpy(pData+nHeadToEndSize,m_pQueueBase,nReadSize-nHeadToEndSize);
	}
	m_Head=(m_Head+nReadSize) % m_QueueSize;
	LeaveCriticalSection(&m_csRW);
	return nReadSize;
}

// 當前隊列緩存的數據大小
int CCircularQueue::Size()
{
	return (m_Tail-m_Head+m_QueueSize) % m_QueueSize;
}

// 隊列是否爲空
bool CCircularQueue::IsEmpty()
{
	return (m_Head == m_Tail) ? true : false;
}

// 隊列是否滿 
bool CCircularQueue::IsFull()
{
	return (((m_Tail+1) % m_QueueSize) == m_Head) ? true : false;
}

// 隊列當前剩餘空間大小
int CCircularQueue::RemainSize()
{
	return ((m_QueueSize-1)-Size());
}

// 打印數據
void CCircularQueue::PrintData()
{
	int tempHead, tempTail;
	tempHead = m_Head;
	tempTail = m_Tail;
	printf("************************************************\n");
	printf("CirualarQueue Data:");
    while(tempHead != tempTail)
	{
		printf("0x%02x ", m_pQueueBase[tempHead]);
		tempHead=(tempHead+1) % m_QueueSize;
	}
	printf("\n");
	printf("************************************************\n");
}

// 重新調整隊列大小
bool CCircularQueue::Resize(int nQueueSize)
{
	unsigned char *pNewQueue = NULL;
	// 申請新的隊列空間
	pNewQueue = new unsigned char[nQueueSize];
	if(pNewQueue == NULL) // 申請內存失敗
		return false;
	// 將隊列原有數據拷貝到新隊列中
	int len = PopFront(pNewQueue,nQueueSize);
	// 釋放原來隊列空間
	if(m_pQueueBase)
	{
		delete[] m_pQueueBase;
		m_pQueueBase = NULL;
	}
	// 使用新的隊列空間
    m_pQueueBase = pNewQueue;
	m_QueueSize = nQueueSize;
	m_Head = 0;
	m_Tail = len;
	return true;
}
發佈了52 篇原創文章 · 獲贊 23 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章