#include "wyjatki.h"

#ifndef _WEKTOR_H
#define _WEKTOR_H

const int max_rozmiar = 20;

template <class T>
class Wektor
{
protected:
	int ile_elementow;
	int rozmiar_tablicy;
	T* tablica_elementow;
public:

	Wektor(int r=max_rozmiar);
	Wektor(const Wektor &);
	T &operator[](int i);
	const T &operator[](int i) const { return tablica_elementow[i]; };
	void Dodaj(const T&);
	void Usun(){ if(ile_elementow > 0) ile_elementow-- ; };
	int Rozmiar() const { return rozmiar_tablicy; };
	int IleElementow() const { return ile_elementow; };
	void Wyczysc(){ ile_elementow = 0;};
	~Wektor(){ delete [] tablica_elementow; };

	typedef T* iterator;
	iterator begin(){ return tablica_elementow; }
	iterator end(){ return tablica_elementow+ile_elementow; }
};

template <class T>
Wektor<T>::Wektor(int r):rozmiar_tablicy(r),ile_elementow(0)
{ 
		if (r<0) throw Wyjatek("Rozmiar nie moze byc mienjszy od zera.");
		tablica_elementow =  new T[rozmiar_tablicy];
}

template <class T>
T& Wektor<T>::operator [](int ind)
{
	if(ind <0 || ind >=ile_elementow) throw ZlyIndeksWektora(ind,"Wyjatek - zly indeks.");
	return tablica_elementow[ind]; 
}

template <class T>
void Wektor<T>::Dodaj(const T& t)
{
	if(ile_elementow == rozmiar_tablicy) 
   {
      rozmiar_tablicy *= 2;
	   T* tmp =  new T[rozmiar_tablicy];
	   for(int i=0;i<ile_elementow;i++) tmp[i]=tablica_elementow[i];
	   delete [] tablica_elementow;
	   tablica_elementow = tmp;
   }
   tablica_elementow[ile_elementow]=t;
	ile_elementow++;
}

template<class T>
Wektor<T>::Wektor(const Wektor& w):rozmiar_tablicy(w.rozmiar_tablicy),ile_elementow(w.ile_elementow)
{
	tablica_elementow= new T[rozmiar_tablicy];
	for(int i=0;i<ile_elementow;i++) tablica_elementow[i]=w.tablica_elementow[i];
}

#endif
