-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRingBuffer.cpp
More file actions
92 lines (66 loc) · 1.17 KB
/
Copy pathRingBuffer.cpp
File metadata and controls
92 lines (66 loc) · 1.17 KB
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
83
84
85
86
87
88
89
90
#include <exception>
#include "RingBuffer.h"
RingBuffer::RingBuffer()
{
_data = new int[_length];
}
RingBuffer::RingBuffer(int length)
{
_length = length;
_data = new int[_length];
}
RingBuffer::~RingBuffer()
{
delete[] _data;
}
void RingBuffer::Push(int data)
{
_data[_startPointer] = data;
_startPointer = (_startPointer + 1) % _length;
++_overallSize;
if (_occupiedSpace < _length)
{
++_occupiedSpace;
}
if (_overallSize > _length)
{
_endPointer = (_endPointer + 1) % _length;
}
}
int RingBuffer::Pop()
{
if (_occupiedSpace == 0)
{
throw std::exception("Ring is empty\n");
}
--_occupiedSpace;
--_overallSize;
int temp = _data[_endPointer];
_endPointer = (_endPointer + 1) % _length;
if(!GetOccupiedQuantity())
{
_endPointer = _startPointer;
_overallSize = 0;
}
return temp;
}
int RingBuffer::GetFreeQuantity()
{
return (_length - _occupiedSpace);
}
int RingBuffer::GetOccupiedQuantity()
{
return _occupiedSpace;
}
int RingBuffer::GetTop()
{
if (_overallSize == 0 || _occupiedSpace == 0)
{
throw std::exception("Ring is empty\n");
}
if (_startPointer == 0)
{
return _data[_length - 1];
}
return _data[(_startPointer - 1)];
}