#include <assert.h>
#include <type_traits>
namespace ximo { template <class T> class vector { public: typedef T value_type; typedef value_type* iterator; typedef const value_type* const_iterator; typedef value_type& reference; typedef const value_type& const_reference; typedef size_t size_type;
private: iterator _start; iterator _finish; iterator _end_of_storage;
public: vector() : _start(0), _finish(0), _end_of_storage(0) {} vector(size_type n, const T& val = T()) : _start(0), _finish(0), _end_of_storage(0) { _initialize_aux(n, val); } template <class InputIterator> vector(InputIterator first, InputIterator last) : _start(0), _finish(0), _end_of_storage(0) { typedef typename std::is_integral<InputIterator> Integral; _initialize_aux(first, last, Integral()); }
private: template <class Integer> void _initialize_aux(Integer n, Integer val, std::true_type) { reserve(n); while (n--) { *_finish = val; ++_finish; } }
template <class InputIterator> void _initialize_aux(InputIterator first, InputIterator last, std::false_type) { assert(first <= last);
int n = 0; for (auto it = first; it != last; ++it, ++n) ;
reserve(n); for (; first != last; ++first) { push_back(*first); } }
public: vector(const vector<T>& v) : _start(0), _finish(0), _end_of_storage(0) { reserve(v.capacity()); for (size_type i = 0; i < v.size(); ++i, ++_finish) { *_finish = v._start[i]; } }
~vector() { if (_start) { delete[] _start; _start = _finish = _end_of_storage = 0; } }
vector<T>& operator=(const vector<T>& v) { if (&v != this) { clear(); reserve(v.capacity()); for (size_type i = 0; i < v.size(); ++i, ++_finish) { *_finish = v._start[i]; } } return *this; }
void assign(size_type n, const T& val) { _fill_assign(n, val); } template <class InputIterator> void assign(InputIterator first, InputIterator last) { typedef typename std::is_integral<InputIterator> Integral; _assign_dispatch(first, last, Integral()); }
private: template <class Integer> void _assign_dispatch(Integer n, Integer val, std::true_type) { _fill_assign((size_type)n, (T)val); }
template <class InputIterator> void _assign_dispatch(InputIterator first, InputIterator last, std::false_type) { assert(first <= last);
int n = 0; for (auto it = first; it != last; ++it, ++n) ;
clear(); reserve(n); for (; first != last; ++first, ++_finish) { *_finish = *first; } }
void _fill_assign(size_type n, const T& val) { if (n > capacity()) { vector<T> tmp(n, val); swap(tmp); } else { for (size_type i = 0; i < n; ++i) { _start[i] = val; } _finish = _start + n; } }
public: reference at(size_type n) { assert(n < size()); return (*this)[n]; } const_reference at(size_type n) const { assert(n < size()); return (*this)[n]; }
reference operator[](size_type n) { return *(begin() + n); } const_reference operator[](size_type n) const { return *(begin() + n); }
reference front() { return *begin(); } const_reference front() const { return *begin(); }
reference back() { return *(end() - 1); } const_reference back() const { return *(end() - 1); }
iterator begin() { return _start; } const_iterator begin() const { return _start; }
iterator end() { return _finish; } const_iterator end() const { return _finish; }
bool empty() const { return begin() == end(); }
size_type size() const { return size_type(end() - begin()); }
size_type max_size() const { return size_type(-1) / sizeof(T); }
void reserve(size_type n) { if (n > capacity()) { const size_type old_size = size(); iterator tmp = new T[n]; if (_start) { for (size_type i = 0; i < old_size; ++i) { tmp[i] = _start[i]; } delete[] _start; } _start = tmp; _finish = tmp + old_size; _end_of_storage = _start + n; } } size_type capacity() const { return size_type(_end_of_storage - begin()); }
void clear() { _finish = _start; }
iterator insert(iterator position, const T& val = T()) { assert(position >= _start && position <= _finish);
if (_finish == _end_of_storage) { const size_type len = position - begin(); const size_type old_size = size(); const size_type new_capacity = old_size != 0 ? 2 * old_size : 1; reserve(new_capacity); position = _start + len; } iterator cur = _finish; while (cur != position) { *cur = *(cur - 1); --cur; } *position = val; ++_finish; return position; } void insert(iterator position, size_type n, const T& val) { _fill_insert(position, n, val); } template <class InputIterator> void insert(iterator position, InputIterator first, InputIterator last) { typedef typename std::is_integral<InputIterator> Integral; _insert_dispatch(position, first, last, Integral()); }
private: template <class Integer> void _insert_dispatch(iterator position, Integer n, Integer val, std::true_type) { _fill_insert(position, (size_type)n, (T)val); }
template <class InputIterator> void _insert_dispatch(iterator position, InputIterator first, InputIterator last, std::false_type) { assert(first <= last);
for (; first != last; ++first, ++position) { position = insert(position, *first); } }
void _fill_insert(iterator position, size_type n, const T& val) { while (n--) { position = insert(position, val); } }
public: iterator erase(iterator position) { assert(position >= _start && position < _finish);
iterator cur = position; while (cur != _finish - 1) { *cur = *(cur + 1); ++cur; } --_finish; return position; } iterator erase(iterator first, iterator last) { assert(first <= last);
for (; first != last; ++first) { erase(first); } return first; }
void push_back(const T& val = T()) { if (_finish != _end_of_storage) { *_finish = val; ++_finish; } else { insert(end(), val); } }
void pop_back() { if (!empty()) { --_finish; } }
void resize(size_type new_size, const T& val = T()) { if (new_size < size()) { erase(begin() + new_size, end()); } else { insert(end(), new_size - size(), val); } }
void swap(vector<T>& v) { std::swap(_start, v._start); std::swap(_finish, v._finish); std::swap(_end_of_storage, v._end_of_storage); } };
template <class T> inline bool operator==(const vector<T>& x, const vector<T>& y) { return x.size() == y.size() && equal(x.begin(), x.end(), y.begin()); }
template <class T> inline bool operator!=(const vector<T>& x, const vector<T>& y) { return !(x == y); }
template <class T> inline bool operator<(const vector<T>& x, const vector<T>& y) { return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
template <class T> inline bool operator<=(const vector<T>& x, const vector<T>& y) { return !(y < x); }
template <class T> inline bool operator>(const vector<T>& x, const vector<T>& y) { return y < x; }
template <class T> inline bool operator>=(const vector<T>& x, const vector<T>& y) { return !(x < y); }
template <class T> inline void swap(vector<T>& x, vector<T>& y) { x.swap(y); } }
|