/**
 * Vector implementation in C++
 * @author: Alexander 'espr1t' Georgiev
 * @created: 4. September, 2012
 *
 * Implements a Vector (also known as Dynamic Array) data structure.
 * Supports the following operations:
 *   -- get element at arbitrary position in O(1)
 *   -- insert element at back in O(1)
 *   -- insert element at front in O(N)
 *   -- remove element from back in O(1)
 *   -- remove element from arbitrary position in O(N)
 *   -- remove all elements in O(N)
 *
 * NOTES:
 * Achieves inserting elements at back with (amortized) O(1) by increasing the
 * size of the reserved memory twice each time it is not sufficient.
 * Since we are using consecutive memory to allow fast access to random elements
 * we *do* need to reserve a new block of memory and copy all elements each time
 * a resize is required. This makes some operations (like inserting or deleting
 * random elements) slower than other data structures.
 */

#ifndef __VECTOR_H__
#define __VECTOR_H__

#include <cassert>
#include <cstddef>

template <class Type> class Vector {
    public:
        // Constructors
        Vector();
        Vector(int size);
        Vector(int size, const Type& value);
        Vector(const Type* array, int size);

        // Copy Constructor
        Vector(const Vector<Type>& other);

        // Assignment Operator
        Vector<Type>& operator = (const Vector<Type>& other);

        // Destructor
        ~Vector();

        // Accessors
        int size() const;
        bool empty() const;
        Type& front() const;
        Type& back() const;
        Type& at(int index) const;
        Type& operator [] (int index) const;

        // Modifiers
        void push_back(const Type& value);
        void push_front(const Type& value);
        void pop_back();
        void pop_front();
        void clear();
        void resize(int size);
        void resize(int size, const Type& value);
        void reserve(int size);
        void insert(int index, const Type& value);
        void erase(int first, int last = -1);

        // Comparison operators
        bool operator < (const Vector<Type>& other) const;
        bool operator > (const Vector<Type>& other) const;
        bool operator <= (const Vector<Type>& other) const;
        bool operator >= (const Vector<Type>& other) const;
        bool operator == (const Vector<Type>& other) const;
        bool operator != (const Vector<Type>& other) const;

    private:
        // Data
        Type* data_;
        int size_, capacity_;

        // Methods
        void init_();
        void clear_();
        void copy_(const Vector<Type>& other);
        void reserve_(int capacity);
        void resize_(int size);
        void resize_(int size, const Type& value);
        void erase_(int first, int last);
        void append_(const Type& value);
        void prepend_(const Type& value);
        int compare_(const Vector<Type>& other) const;
};

// Private methods
template <typename Type>
void Vector<Type>::init_() {
    this->data_ = NULL;
    this->size_ = this->capacity_ = 0;
}

template <typename Type>
void Vector<Type>::clear_() {
    delete[] this->data_;
    this->data_ = NULL;
    this->size_ = this->capacity_ = 0;
}

template <typename Type>
void Vector<Type>::copy_(const Vector<Type>& other) {
    this->clear_();
    this->reserve_(other.size_);
    this->size_ = other.size_;
    for (int i = 0; i < this->size_; i++)
        this->data_[i] = other.data_[i];
}

template <typename Type>
void Vector<Type>::reserve_(int capacity) {
    capacity = (capacity <= 0 ? 1 : capacity);
    if (capacity <= this->capacity_)
        return;

    Type* data = new Type[capacity];
    this->capacity_ = capacity;
    for (int i = 0; i < this->size_; i++)
        data[i] = this->data_[i];
    delete[] this->data_;
    this->data_ = data;
}

template <typename Type>
void Vector<Type>::resize_(int size) {
    if (this->capacity_ < size)
        this->reserve_(size);
    this->size_ = size;
}

template <typename Type>
void Vector<Type>::resize_(int size, const Type& value) {
    if (this->capacity_ < size)
        this->reserve_(size);
    while (this->size_ < size)
        this->data_[this->size_++] = value;
    this->size_ = size; // if smaller
}

template <typename Type>
void Vector<Type>::erase_(int first, int last) {
    first = (first < 0 ? 0 : first);
    last = (last < 0 ? first + 1 : last);
    if (first >= this->size_ || last <= first)
        return;

    for (int i = last; i < this->size_; i++)
        this->data_[first + i - last] = this->data_[i];
    this->size_ -= last - first;
}

