#ifndef RING_BUFFER_H
#define RING_BUFFER_H
template <class T> class RingBuffer
{
public:
RingBuffer():front(NULL), rear(NULL), mCount(0), first(NULL)
{
}
virtual ~RingBuffer()
{
}
bool Init(int count)
{
mCount=count;
if ((front != NULL) && (rear != NULL)) {
return false;
}
first = new rbs();
first->next=NULL;
count--;
rbs *curr = first;
while(0 < count) {
rbs *temp = new rbs();
temp->next=NULL;
curr->next=temp;
curr = temp;
count--;
}
curr->next = first;
return true;
}
bool IsEmpty()
{
if (front==rear)
return true;
return false;
}
bool IsFull()
{
if (front == NULL)
return false;
if ((rear == NULL) && (front->next == first))
return true;
if (front->next == rear)
return true;
return false;
}
bool set(T *value) {
if(IsFull()) return false;
if (front == NULL) {
front=first;
front->val = value;
} else {
front = front->next;
front->val = value;
}
return true;
}
T* get() {
if(IsEmpty()) return false;
T* value = NULL;
if (rear == NULL) {
rear = first;
value = rear->val;
} else {
rear = rear->next;
value = rear->val;
}
return value;
}
private:
typedef struct _rbs
{
T *val;
_rbs *next;
_rbs() : next(NULL), val(NULL)
{
}
}rbs;
rbs *front;
rbs *rear;
int mCount;
rbs *first;
};
#endif //#ifndef CIRCULAR_QUEUE_H
_______________________________________________________________________________
#include <iostream>
#include "RingBuffer.hpp"
#define MAX_COUNT 5
void Log(bool flag, char* msg)
{
if (true == flag)
{
std::cout<<"SUCCESS In "<<msg<<std::endl;
}
else {
std::cout<<"FAILURE In "<<msg<<std::endl;
}
}
int main()
{
RingBuffer<long> rb;
bool flag = false;
int value = 0;
flag = rb.Init(MAX_COUNT);
Log(flag, "Init CQ with Int");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(5);
Log(flag, "set 5");
flag = rb.set(3);
Log(flag, "set 3");
flag = rb.set(4);
Log(flag, "set 4");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(1);
Log(flag, "set 1");
flag = rb.set(2);
Log(flag, "set 2");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(6);
Log(flag, "set 1");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
return 0;
}
#define RING_BUFFER_H
template <class T> class RingBuffer
{
public:
RingBuffer():front(NULL), rear(NULL), mCount(0), first(NULL)
{
}
virtual ~RingBuffer()
{
}
bool Init(int count)
{
mCount=count;
if ((front != NULL) && (rear != NULL)) {
return false;
}
first = new rbs();
first->next=NULL;
count--;
rbs *curr = first;
while(0 < count) {
rbs *temp = new rbs();
temp->next=NULL;
curr->next=temp;
curr = temp;
count--;
}
curr->next = first;
return true;
}
bool IsEmpty()
{
if (front==rear)
return true;
return false;
}
bool IsFull()
{
if (front == NULL)
return false;
if ((rear == NULL) && (front->next == first))
return true;
if (front->next == rear)
return true;
return false;
}
bool set(T *value) {
if(IsFull()) return false;
if (front == NULL) {
front=first;
front->val = value;
} else {
front = front->next;
front->val = value;
}
return true;
}
T* get() {
if(IsEmpty()) return false;
T* value = NULL;
if (rear == NULL) {
rear = first;
value = rear->val;
} else {
rear = rear->next;
value = rear->val;
}
return value;
}
private:
typedef struct _rbs
{
T *val;
_rbs *next;
_rbs() : next(NULL), val(NULL)
{
}
}rbs;
rbs *front;
rbs *rear;
int mCount;
rbs *first;
};
#endif //#ifndef CIRCULAR_QUEUE_H
_______________________________________________________________________________
#include <iostream>
#include "RingBuffer.hpp"
#define MAX_COUNT 5
void Log(bool flag, char* msg)
{
if (true == flag)
{
std::cout<<"SUCCESS In "<<msg<<std::endl;
}
else {
std::cout<<"FAILURE In "<<msg<<std::endl;
}
}
int main()
{
RingBuffer<long> rb;
bool flag = false;
int value = 0;
flag = rb.Init(MAX_COUNT);
Log(flag, "Init CQ with Int");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(5);
Log(flag, "set 5");
flag = rb.set(3);
Log(flag, "set 3");
flag = rb.set(4);
Log(flag, "set 4");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(1);
Log(flag, "set 1");
flag = rb.set(2);
Log(flag, "set 2");
flag = rb.IsEmpty();
Log(flag, "Queue is empty");
flag = rb.IsFull();
Log(flag, "Queue is full");
flag = rb.set(6);
Log(flag, "set 1");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
flag = rb.get(value);
Log(flag, "get ");
return 0;
}
No comments:
Post a Comment