blob: 9701e7faca5e227448fc6d7b76c6045a86f42bdc (
plain)
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
|
#include "circularbuffer.hpp"
#include <algorithm>
namespace caelestia::internal {
CircularBuffer::CircularBuffer(QObject* parent)
: QObject(parent) {}
int CircularBuffer::capacity() const {
return m_capacity;
}
void CircularBuffer::setCapacity(int capacity) {
if (capacity < 0)
capacity = 0;
if (m_capacity == capacity)
return;
const auto old = values();
m_capacity = capacity;
m_data.resize(capacity);
m_data.fill(0.0);
m_head = 0;
m_count = 0;
// Re-push old values, keeping the most recent ones
const auto start = old.size() > capacity ? old.size() - capacity : 0;
for (auto i = start; i < old.size(); ++i) {
m_data[m_head] = old[i];
m_head = (m_head + 1) % m_capacity;
m_count++;
}
emit capacityChanged();
emit countChanged();
emit valuesChanged();
}
int CircularBuffer::count() const {
return m_count;
}
QList<qreal> CircularBuffer::values() const {
QList<qreal> result;
result.reserve(m_count);
for (int i = 0; i < m_count; ++i)
result.append(at(i));
return result;
}
qreal CircularBuffer::maximum() const {
if (m_count == 0)
return 0.0;
qreal maxVal = at(0);
for (int i = 1; i < m_count; ++i)
maxVal = std::max(maxVal, at(i));
return maxVal;
}
void CircularBuffer::push(qreal value) {
if (m_capacity <= 0)
return;
m_data[m_head] = value;
m_head = (m_head + 1) % m_capacity;
if (m_count < m_capacity) {
m_count++;
emit countChanged();
}
emit valuesChanged();
}
void CircularBuffer::clear() {
if (m_count == 0)
return;
m_head = 0;
m_count = 0;
emit countChanged();
emit valuesChanged();
}
qreal CircularBuffer::at(int index) const {
if (index < 0 || index >= m_count)
return 0.0;
const int actualIndex = (m_head - m_count + index + m_capacity) % m_capacity;
return m_data[actualIndex];
}
} // namespace caelestia::internal
|