template <typename Type>
void Vector<Type>::append_(const Type& value) {
    if (this->size_ >= this->capacity_)
        this->reserve_(this->capacity_ * 2);
    this->data_[this->size_] = value;
    this->size_++;
}

template <typename Type>
void Vector<Type>::prepend_(const Type& value) {
    if (this->size_ >= this->capacity_)
        this->reserve_(this->capacity_ * 2);
    for (int i = this->size_; i > 0; i--)
        this->data_[i] = this->data_[i - 1];
    this->data_[0] = value;
    this->size_++;
}

template <typename Type>
int Vector<Type>::compare_(const Vector<Type>& other) const {
    int minSize = (this->size_ < other.size_ ? this->size_ : other.size_);
    for (int i = 0; i < minSize; i++) {
        if (this->data_[i] < other.data_[i])
            return -1;
        if (other.data_[i] < this->data_[i])
            return +1;
    }
    if (this->size_ != other.size_)
        return this->size_ < other.size_ ? -1 : +1;
    return 0;
}

// Public methods
template <typename Type>
Vector<Type>::Vector() {
    this->init_();
}

template <typename Type>
Vector<Type>::Vector(int size) {
    this->init_();
    this->resize_(size);
}

template <typename Type>
Vector<Type>::Vector(int size, const Type& value) {
    this->init_();
    this->resize_(size, value);
}

template <typename Type>
Vector<Type>::Vector(const Type* array, int size) {
    this->init_();
    this->reserve_(size);
    this->size_ = size;
    for (int i = 0; i < this->size_; i++)
        this->data_[i] = array[i];
}

template <typename Type>
Vector<Type>::Vector(const Vector<Type>& other) {
    this->init_();
    this->copy_(other);
}

template <typename Type>
Vector<Type>& Vector<Type>::operator = (const Vector<Type>& other) {
    if (this != &other)
        this->copy_(other);
    return *this;
}

template <typename Type>
Vector<Type>::~Vector() {
    this->clear_();
}

template <typename Type>
int Vector<Type>::size() const {
    return this->size_;
}

template <typename Type>
bool Vector<Type>::empty() const {
    return !this->size_;
}

template <typename Type>
Type& Vector<Type>::front() const {
    assert(this->size_ > 0);
    return this->data_[0];
}

template <typename Type>
Type& Vector<Type>::back() const {
    assert(this->size_ > 0);
    return this->data_[this->size_ - 1];
}

template <typename Type>
Type& Vector<Type>::at(int index) const {
    assert(index >= 0 && index < this->size_);
    return this->data_[index];
}

template <typename Type>
Type& Vector<Type>::operator [] (int index) const {
    assert(index >= 0 && index < this->size_);
    return this->data_[index];
}

template <typename Type>
void Vector<Type>::push_back(const Type& value) {
    this->append_(value);
}

template <typename Type>
void Vector<Type>::push_front(const Type& value) {
    this->prepend_(value);
}

template <typename Type>
void Vector<Type>::pop_back() {
    this->erase_(this->size_ - 1, this->size_);
}

template <typename Type>
void Vector<Type>::pop_front() {
    this->erase_(0, 1);
}

template <typename Type>
void Vector<Type>::clear() {
    this->clear_();
}

template <typename Type>
void Vector<Type>::resize(int size) {
    this->resize_(size);
}

template <typename Type>
void Vector<Type>::resize(int size, const Type& value) {
    this->resize_(size, value);
}

template <typename Type>
void Vector<Type>::reserve(int capacity) {
    this->reserve_(capacity);
}

template <typename Type>
void Vector<Type>::erase(int first, int last) {
    this->erase_(first, last);
}

template <typename Type>
bool Vector<Type>::operator < (const Vector<Type>& other) const {
    return this->compare_(other) < 0;
}

template <typename Type>
bool Vector<Type>::operator > (const Vector<Type>& other) const {
    return this->compare_(other) > 0;
}

template <typename Type>
bool Vector<Type>::operator <= (const Vector<Type>& other) const {
    return this->compare_(other) <= 0;
}

template <typename Type>
bool Vector<Type>::operator >= (const Vector<Type>& other) const {
    return this->compare_(other) >= 0;
}

template <typename Type>
bool Vector<Type>::operator == (const Vector<Type>& other) const {
    return this->compare_(other) == 0;
}

template <typename Type>
bool Vector<Type>::operator != (const Vector<Type>& other) const {
    return this->compare_(other) != 0;
}

#endif // __VECTOR_H__
