-
Notifications
You must be signed in to change notification settings - Fork 0
/
cBuffer.h
151 lines (128 loc) · 3.01 KB
/
cBuffer.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef CBUFFER_H
#define CBUFFER_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <cmath>
#include <mutex>
#include <condition_variable>
using namespace std;
template <class T>
class CircularBuffer
{
private:
vector<T> elements;
uint length;
uint ind;
bool full;
uint incrementIndex(uint ind);
uint deincrementIndex(uint ind);
mutex mtx;
condition_variable newDataAvailable;
public:
CircularBuffer<T>(uint length);
void push(T const &);
void printAll();
bool filled();
vector<T> getThree(long int time);
T getClosest(long int time);
T getInterpolated(long int time);
T getLastDataPoint();
};
template <class T>
CircularBuffer<T>::CircularBuffer(uint length) {
this->length = length;
ind = 0;
}
template <class T>
T CircularBuffer<T>::getLastDataPoint() {
return elements[ind];
}
template <class T>
bool CircularBuffer<T>::filled() {
if (elements.size() < length)
return false;
return true;
}
template <class T>
void CircularBuffer<T>::push(T const &elem) {
if (!full) {
elements.push_back(elem);
ind++;
if (this->filled()) {
full = true;
}
} else {
ind = incrementIndex(ind);
elements[ind] = elem;
}
newDataAvailable.notify_all();
}
template <class T>
void CircularBuffer<T>::printAll() {
if (full){
for (int i = 0; i < length; i++){
cout << elements[i] << endl;
}
}
}
template <class T>
uint CircularBuffer<T>::incrementIndex(uint i) {
return (i + 1) % (length);
}
template <class T>
uint CircularBuffer<T>::deincrementIndex(uint i) {
i--;
if (i > 40000000){
i = length - 1;
}
return i;
}
template <class T>
vector<T> CircularBuffer<T>::getThree(long int time) {
bool errorIncreasing = false;
int tmperr = 0;
uint tempInd = ind;
int err = 1000000;
uint foundInd = tempInd;
uint i = 0;
while (!errorIncreasing and i < length){
tmperr = abs(elements[tempInd].time - time);
if (tmperr <= err) {
foundInd = tempInd;
err = tmperr;
} else {
errorIncreasing = true;
}
i++;
tempInd = deincrementIndex(tempInd);
}
vector<T> v;
v.push_back(elements[deincrementIndex(foundInd)]);
v.push_back(elements[foundInd]);
v.push_back(elements[incrementIndex(foundInd)]);
if (v[1].time > v[2].time) {
std::unique_lock<std::mutex> lk(mtx);
newDataAvailable.wait(lk);
v[2] = elements[incrementIndex(foundInd)];
}
#ifdef DEBUG_INTERPOLATION
cout << v[0].time << " " << v[1].time << " " << v[2].time << " " << endl;
#endif
return v;
}
template <class T>
T CircularBuffer<T>::getClosest(long int time) {
return getThree(time)[1].clone();
}
template <class T>
T CircularBuffer<T>::getInterpolated(long int time) {
vector<T> v = getThree(time);
if (time > v[1].time)
return v[1].interpolate(v[2],time);
else
return v[1].interpolate(v[0],time);
}
#endif