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

array_list.h « GcodeProcessorLib - github.com/FormerLurker/ArcWelderLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1befaa67677bba7f49e4923ff5ec6f7c327982c4 (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gcode Processor Library
//
// Tools for parsing gcode and calculating printer state from parsed gcode commands.

//
// Copyright(C) 2021 - 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 array_list
{
public:
	array_list()
	{
		auto_grow_ = true;
		max_size_ = 50;
		front_index_ = 0;
		count_ = 0;
		items_ = new T[max_size_];
	}

	array_list(int max_size)
	{
		auto_grow_ = false;
		max_size_ = max_size;
		front_index_ = 0;
		count_ = 0;
		items_ = new T[max_size];
	}

	virtual ~array_list() {
		delete[] items_;
	}

	void resize(int max_size)
	{
		T* new_items = new T[max_size];
		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;
	}

	inline int get_index_position(int index) const
	{
		int index_position = index + front_index_ + max_size_;
		while (index_position >= max_size_)
		{
			index_position = index_position - max_size_;
		}
		return index_position;
	}

	void push_front(T object)
	{
		if (count_ == max_size_)
		{
			if (auto_grow_)
			{
				resize(max_size_ * 2);
			}
			else {
				throw std::exception();
			}
		}
		//front_index_ = (front_index_ - 1 + max_size_) % max_size_;
		front_index_ -= 1;
		if (front_index_ < 0)
		{
			front_index_ = max_size_ - 1;
		}
		count_++;
		items_[front_index_] = object;
	}

	void push_back(T object)
	{
		if (count_ == max_size_)
		{
			if (auto_grow_)
			{
				resize(max_size_ * 2);
			}
			else {
				throw std::exception();
			}
		}
		int pos = get_index_position(count_);
		items_[pos] = object;
		count_++;
	}

	T& pop_front()
	{
		if (count_ == 0)
		{
			throw std::exception();
		}

		int prev_start = front_index_;

		front_index_ += 1;
		if (front_index_ >= max_size_)
		{
			front_index_ = 0;
		}
		count_--;
		return items_[prev_start];
	}

	T& pop_back()
	{
		if (count_ == 0)
		{
			throw std::exception();
		}
		int pos = get_index_position(count_ - 1);
		count_--;
		return items_[pos];
	}

	T& operator[] (int index) const
	{
		//int opos = get_index_position(index);
		return items_[get_index_position(index)];
	}

	T& get(int index) const
	{
		int opos = get_index_position(index);
		return items_[opos];
	}

	int count() const
	{
		return count_;
	}

	int get_max_size() const
	{
		return max_size_;
	}

	void clear()
	{
		count_ = 0;
		front_index_ = 0;
	}

	void copy(const array_list<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_;
	bool auto_grow_;
};