2018-10-24 21:08:06 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2018-11-07 02:10:38 -05:00
|
|
|
#include "cpputils.h"
|
2018-10-24 21:08:06 -04:00
|
|
|
|
|
|
|
template <class T>
|
2018-11-08 05:03:59 -05:00
|
|
|
class Vector {
|
2018-10-24 21:08:06 -04:00
|
|
|
public:
|
2018-11-08 05:03:59 -05:00
|
|
|
Vector() : _data(0), _size(0), _capacity(0) {}
|
|
|
|
~Vector() { delete []_data; }
|
2018-10-24 21:08:06 -04:00
|
|
|
|
|
|
|
class iterator {
|
2018-11-08 05:03:59 -05:00
|
|
|
friend class Vector;
|
2018-10-24 21:08:06 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
iterator(T* n= 0): _node(n) {}
|
|
|
|
iterator(const iterator& i): _node(i._node) {}
|
|
|
|
~iterator() {} // Should NOT delete _node
|
|
|
|
|
|
|
|
const T& operator * () const { return (*_node); }
|
|
|
|
T& operator * () { return (*_node); }
|
|
|
|
iterator& operator ++ () {
|
|
|
|
++_node;
|
|
|
|
return (*this);
|
|
|
|
}
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator operator ++ (int) {
|
|
|
|
iterator temp = *this;
|
|
|
|
++_node;
|
|
|
|
return temp;
|
|
|
|
}
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator& operator -- () {
|
|
|
|
--_node;
|
|
|
|
return (*this);
|
|
|
|
}
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator operator -- (int) {
|
|
|
|
iterator temp = *this;
|
|
|
|
--_node;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator operator + (int i) const {
|
|
|
|
iterator temp = *this;
|
|
|
|
temp += i;
|
|
|
|
return temp;
|
|
|
|
}
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator& operator += (int i) {
|
|
|
|
_node += i;
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator& operator = (const iterator& i) {
|
|
|
|
_node = i._node;
|
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator != (const iterator& i) const {
|
|
|
|
return _node != i._node;
|
|
|
|
}
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
bool operator == (const iterator& i) const { return !(*this != i); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* _node;
|
|
|
|
};
|
|
|
|
|
2018-11-08 05:03:59 -05:00
|
|
|
Vector &operator=(const Vector& a) {
|
2018-11-07 04:09:37 -05:00
|
|
|
delete [] _data;
|
|
|
|
_data = nullptr;
|
2018-11-04 03:38:06 -05:00
|
|
|
_size = a._size;
|
|
|
|
_capacity = a._capacity;
|
2018-11-05 14:37:47 -05:00
|
|
|
if (_capacity) {
|
|
|
|
_data = new T[_capacity];
|
|
|
|
for(int i = 0; i < _size; ++i)
|
2018-11-07 04:09:37 -05:00
|
|
|
_data[i] = a[i];
|
2018-11-05 14:37:47 -05:00
|
|
|
}
|
2018-11-04 03:38:06 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-11-08 05:03:59 -05:00
|
|
|
Vector &operator=(Vector&& a) {
|
2018-11-07 04:09:37 -05:00
|
|
|
delete [] _data;
|
|
|
|
_size = a._size;
|
|
|
|
_capacity = a._capacity;
|
|
|
|
_data = a._data;
|
|
|
|
a._size = 0;
|
|
|
|
a._capacity = 0;
|
|
|
|
a._data = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator begin() const { return iterator(_data); }
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
iterator end() const { return iterator(_data + _size); }
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
bool empty() const { return !_size; }
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
size_t size() const { return _size; }
|
|
|
|
|
|
|
|
T& operator [] (size_t i) { return _data[i]; }
|
2018-11-02 23:56:15 -04:00
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
const T& operator [] (size_t i) const { return _data[i]; }
|
|
|
|
|
|
|
|
const T& back() const { return _data[_size - 1]; }
|
|
|
|
|
|
|
|
void push_back(const T& x) {
|
|
|
|
if(_size == _capacity)
|
|
|
|
expand();
|
|
|
|
_data[_size] = x;
|
|
|
|
++_size;
|
|
|
|
}
|
|
|
|
|
2018-11-02 23:56:15 -04:00
|
|
|
void push_back(T&& x) {
|
|
|
|
if(_size == _capacity)
|
|
|
|
expand();
|
2018-11-07 02:10:38 -05:00
|
|
|
_data[_size] = utils::move(x);
|
2018-11-02 23:56:15 -04:00
|
|
|
++_size;
|
|
|
|
}
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
void pop_front() { erase(begin()); }
|
|
|
|
|
|
|
|
void pop_back() { if(_size) --_size; }
|
|
|
|
|
|
|
|
bool erase(iterator pos) {
|
|
|
|
T* d = pos._node;
|
|
|
|
if (_size == 0 || d < _data || d >= _data + _size)
|
|
|
|
return false;
|
|
|
|
for (; d < _data + _size - 1; ++d)
|
2018-11-07 02:10:38 -05:00
|
|
|
*d = utils::move(*(d + 1));
|
2018-10-24 21:08:06 -04:00
|
|
|
--_size;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool erase(const T& x) {
|
|
|
|
for (T* i = _data; i < _data + _size; ++i) {
|
|
|
|
if(*i == x) {
|
|
|
|
erase(iterator(i));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
void clear(bool dealloc = false) {
|
|
|
|
_size = 0;
|
|
|
|
if (dealloc) {
|
|
|
|
_capacity = 0;
|
|
|
|
delete [] _data;
|
|
|
|
_data = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2018-10-24 21:08:06 -04:00
|
|
|
|
2018-11-07 02:10:38 -05:00
|
|
|
void sort() {
|
2018-10-24 21:08:06 -04:00
|
|
|
qsort(_data, _size, sizeof(T), compare);
|
|
|
|
}
|
|
|
|
|
2018-11-03 04:03:11 -04:00
|
|
|
T* data() { return _data; }
|
|
|
|
|
|
|
|
const T* data() const { return _data; }
|
|
|
|
|
2018-10-24 21:08:06 -04:00
|
|
|
// void reserve(size_t n) { ... }
|
|
|
|
// void resize(size_t n) { ... }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* _data;
|
|
|
|
size_t _size; // number of valid elements
|
|
|
|
size_t _capacity; // max number of elements
|
|
|
|
static int(*_cmp)(T&, T&);
|
|
|
|
|
|
|
|
static int compare(const void *a, const void *b) {
|
|
|
|
return _cmp ? _cmp(*((T*) a), *((T*) b)) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void expand() {
|
|
|
|
if (_capacity == 0)
|
|
|
|
_capacity = 1;
|
|
|
|
else
|
|
|
|
_capacity *= 2;
|
|
|
|
T* temp = _data;
|
|
|
|
_data = new T[_capacity];
|
2018-11-04 03:38:06 -05:00
|
|
|
for(int i = 0; i < _size; ++i)
|
2018-11-07 02:10:38 -05:00
|
|
|
_data[i] = utils::move(temp[i]);
|
2018-10-24 21:08:06 -04:00
|
|
|
delete [] temp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2018-11-08 05:03:59 -05:00
|
|
|
int(* Vector<T>::_cmp)(T&, T&) = nullptr;
|