Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- using namespace std;
- class Compare
- {
- public:
- virtual int CompareElements(void* e1, void* e2) = 0;
- };
- class exceptie : public exception
- {
- virtual const char* what() const throw()
- {
- return "Indexul este inafara domeniului!";
- }
- };
- template<class T>
- class ArrayIterator
- {
- private:
- int Current;// mai adaugati si alte date si functii necesare pentru iterator
- T* begin;
- T* end;
- public:
- ArrayIterator()
- {
- }
- ArrayIterator& operator ++ ()
- {
- ArrayIterator x;
- x.Current = this->Current + 1;
- return x;
- }
- ArrayIterator& operator -- ()
- {
- ArrayIterator x;
- x.Current = this->Current - 1;
- return x;
- }
- bool operator= (ArrayIterator<T> &a)
- {
- if (this->Current == a.Current)
- return 1;
- }
- bool operator!=(ArrayIterator<T> &)
- {
- if (this->Current == a.Current)
- return 0;
- }
- T* GetElement();
- };
- template<class T>
- class Array
- {
- private:
- T* List; // lista cu pointeri la obiecte de tipul T*
- int Capacity; // dimensiunea listei de pointeri
- int Size; // cate elemente sunt in lista
- exceptie outofbounds;
- public:
- bool changesizeifnecessary()
- {
- if (Size + 1 >= Capacity)
- {
- Array<T> temp(*this);
- memcpy(temp.List, this->List, Size);
- Capacity += 20;
- List = (T*)malloc(Capacity);
- memcpy(this->List, temp.List, Size);
- return true;
- }
- return false;
- }
- Array()
- {
- Capacity = 0;
- Size = 0;
- }
- // Lista nu e alocata, Capacity si Size = 0
- ~Array()
- {
- delete List;
- }// destructor
- Array(int capacity)
- {
- Capacity = capacity+20;
- List = (T*)malloc(Capacity+20);
- Size = 0;
- }// Lista e alocata cu 'capacity' elemente
- Array(const Array<T> &otherArray)
- {
- if (otherArray.Size > this->Size)
- {
- if(Size==0)
- List = (T*)malloc(otherArray.Capacity);
- Capacity = otherArray.Capacity;
- Size = otherArray.Size;
- }
- for (int i = 0; i < Size; i++)
- List[i] = otherArray.List[i];
- }// constructor de copiere
- T& operator[] (int index)
- {
- try
- {
- if (index < 0 || index >= Size)
- {
- throw outofbounds;
- return *List;
- }
- return *(List + index);
- }
- catch (exception& e)
- {
- cout << "Exception: " << e.what()<<'\n';
- return *List;
- }
- }// arunca exceptie daca index este out of range
- const Array<T>& operator+=(const T &newElem)
- {
- changesizeifnecessary();
- *(this->List+Size) = newElem;
- Size++;
- return *this;
- }// adauga un element de tipul T la sfarsitul listei si returneaza this
- const Array<T>& Insert(int index, const T &newElem)
- {
- changesizeifnecessary();
- Size++;
- memmove(List + index + 1, List + index, Size*sizeof(T));
- this->operator[](index)= newElem;
- return *this;
- }
- // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
- const Array<T>& Insert(int index, const Array<T> otherArray)
- {
- changesizeifnecessary();
- T* templist = (T*)malloc(Capacity);
- memcpy(templist, List+index, Size-index);
- memcpy(List + index, otherArray.List, otherArray.Size);
- memcpy(List + index + otherArray.Size, templist, Size - index);
- Size += otherArray.Size;
- return *this;
- }
- // adauga o lista pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
- const Array<T>& Delete(int index)
- {
- if (this->operator[](index) == *List) return *this;
- memcpy(List + index, List + index + 1, Size - index);
- Size--;
- return *this;
- }// sterge un element de pe pozitia index, returneaza this. Daca index e invalid arunca o exceptie
- bool operator==(const Array<T> &otherArray)
- {
- if (this->Size != otherArray.Size)
- return false;
- for (int i = 0; i < Size; i++)
- if (List[i] != otherArray[i])
- return false;
- return true;
- }
- void Sort()
- {
- }// sorteaza folosind comparatia intre elementele din T
- void Sort(int(*compare)(const T&, const T&)); // sorteaza folosind o functie de comparatie
- void Sort(Compare *comparator); // sorteaza folosind un obiect de comparatie
- // functii de cautare - returneaza pozitia elementului sau -1 daca nu exista
- int BinarySearch(const T& elem); // cauta un element folosind binary search in Array
- int BinarySearch(const T& elem, int(*compare)(const T&, const T&));// cauta un element folosind binary search si o functie de comparatie
- int BinarySearch(const T& elem, Compare *comparator);// cauta un element folosind binary search si un comparator
- int Find(const T& elem); // cauta un element in Array
- int Find(const T& elem, int(*compare)(const T&, const T&));// cauta un element folosind o functie de comparatie
- int Find(const T& elem, Compare *comparator);// cauta un element folosind un comparator
- int GetSize();
- int GetCapacity();
- ArrayIterator<T> GetBeginIterator();
- ArrayIterator<T> GetEndIterator();
- void Print()
- {
- for (int i = 0; i < Size; i++)
- cout << List[i] << " ";
- }
- };
- int main()
- {
- Array<int> arr;
- arr += 2;
- arr += 3;
- arr.Insert(1, 3);
- arr.Delete(2);
- Array<int> arr2(arr);
- arr2.Insert(0, arr);
- arr2.Print();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement