containers/Array.hpp cleanup and remove data initialization

This commit is contained in:
Daniel Agar 2019-01-19 13:30:53 -05:00 committed by Lorenz Meier
parent 7008558fac
commit 652c9dfc49
1 changed files with 25 additions and 75 deletions

View File

@ -38,18 +38,13 @@
namespace px4
{
template <typename TYPE, size_t N>
template <class T, size_t N>
class Array
{
typedef TYPE &reference;
typedef const TYPE &const_reference;
typedef TYPE *iterator;
typedef const TYPE *const_iterator;
public:
Array() = default;
bool push_back(const TYPE &x)
bool push_back(const T &x)
{
if (_size == N) {
_overflow = true;
@ -73,84 +68,39 @@ public:
}
}
reference operator[](size_t n)
{
return _items[n];
}
const_reference operator[](size_t n) const
{
return _items[n];
}
reference at(size_t n)
{
return _items[n];
}
const_reference at(size_t n) const
{
return _items[n];
}
size_t size() const
{
return _size;
}
size_t max_size() const
{
return N;
}
size_t capacity() const
{
return N;
}
bool empty() const
{
return _size == 0;
}
bool is_overflowed()
{
return _overflow;
}
iterator begin()
{
return &_items[0];
}
iterator end()
{
return &_items[_size];
}
const_iterator begin() const
{
return &_items[0];
}
const_iterator end() const
{
return &_items[_size];
}
void erase(iterator item)
void erase(T *item)
{
if (item - _items < static_cast<int>(_size)) {
--_size;
for (iterator it = item; it != &_items[_size]; ++it) {
for (T *it = item; it != &_items[_size]; ++it) {
*it = *(it + 1);
}
}
}
T &operator[](size_t n) { return _items[n]; }
const T &operator[](size_t n) const { return _items[n]; }
T &at(size_t n) { return _items[n]; }
const T &at(size_t n) const { return _items[n]; }
size_t size() const { return _size; }
size_t max_size() const { return N; }
size_t capacity() const { return N; }
bool empty() const { return _size == 0; }
bool is_overflowed() { return _overflow; }
T *begin() { return &_items[0]; }
T *end() { return &_items[_size]; }
const T *begin() const { return &_items[0]; }
const T *end() const { return &_items[_size]; }
private:
TYPE _items[N] {};
T _items[N];
size_t _size{0};
bool _overflow{false};
};