Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CONTAINER_H
- #define CONTAINER_H
- #include<iostream>
- using namespace std;
- template <class T>
- class aghContainer{
- public:
- virtual ~aghContainer(){};
- virtual bool insert(int index, T const& val){};
- virtual T& at(int index) const{};
- virtual int size(void) const{};
- virtual bool remove(int index){};
- void append(T const& val){
- insert(size(),val);
- }
- void show() const{
- for(int i=0;i<size();i++)
- cout<<at(i)<<" - ";
- cout<<endl;
- }
- bool replace(int index, T const& val){
- if(index<=size() || index<0)
- return false;
- remove(index);
- insert(index,val);
- return true;
- }
- void clear(void){
- while(size())
- remove(0);
- }
- bool isEmpty(void){
- if(size()==0)
- return true;
- return false;
- }
- int indexOf(T const& _value, int _from = 0) const{
- if(_from<0 || _from>=size())
- return -1;
- for(int i=_from;i<size();i++)
- if(at(i)== _value) return i; // zwroc indeks pierwszego napotkanego elementu o zadanej wartosci
- return -1;
- }
- bool contains(T const& _value, int _from = 0) const{
- if(_from<0 || _from>=size())
- return false;
- for(int i=_from;i<size();i++)
- if(at(i)== _value) return true; //zwroc prawde przy znalezieniu pierwszego elementu o zadanej wartosci
- return false;
- }
- aghContainer<T>& operator=(const aghContainer<T> &right){
- if(*this!=right){
- clear(); //usun zawartosc
- for(int i=0;i< right.size(); i++)
- append(right.at(i)); //przekopiuj zawartosc prawego do this
- return *this;
- }
- }
- bool operator==(aghContainer<T> const& right){
- if(size() != right.size()){
- return false;
- }
- for(int i=0;i<size();i++)
- if(at(i)!=right.at(i))
- return false; //zwroc falsz przy pierwszym napotkanym roznym elemencie
- return true;
- }
- bool operator!=(aghContainer<T> const& right){
- return !(*this == right);
- }
- T& operator[](int n) const{return at(n);}
- aghContainer<T>& operator+=(aghContainer<T> const& right){
- for(int i=0;i<right.size();i++)
- append(right.at(i)); //dawanie kolejnych wartości na koniec
- return *this;
- }
- aghContainer<T>& operator+=(T const& element){
- append(element); //dodanie jednej wartosci na koniec
- return *this;
- }
- aghContainer<T>& operator<<(T const& element){
- *this += element; //operator "<<" robi to samo co operator "+="
- return *this;
- }
- aghContainer<T>& operator<<(aghContainer<T> const& right){
- *this +=right; //operator "<<" robi to samo co operator "+="
- return *this;
- }
- friend ostream& operator<<(ostream& output, aghContainer<T> const& right){
- for(int i=0;i<size();i++)
- output<<at(i)<<" "; //kolejne elementy pojemnika beda przedzielone spacjami
- return output;
- }
- };
- #endif
- /---------------------------------------------------
- #ifndef VECTOR_H
- #define VECTOR_H
- #include"aghContainer.h"
- #include"aghException.h"
- template<class T>
- class aghVector: public aghContainer<T>{
- private:
- T* tab; /// tablica
- int length; /// rozmiar tablicy
- public:
- /// \brief konstruktor kopiujacy
- aghVector<T> (const aghContainer<T> &obj){
- tab=new T[obj.size()];
- length=obj.size();
- for(int i=0;i<length;i++)
- at(i)=obj.at(i);
- }
- /// \brief konstruktor domyslny
- aghVector<T> (int _length=0){
- length=_length;
- tab=new T[1];
- }
- /// \brief destruktor
- ~aghVector<T>(){
- delete[] tab;
- tab=NULL;
- length=0;
- }
- //-------------------------------------------------
- /// \brief wstaw element na wybrana pozycje
- /// \param index - gdzie wstawic
- /// \param val - co wstawic
- /// \return true udalo sie wstawic , false nie udalo sie
- bool insert(int index, T const& val){
- if(index < 0) //proba wstawienia na ujemny indeks
- return false;
- if(index >= length){ //wstawianie na indeks wiekszy od rozmiaru
- T* tempTab=new T[length];
- //tablica pomocnicza ktora stanie sie kopia aktualnej
- for(int i=0;i<length;i++)
- tempTab[i]=tab[i];
- delete [] tab;
- tab=new T[index+1];
- tab[index]=val;
- //stworz nowa tablice na o odpowiedniej dlugosci
- for(int i=0;i<length;i++)
- tab[i]=tempTab[i];
- delete [] tempTab;
- length =index+1; //zwieksz dlugosc
- //na tej tablicy znajdzie sie dodany element na odpowiednim indeksie
- }
- else{// index mniejszy od dlugosci
- length++; // zwieksz dlugosc
- T* tempTab=new T[length];
- //stworz tablice pomocnicza
- for(int i=0;i<index;i++)
- tempTab[i]=tab[i];
- tempTab[index]=val;
- //tablica pomocnicza jest tworzona w 2 etapach
- //kopiuj elementy do indeksu - dodaj element z parametru - kopiuj elementy po indeksie
- for(int i=index+1;i<length;i++)
- tempTab[i]=tab[i-1];
- delete [] tab;
- tab=new T[length];
- //skopiuj elementy na aktualna tablice
- for(int i=0;i<length;i++)
- tab[i]=tempTab[i];
- delete [] tempTab;
- }
- return true;
- }
- //-------------------------------------------------
- /// \brief zwroc referencje do indeksu
- /// \param index - ktory indeks
- /// \return zwroc referencje
- T& at(int index) const{
- if(index>=size() || index<0){
- return tab[0];
- throw aghException("Index out of range!",-1);
- }
- return tab[index];
- }
- //-------------------------------------------------
- /// \brief zwroc rozmiar
- /// \return rozmiar
- int size(void) const{
- return length;
- }
- //-------------------------------------------------
- /// \brief usun element o wybranym indeksie
- /// \param index - ktory indeks
- /// \return true da sie usunac - false nie da sie
- bool remove(int index){
- if(index<0 || index>=length)
- return false;
- length--;//zmniejsz rozmiar
- T* tempTab=new T[length];
- //tablica pomocnicza
- for(int i=0;i<index;i++)
- tempTab[i]=tab[i];
- for(int i=index;i<length;i++)
- tempTab[i]=tab[i+1];
- //kopiowanie do tablicy podzielono na 2 etapy
- //przed indeksem i po indeksie , pomijajac sam indeks
- delete [] tab;
- tab=new T[index];
- for(int i=0;i<length;i++)
- tab[i]=tempTab[i];
- //skopiuj do aktualnej tablicy
- delete [] tempTab;
- return true;
- }
- //-------------------------------------------------
- /// \brief operator porownania
- /// \param right obiekt do ktorego przyrownamy
- /// \return zwraca referencje do wskaznika this
- aghVector<T>& operator=(const aghVector<T>& right){
- if(*this!=right){
- for(int i=0;i<size();i++)
- remove(0);
- for(int i=0;i<right.size();i++)
- insert(i,right.at(i));
- return *this;
- }
- }
- //-------------------------------------------------
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement