-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.cpp
64 lines (47 loc) · 1.06 KB
/
Buffer.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
#include "Buffer.h"
// Constructors
Buffer::Buffer(int length, int init){
this->values = new int[length];
this->length = length;
this->init = init;
this->clear();
}
// Getters
int Buffer::calcAverage(){
float sum = 0;
for(int i = 0; i < this->length; i++)
sum += this->values[i];
this->average = sum / this->length;
return this->average;
}
int Buffer::getAverage(){
return this->average;
}
int Buffer::getAt(int position){
return this->values[position];
}
bool Buffer::empty(){
return this->calcAverage() == this->init;
}
int Buffer::size(){
return this->length;
}
// Setters
void Buffer::fill(int value){
for(int i = 0; i < this->length; i++)
this->values[i] = value;
this->calcAverage();
}
void Buffer::insert(int value){
// Move array values
for(int i = this->length -1; i > 0; i--)
this->values[i] = this->values[i - 1];
// Insert new value
this->values[0] = value;
// Update the average variable
this->calcAverage();
}
void Buffer::clear(){
// Clear the Buffer and enable to fill on first insert
this->fill(this->init);
}