-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularbuffer.cpp
82 lines (68 loc) · 1.42 KB
/
circularbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "circularbuffer.h"
CircularBuffer::CircularBuffer(int windowSize)
{
data = new double[windowSize];
dataArr = new double[windowSize];
this->size = windowSize;
this->front = 0;
this->rear = -1;
this->numItems = 0;
}
CircularBuffer::~CircularBuffer()
{
delete[] data;
delete[] dataArr;
}
void CircularBuffer::enqueue(double el)
{
this->rear = (this->rear + 1) % this->size;
this->data[this->rear] = el;
if (this->numItems < this->size)
++this->numItems;
else
this->front = (this->front + 1) % this->size;
updateDataArr();
}
double CircularBuffer::dequeue()
{
if (!this->numItems)
{
return 0;
}
double el = this->data[this->front];
this->data[this->front] = 0;
this->front = (this->front + 1) % this->size;
--this->numItems;
updateDataArr();
return el;
}
void CircularBuffer::printBuffer()
{
if (!this->numItems)
{
std::cout << "Buffer is Empty!\n";
return;
}
int count = 0;
int index;
while (count < this->numItems)
{
index = (this->front + count) % this->size;
std::cout << this->data[index] << " ";
++count;
}
std::cout << std::endl;
}
void CircularBuffer::updateDataArr()
{
int index, count;
for (count = 0; count < this->size; ++count)
{
index = (this->front + count) % this->size;
dataArr[count] = this->data[index];
}
}
bool CircularBuffer::isEmpty()
{
return this->numItems == 0;
}