#ifndef MY_VECTOR
#define MY_VECTOR
#include <iostream>
#include <memory>
#include <algorithm>
#include "Iterator.h"
template <typename T> class Vector {
public:
true
truetypedef std::size_t size_type;
truetypedef Iterator<T> iterator;
truetypedef const Iterator<T> const_iterator;
truetypedef ReIterator<T> reverse_iterator;
truetypedef const ReIterator<T> const_reverse_iterator;
truetypedef signed int difference_type;
truetypedef T value_type;
truetypedef T& reference;
truetypedef const T& const_reference;
true
trueVector(): elements(0), first_free(0), last(0) { }
trueVector(size_type, const T&);
truetemplate <typename Iter> Vector(Iter*, Iter*);
trueexplicit Vector(size_type);
trueVector(const Vector&);
true
trueiterator begin() { return elements; }
trueiterator end() { return first_free; }
truereverse_iterator rbegin() { return first_free - 1; }
truereverse_iterator rend() { return elements - 1; }
trueconst_iterator begin() const { return elements; }
trueconst_iterator end() const { return first_free; }
trueconst_reverse_iterator rbegin() const { return first_free - 1; }
trueconst_reverse_iterator rend() const { return elements - 1; }
true
truevoid push_back(const T&);
truevoid push_front(const T&);
true
truesize_type size() const { return first_free - elements; }
truesize_type max_size() const { return last - elements; }
truebool empty() const { if (elements) return 0; return 1; }
truevoid resize(const size_type);
truevoid resize(const size_type, const T&);
true
trueT& back() const { return *(first_free - 1); }
trueT& front() const { return *elements; }
trueT& operator[](const size_type) { return elements[index]; };
trueconst T& operator[](const size_type) const { return elements[index]; };
trueT& at(const size_type index) { return elements[index]; }
trueconst T& at(const size_type index) const { return elements[index]; }
true
trueiterator erase(iterator);
trueiterator erase(iterator, iterator);
truevoid clear();
truevoid pop_back();
truevoid pop_front();
true
trueVector& operator=(const Vector&);
truevoid swap(Vector&);
truetemplate <typename Iter> void assign(Iter*, Iter*);
truevoid assign(size_type, const T&);
true
truesize_type capacity() const { return last - elements; }
truevoid reserve(const size_t);
true~Vector() { destory(); }
true
truevoid display();
private:
truestatic std::allocator<T> alloc;
truevoid reallocate();
trueT* elements;
trueT* first_free;
trueT* last;
truevoid destory();
truetemplate <typename Iter> void copy_elem(Iter*, Iter*);
};
template <typename T> Vector<T>::Vector(typename Vector<T>::size_type n, const T &t)
{
trueelements = alloc.allocate(n * 3 / 2);
truefirst_free = elements + n;
truelast = elements + n * 3 / 2;
truefor (size_type i = 0; i != n; i++)
truetruealloc.construct(elements + i, t);
}
template <typename T> template <typename Iter> Vector<T>::Vector(Iter *b, Iter *e)
{
trueelements = alloc.allocate((e - b) * 3 / 2);
truefirst_free = elements + (e - b);
truelast = elements + (e - b) * 3 / 2;
truecopy_elem(b, e);
}
template <typename T> Vector<T>::Vector(typename Vector<T>::size_type n)
{
trueelements = alloc.allocate(n * 3 / 2);
truefirst_free = elements + n;
truelast = elements + n * 3 / 2;
truefor (size_type i = 0; i != n; i++)
truetruealloc.construct(elements + i, T());
}
template <typename T> Vector<T>::Vector(const Vector<T> &vec)
{
true
trueelements = alloc.allocate(vec.capacity());
truefirst_free = elements + (vec.size());
truelast = elements + (vec.capacity());
true
truecopy_elem(vec.elements, vec.first_free);
}
template <typename T> void Vector<T>::push_back(const T &t)
{
trueif (first_free == last)
truetruereallocate();
truealloc.construct(first_free, t);
true++first_free;
}
template <typename T> void Vector<T>::push_front(const T &t)
{
trueif (first_free == last)
truetruereallocate();
truealloc.construct(first_free, t);
T tmp = *first_free;
true*first_free = *elements;
true*elements = tmp;
true++first_free;
}
template <typename T> void Vector<T>::resize(const size_type n)
{
truesize_type size = first_free - elements;
trueif (n > last - elements) {
truetrue
truetruewhile (n > last - elements)
truetruetruereallocate();
truetruestd::uninitialized_fill(elements + size, elements + n, T());
true} else if (n > size)
truetruestd::uninitialized_fill(elements + size, elements + n, T());
trueelse {
truetruefor (T *p = first_free; p != elements + n; )
truetruetruealloc.destroy(--p);
true}
truefirst_free = elements + n;
}
template <typename T> void Vector<T>::resize(const size_type n, const T &t)
{
truesize_t size = first_free - elements;
truesize_t capacity = last - elements;
trueif (n > capacity) {
truetrue
truetruewhile (n > last - elements)
truetruetruereallocate();
truetruestd::uninitialized_fill(elements + size, elements + n, t);
true} else if (n > size)
truetruestd::uninitialized_fill(elements + size, elements + n, t);
trueelse {
truetruefor (T *p = first_free; p != elements + n; )
truetruetruealloc.destroy(--p);
true}
truefirst_free = elements + n;
}
template <typename T> typename Vector<T>::iterator Vector<T>::erase(iterator iter)
{
trueVector<T>::iterator back_iter = iter;
truefor ( ; iter != first_free - 1; ++iter)
truetrue*iter = *(iter + 1);
truealloc.destroy(--first_free);
truereturn back_iter;
}
template <typename T> typename Vector<T>::iterator Vector<T>::erase(iterator b, iterator e)
{
trueVector<T>::iterator back_iter = b;
trueVector<T>::iterator old_first_free = first_free;
truefor ( ; e != first_free - 1; ++b, ++e)
truetrue*b = *(e + 1);
truefirst_free = b;
truewhile (b != old_first_free)
truetruealloc.destroy(b++);
truereturn back_iter;
}
template <typename T> void Vector<T>::clear()
{
truefor (T *p = first_free; p != elements; )
truetruealloc.destroy(--p);
truefirst_free = elements;
}
template <typename T> void Vector<T>::pop_back()
{
truealloc.destroy(--first_free);
}
template <typename T> void Vector<T>::pop_front()
{
truefor (T *p = elements; p != first_free - 1; ++p)
truetrue*p = *(p + 1);
true--first_free;
}
template <typename T> void Vector<T>::swap(Vector<T> &vec)
{
trueVector::iterator tmp_elements = elements;
trueVector::iterator tmp_first_free = first_free;
trueVector::iterator tmp_last = last;
trueelements = vec.elements;
truefirst_free = vec.first_free;
truelast = vec.last;
truevec.elements = tmp_elements;
truevec.first_free = tmp_first_free;
truevec.last = tmp_last;
}
template <typename T> template <typename Iter> void Vector<T>::assign(Iter* b, Iter* e)
{
truetypename Vector<T>::size_type n = e - b;
truewhile (last - elements < e - b)
truetruereallocate();
trueclear();
truefor (T *p = elements; b != e; ++p, ++b)
truetrue*p = *b;
truefirst_free = elements + n;
}
template <typename T> void Vector<T>::assign(typename Vector<T>::size_type n, const T& t)
{
truewhile (last - elements < n)
truetruereallocate();
trueclear();
truestd::uninitialized_fill(elements, elements + n, t);
truefirst_free = elements + n;
}
template <typename T> void Vector<T>::reserve(const std::size_t capa)
{
truestd::ptrdiff_t size = first_free - elements;
trueT* newelements = alloc.allocate(capa);
trueif (size < capa)
truetruestd::uninitialized_copy(elements, first_free, newelements);
trueelse
truetruestd::uninitialized_copy(elements, elements + capa, newelements);
truefor (T *p = first_free; p != elements; )
truetruealloc.destory(--p);
trueif (elements)
truetruealloc.deallocate(elements, last - elements);
trueelements = newelements;
truefirst_free = elements + std::min((int)size, (int)capa);
truelast = first + capa;
}
template <typename T> void Vector<T>::display()
{
truefor (Vector<T>::iterator iter = begin(); iter != end(); ++iter)
truetruestd::cout << *iter << " ";
truestd::cout << std::endl;
}
template <typename T> Vector<T>& Vector<T>::operator=(const Vector<T> &vec)
{
trueif (this != &vec) {
truetruedestory();
truetrueelements = alloc.allocate(vec.capacity());
truetruefirst_free = elements + vec.size();
truetruelast = elements + vec.capacity();
truetruecopy_elem(vec.elements, vec.first_free);
true}
truereturn *this;
}
template <typename T> std::allocator<T> Vector<T>::alloc;
template <typename T> void Vector<T>::reallocate()
{
truestd::ptrdiff_t capacity = last - elements;
truestd::ptrdiff_t size = last - elements;
true
truestd::ptrdiff_t newcapacity = 2 * std::max(capacity, 1);
trueT* newelements = alloc.allocate(newcapacity);
true
truestd::uninitialized_copy(elements, first_free, newelements);
truefor (T *p = first_free; p != elements; )
truetruealloc.destroy(--p);
trueif (elements)
truetruealloc.deallocate(elements, last - elements);
trueelements = newelements;
truefirst_free = elements + size;
truelast = elements + newcapacity;
}
template <typename T> void Vector<T>::destory()
{
truefor (T *p = first_free; p != elements; )
truetruealloc.destroy(--p);
trueif (elements)
truetruealloc.deallocate(elements, last - elements);
}
template <typename T> template <typename Iter> void Vector<T>::copy_elem(Iter *b, Iter *e)
{
truefor (typename Vector<T>::size_type i = 0; i != e - b; ++i)
truetruealloc.construct(elements + i, *(b + i));
}
#endif