blob: 0312a291a5c1ca43d02a6b5503ba5595e160e2d9 (
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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_;
};
|