summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/Internal/circularbuffer.cpp
diff options
context:
space:
mode:
author2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2026-03-12 22:51:07 +1100
committer2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>2026-03-12 22:51:07 +1100
commit27cb290423ba5c30c1856427609938c230fa9f32 (patch)
tree037e5ede085be567b0dec680eb7a10cb423682bd /plugin/src/Caelestia/Internal/circularbuffer.cpp
parentnotifs: skip markdown parsing for plain text bodies (diff)
downloadcaelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.tar.gz
caelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.tar.bz2
caelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.zip
feat: replace canvas -> c++ component
Also add c++ ring buffer
Diffstat (limited to 'plugin/src/Caelestia/Internal/circularbuffer.cpp')
-rw-r--r--plugin/src/Caelestia/Internal/circularbuffer.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/Internal/circularbuffer.cpp b/plugin/src/Caelestia/Internal/circularbuffer.cpp
new file mode 100644
index 0000000..9701e7f
--- /dev/null
+++ b/plugin/src/Caelestia/Internal/circularbuffer.cpp
@@ -0,0 +1,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