-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStack.h
200 lines (168 loc) · 4.13 KB
/
Stack.h
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
193
194
195
196
197
198
199
200
// Stack
// © 2019 Patrick Lafarguette
//
// See https://github.com/zacsketches/Arduino_Vector
// and https://en.wikipedia.org/wiki/Bubble_sort
#ifndef STACK_H
#define STACK_H
#include <Arduino.h>
#ifndef UNUSED
#define UNUSED(X) (void)X
#endif
template<typename T>
void* operator new(size_t size, T* item) {
UNUSED(size);
return item;
}
template<typename T> struct Allocator {
Allocator() {};
T* allocate(unsigned int count) {
return reinterpret_cast<T*>(new char[count * sizeof(T)]);
}
void deallocate(T* item) {
delete[] reinterpret_cast<char*>(item);
}
void create(T* pointer, const T& item) {
new (pointer) T(item);
}
void destroy(T* item) {
item->~T();
}
};
template<class T, class A = Allocator<T> >
class Stack {
private:
unsigned int _count;
unsigned int _capacity;
A _allocator;
T* _items;
Stack(const Stack&);
public:
Stack() :
_count(0), _capacity(0), _items(0) {
}
Stack(const int capacity) :
_count(0) {
reserve(capacity);
}
Stack& operator=(const Stack&);
~Stack() {
for (unsigned int index = 0; index < _count; ++index) {
_allocator.destroy(&_items[index]);
}
}
T& operator[](unsigned int index) {
return _items[index];
}
const T& operator[](unsigned int index) const {
return _items[index];
}
unsigned int count() const {
return _count;
}
unsigned int capacity() const {
return _capacity;
}
void reserve(unsigned int capacity);
void push(const T& item);
void pop();
void clear();
void sort(bool (*Compare)(T& a, T& b));
void dump();
private:
void swap(const unsigned int a, const unsigned int b);
};
template<class T, class A> Stack<T, A>& Stack<T, A>::operator=(const Stack& stack) {
if (this == &stack) {
return *this;
}
if (stack.count() <= _capacity) {
for (unsigned int index = 0; index < stack._count(); ++index) {
_items[index] = stack[index];
}
_count = stack._count();
return *this;
}
T* items = _allocator.allocate(stack.count());
for (unsigned int index = 0; index < stack.count(); ++index) {
_allocator.create(&items[index], stack[index]);
}
for (unsigned int index = 0; index < _count; ++index) {
_allocator.destroy(&_items[index]);
}
_capacity = _count = stack.count();
_items = items;
return *this;
}
template<class T, class A> void Stack<T, A>::reserve(unsigned int capacity) {
if (capacity <= _capacity) {
return;
}
T* pointer = _allocator.allocate(capacity);
for (unsigned int index = 0; index < _count; ++index) {
_allocator.create(&pointer[index], _items[index]);
}
for (unsigned int index = 0; index < _count; ++index) {
_allocator.destroy(&_items[index]);
}
_allocator.deallocate(_items);
_items = pointer;
_capacity = capacity;
}
template<class T, class A>
void Stack<T, A>::push(const T& item) {
if (_capacity == 0) {
reserve(4);
} else if (_count == _capacity) {
reserve(2 * _capacity);
}
_allocator.create(&_items[_count], item);
++_count;
}
template<class T, class A>
void Stack<T, A>::pop() {
if (_count > 0) {
_allocator.destroy(&_items[--_count]);
}
}
template<class T, class A>
void Stack<T, A>::clear() {
for (unsigned int index = 0; index < _count; ++index) {
_allocator.destroy(&_items[index]);
}
_count = 0;
}
template<class T, class A>
void Stack<T, A>::sort(bool (*Compare)(T& a, T& b)) {
if (_count) {
unsigned int last = _count;
do {
unsigned int next = 0;
for (unsigned int index = 1; index < last; ++index) {
if (Compare(_items[index - 1], _items[index])) {
// Swap
swap(index - 1, index);
next = index;
}
}
last = next;
} while (last > 1);
}
}
template<class T, class A>
void Stack<T, A>::swap(const unsigned int a, const unsigned int b) {
T swap = _items[a];
_items[a] = _items[b];
_items[b] = swap;
}
template<class T, class A>
void Stack<T, A>::dump() {
for (unsigned int index = 0; index < _count; ++index) {
Serial.print(index);
Serial.print(" : ");
Serial.println(*_items[index]);
}
Serial.println();
Serial.flush();
}
#endif /* STACK_H */