-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.h
267 lines (243 loc) · 7.13 KB
/
Array.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Control code for Semaphore Signals
*
* Copyright (C) 2021 Daris A Nevil - International Brotherhood of Live Steamers
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DARIS A NEVIL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __Array_h_
#define __Array_h_
#include <Arduino.h>
#include <assert.h>
template <class Type>
class Array
{
public:
Array<Type>() :
m_vector(NULL),
m_vector_elems(0),
m_vector_max(0)
{
}
~Array<Type>()
{
delete [] m_vector;
}
/// Assign the contents of another array to this one by copying elements.
/// \param p_array Reference to another Array of the same type.
/// \returns False on error (such as no memory)
///
bool assign(Array<Type>& p_array)
{
size_t new_size = p_array.size();
clear();
if (!reserve(new_size))
{
return false;
}
for (size_t i = 0; i < new_size; ++i)
{
m_vector[i] = p_array.m_vector[i];
}
return true;
}
/// Assign the contents of a constant C array to this one by copying elements.
/// \param p_array Pointer to a constant array of the same type.
/// \param p_array_size Number of elements in the constant array.
///
void assign(const Type* p_array, size_t p_array_size)
{
clear();
if (!reserve(p_array_size))
{
return false;
}
for (size_t i = 0; i < p_array_size; ++i)
{
m_vector[i] = p_array->m_vector[i];
}
return true;
}
/// Assign a single element of the same type to this Array.
///
void assign(Type& p_elem)
{
clear();
if (!reserve(1))
{
return false;
}
m_vector[0] = p_elem;
return true;
}
/// Access an element in the array.
/// \param p_index The zero-based index number of the element to access
/// \returns A pointer to the Array slot
///
Type& at(size_t p_index)
{
assert(p_index < m_vector_elems);
return m_vector[p_index];
}
Type& operator[](size_t p_index)
{
assert(p_index < m_vector_elems);
return m_vector[p_index];
}
/// \returns A pointer to the last element in the Array.
///
Type& back(void)
{
assert(!empty());
return m_vector[m_vector_elems-1];
}
/// \returns A pointer to the first element in the Array.
///
Type& front(void)
{
assert(!empty());
{
return NULL;
}
return &m_vector[0];
}
/// \returns The size of the storage space currently allocated to the Array.
///
size_t capacity(void) const
{
return m_vector_max;
}
/// Removes all elements from the vector, leaving the container with a size of 0.
///
void clear(void)
{
// Don't delete m_vector
m_vector_elems = 0;
}
/// \returns A direct pointer to the memory array used internally by the vector.
/// Please note this pointer can later become invalidated by further operations
/// on the Array.
///
Type* data(void)
{
return m_vector;
}
/// \returns True if the vector is empty.
///
bool empty(void) const
{
return m_vector_elems == 0;
}
/// Remove the specified element
/// \param p_index The index of the lement to remove.
///
void remove(size_t p_index)
{
assert(p_index < m_vector_elems);
for (size_t i = p_index; i < m_vector_elems-1; ++i)
{
m_vector[i] = m_vector[i+1];
}
--m_vector_elems;
}
/// Removes the last element in the vector.
///
void pop_back(void)
{
if (m_vector_elems)
{
--m_vector_elems;
}
}
/// Removes the first element in the vector.
///
void pop_front(void)
{
if (m_vector_elems)
{
remove(0);
}
}
/// Adds a new element at the end of the vector, after its current last element.
/// \param p_value Reference to the value to add.
/// \returns False on error, such as out of memory.
///
bool push_back(const Type& p_value)
{
if (!reserve(size() + 1))
{
return false;
}
m_vector[m_vector_elems++] = p_value;
}
/// Adds a new element at the front of the vecotr, which becomes the first element.
/// \param p_value Reference to the value to add.
///
bool push_front(const Type& p_value)
{
if (!reserve(size() + 1))
{
return false;
}
for (size_t i = 0; i < m_vector_elems; ++i)
{
m_vector[i+1] = m_vector[i];
}
m_vector[0] = p_value;
++m_vector_elems;
return true;
}
/// Requests that the vector capacity be at least enough to contain p_size elements.
/// \param p_size The minimum capacity requested.
///
static const size_t m_grow_quantum = 16;
bool reserve(size_t p_size)
{
if (p_size < m_vector_max)
{
return true;
}
size_t new_size = m_vector_max + m_grow_quantum;
Type* new_vector = new Type[new_size];
if (!new_vector)
{
return false;
}
for (size_t i = 0; i < m_vector_elems; ++i)
{
new_vector[i] = m_vector[i];
}
delete [] m_vector;
m_vector = new_vector;
m_vector_max = new_size;
return true;
}
/// \returns The number of elements in the vector.
///
size_t size(void) const
{
return m_vector_elems;
}
protected:
Type* m_vector; // Pointer to a vector of Type objects
size_t m_vector_elems; // Number of valid elements in m_vector
size_t m_vector_max; // Maximum number of elements in m_vector
};
#endif // __Array_h_