#include "vector.h"
#include "../domain/trench.h"
#include <exception>
#include "../exceptions/exceptions.h"

template<class T>
Vector<T>::Vector(){
    this->_capacity = 10;
    this->_size = 0;
    this->_data = new T[10];
}

template<class T>
Vector<T>::Vector(int capacity){
    this->_capacity = capacity;
    this->_size = 0;
    this->_data = new T[capacity];
}

template<class T>
Vector<T>::~Vector(){
    for(int i=0;i<this->_size;i++){
        delete this->_data[i];
    }
    this->_size=0;
    delete[] this->_data;
}

template<class T>
Vector<T>::Vector(const Vector<T> &vector){
    _capacity = vector._capacity;
    _size = vector._size;
    _data = new T[_capacity];
    for(int i=0;i<_size;i++){
        _data[i]=vector._data[i];
    }
}

template<class T>
T& Vector<T>::operator[](int index){
    if(index<0 || index>this->_size-1)
    {
        throw VectorException("Index out of bounds");
    }
    return this->_data[index];
}

template<class T>
void Vector<T>::add(T elem){
    if(this->_size==this->_capacity)
        this->_resize();
    this->_data[this->_size]=elem;
    this->_size+=1;
}

template<class T>
void Vector<T>::remove(T elem){
    for(int i=0;i<this->_size;i++){
        if(elem == this->_data[i]){
            this->remove(i);
            return;
        }
    }
    throw VectorException("Element not found");
}

template<class T>
void Vector<T>::remove(int index){
    if(index<0 || index>this->_size-1)
    {
        throw VectorException("Index out of bounds");
    }
    delete this->_data[index];
    for(int i=index;i<this->_size-1;i++){
        this->_data[i]=this->_data[i+1];
    }
    this->_size--;
}

template<class T>
int Vector<T>::get(T elem){
    for(int i=0;i<this->_size;i++){
        if(elem == this->_data[i]){
            return i;
        }
    }
    return -1;
}

template<class T>
int Vector<T>::size(){
    return this->_size;
}

template<class T>
int Vector<T>::capacity(){
    return this->_capacity;
}

template<class T>
void Vector<T>::_resize(){
    this->_capacity*=2;
    T* newData = new T[this->_capacity];
    for(int i=0;i<this->_size;i++){
        newData[i]=this->_data[i];
    }
    delete[] this->_data;
    this->_data=newData;
}

template<class T>
T* Vector<T>::begin(){
    return this->_data;
}

template<class T>
T* Vector<T>::end(){
    return this->_data+this->_size;
}


template class Vector<Trench*>;