#ifndef VECTOR_H #define VECTOR_H #include "core/error_macros.h" template class Vector { public: void push_back(const T &element); void pop_back(); void remove(const int index); void remove_keep_order(const int index); void erase(const T &element); void clear(); bool empty() const; T get(const int index); const T &get(const int index) const; void set(const int index, const T &value); void swap(const int index1, const int index2); void sort_inc(); void sort_dec(); int size() const; int capacity() const; void ensure_capacity(const int capacity); void resize(const int s); void append_array(const Vector &other); int find(const T &val) const; T *dataw(); const T *data() const; const T &operator[](const int index) const; T &operator[](const int index); Vector(); Vector(int prealloc); Vector(int prealloc, int grow_by); ~Vector(); private: T *_data; int _actual_size; int _size; int _grow_by; }; template void Vector::push_back(const T &element) { ensure_capacity(_size + 1); _data[_size++] = element; } template void Vector::pop_back() { if (_size == 0) { return; } --_size; } template void Vector::remove(const int index) { _data[index] = _data[_size - 1]; --_size; } template void Vector::remove_keep_order(const int index) { --_size; for (int i = index; i < _size; ++i) { _data[i] = _data[i + 1]; } } template void Vector::erase(const T &element) { int index = find(element); if (index != -1) { remove(index); } } template void Vector::clear() { _size = 0; } template bool Vector::empty() const { return _size == 0; } template T Vector::get(const int index) { return _data[index]; } template const T &Vector::get(const int index) const { return _data[index]; } template void Vector::set(const int index, const T &value) { _data[index] = value; } template void Vector::swap(const int index1, const int index2) { T e = _data[index1]; _data[index1] = _data[index2]; _data[index2] = e; } template void Vector::sort_inc() { for (int i = 0; i < _size; ++i) { for (int j = i + 1; j < _size; ++j) { if (_data[j] < _data[i]) { swap(i, j); } } } } template void Vector::sort_dec() { for (int i = 0; i < _size; ++i) { for (int j = i + 1; j < _size; ++j) { if (_data[j] > _data[i]) { swap(i, j); } } } } template int Vector::size() const { return _size; } template int Vector::capacity() const { return _actual_size; } template void Vector::ensure_capacity(const int capacity) { if (capacity <= _actual_size) { return; } int tsize = capacity + _grow_by; T *nd = new T[tsize]; if (_data) { for (int i = 0; i < _size; ++i) { nd[i] = _data[i]; } delete[] _data; } _data = nd; _actual_size = tsize; } template void Vector::resize(const int s) { ensure_capacity(s); _size = s; } template void Vector::append_array(const Vector &other) { ensure_capacity(_size + other._size); for (int i = 0; i < other._size; ++i) { _data[_size++] = other._data[i]; } } template int Vector::find(const T &val) const { for (int i = 0; i < _size; ++i) { if (_data[i] == val) { return i; } } return -1; } template T *Vector::dataw() { return _data; } template const T *Vector::data() const { return _data; } template const T &Vector::operator[](const int index) const { return _data[index]; } template T &Vector::operator[](const int index) { return _data[index]; } template Vector::Vector() { _data = nullptr; _actual_size = 0; _size = 0; _grow_by = 100; } template Vector::Vector(int prealloc) { _data = nullptr; _actual_size = 0; _size = 0; _grow_by = 100; ensure_capacity(prealloc); } template Vector::Vector(int prealloc, int grow_by) { _data = nullptr; _actual_size = 0; _size = 0; _grow_by = grow_by; ensure_capacity(prealloc); } template Vector::~Vector() { if (_data) { delete[] _data; _data = nullptr; } } #endif