Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/FormerLurker/ArcWelderLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFormerLurker <hochgebe@gmail.com>2020-04-26 01:20:39 +0300
committerFormerLurker <hochgebe@gmail.com>2020-04-26 01:20:39 +0300
commit4fcf89d4995921b89b579d06052df11b66e4879f (patch)
tree390bbfcab87591e802f9ac91ac2d531b4ae53946 /GcodeProcessorLib/circular_buffer.h
Add project files.
Diffstat (limited to 'GcodeProcessorLib/circular_buffer.h')
-rw-r--r--GcodeProcessorLib/circular_buffer.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/GcodeProcessorLib/circular_buffer.h b/GcodeProcessorLib/circular_buffer.h
new file mode 100644
index 0000000..1658b78
--- /dev/null
+++ b/GcodeProcessorLib/circular_buffer.h
@@ -0,0 +1,117 @@
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Gcode Processor Library
+//
+// Tools for parsing gcode and calculating printer state from parsed gcode commands.
+//
+// Copyright(C) 2020 - Brad Hochgesang
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// This program is free software : you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+// GNU Affero General Public License for more details.
+//
+//
+// You can contact the author at the following email address:
+// FormerLurker@pm.me
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+#pragma once
+#include <exception>
+template <typename T>
+class circular_buffer
+{
+public:
+ circular_buffer()
+ {
+ max_size_ = 50;
+ front_index_ = 0;
+ count_ = 0;
+ items_ = new T[max_size_];
+ }
+ circular_buffer(int max_size)
+ {
+ max_size_ = max_size;
+ front_index_ = 0;
+ count_ = 0;
+ items_ = new T[max_size];
+ }
+ virtual ~circular_buffer() {
+ delete[] items_;
+ }
+ void resize(int max_size)
+ {
+ T* new_items = new T[max_size];
+ int count = count_;
+ for (int index = 0; index < count_; index++)
+ {
+ new_items[index] = items_[(front_index_ + index + max_size_) % max_size_];
+ }
+ front_index_ = 0;
+ delete[] items_;
+ items_ = new_items;
+ max_size_ = max_size;
+ }
+ void push_front(T object)
+ {
+ front_index_ = (front_index_ - 1 + max_size_) % max_size_;
+ count_++;
+ items_[front_index_] = object;
+ }
+ T pop_front()
+ {
+ if (count_ == 0)
+ {
+ throw std::exception();
+ }
+
+ int prev_start = front_index_;
+ front_index_ = (front_index_ + 1 + max_size_) % max_size_;
+ count_--;
+ return items_[prev_start];
+ }
+
+ T get(int index)
+ {
+ return items_[(front_index_ + index + max_size_) % max_size_];
+ }
+
+ int count()
+ {
+ return count_;
+
+ }
+ int get_max_size()
+ {
+ return max_size_;
+ }
+ void clear()
+ {
+ count_ = 0;
+ front_index_ = 0;
+ }
+ void copy(const circular_buffer<T>& source)
+ {
+ if (max_size_ < source.max_size_)
+ {
+ resize(source.max_size_);
+ }
+ clear();
+ for (int index = 0; index < source.count_; index++)
+ {
+ items_[index] = source[index];
+ }
+ front_index_ = source.front_index_;
+ count_ = source.count_;
+
+ }
+
+protected:
+ T* items_;
+ int max_size_;
+ int front_index_;
+ int count_;
+}; \ No newline at end of file