Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <exception>
  3. using namespace std;
  4.  
  5. class Compare
  6. {
  7. public:
  8.  
  9.     virtual int CompareElements(void* e1, void* e2) = 0;
  10.  
  11. };
  12.  
  13. class exceptie : public exception
  14. {
  15.     virtual const char* what() const throw()
  16.     {
  17.         return "Indexul este inafara domeniului!";
  18.     }
  19. };
  20.  
  21. template<class T>
  22. class ArrayIterator
  23. {
  24. private:
  25.  
  26.     int Current;// mai adaugati si alte date si functii necesare pentru iterator
  27.     T* begin;
  28.     T* end;
  29. public:
  30.  
  31.     ArrayIterator()
  32.     {
  33.     }
  34.  
  35.     ArrayIterator& operator ++ ()
  36.     {
  37.         ArrayIterator x;
  38.         x.Current = this->Current + 1;
  39.         return x;
  40.     }
  41.  
  42.     ArrayIterator& operator -- ()
  43.     {
  44.         ArrayIterator x;
  45.         x.Current = this->Current - 1;
  46.         return x;
  47.     }
  48.  
  49.     bool operator= (ArrayIterator<T> &a)
  50.     {
  51.         if (this->Current == a.Current)
  52.             return 1;
  53.     }
  54.  
  55.     bool operator!=(ArrayIterator<T> &)
  56.     {
  57.         if (this->Current == a.Current)
  58.             return 0;
  59.     }
  60.  
  61.  
  62.  
  63.     T* GetElement();
  64.  
  65. };
  66.  
  67. template<class T>
  68. class Array
  69. {
  70. private:
  71.  
  72.     T* List; // lista cu pointeri la obiecte de tipul T*
  73.  
  74.     int Capacity; // dimensiunea listei de pointeri
  75.  
  76.     int Size; // cate elemente sunt in lista
  77.    
  78.     exceptie outofbounds;
  79.  
  80. public:
  81.  
  82.     bool changesizeifnecessary()
  83.     {
  84.         if (Size + 1 >= Capacity)
  85.         {
  86.             Array<T> temp(*this);
  87.             memcpy(temp.List, this->List, Size);
  88.             Capacity += 20;
  89.             List = (T*)malloc(Capacity);
  90.             memcpy(this->List, temp.List, Size);
  91.             return true;
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     Array()
  97.     {
  98.         Capacity = 0;
  99.         Size = 0;
  100.     }
  101.     // Lista nu e alocata, Capacity si Size = 0
  102.  
  103.     ~Array()
  104.     {
  105.         delete List;
  106.     }// destructor
  107.  
  108.     Array(int capacity)
  109.     {
  110.         Capacity = capacity+20;
  111.         List = (T*)malloc(Capacity+20);
  112.         Size = 0;
  113.     }// Lista e alocata cu 'capacity' elemente
  114.  
  115.     Array(const Array<T> &otherArray)
  116.     {
  117.         if (otherArray.Size > this->Size)
  118.         {
  119.             if(Size==0)
  120.                 List = (T*)malloc(otherArray.Capacity);
  121.             Capacity = otherArray.Capacity;
  122.             Size = otherArray.Size;
  123.         }
  124.         for (int i = 0; i < Size; i++)
  125.             List[i] = otherArray.List[i];
  126.     }// constructor de copiere
  127.  
  128.    
  129.     T& operator[] (int index)
  130.     {
  131.         try
  132.         {
  133.             if (index < 0 || index >= Size)
  134.             {
  135.                 throw outofbounds;
  136.                 return *List;
  137.             }
  138.  
  139.             return *(List + index);
  140.         }
  141.         catch (exception& e)
  142.         {
  143.             cout << "Exception: " << e.what()<<'\n';
  144.             return *List;
  145.         }
  146.     }// arunca exceptie daca index este out of range
  147.  
  148.  
  149.     const Array<T>& operator+=(const T &newElem)
  150.     {
  151.         changesizeifnecessary();
  152.        
  153.         *(this->List+Size) = newElem;
  154.         Size++;
  155.         return *this;
  156.  
  157.     }// adauga un element de tipul T la sfarsitul listei si returneaza this
  158.  
  159.     const Array<T>& Insert(int index, const T &newElem)
  160.     {
  161.         changesizeifnecessary();
  162.         Size++;
  163.         memmove(List + index + 1, List + index, Size*sizeof(T));
  164.         this->operator[](index)= newElem;
  165.         return *this;
  166.     }
  167.     // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
  168.  
  169.     const Array<T>& Insert(int index, const Array<T> otherArray)
  170.     {
  171.         changesizeifnecessary();
  172.  
  173.         T* templist = (T*)malloc(Capacity);
  174.        
  175.         memcpy(templist, List+index, Size-index);
  176.         memcpy(List + index, otherArray.List, otherArray.Size);
  177.         memcpy(List + index + otherArray.Size, templist, Size - index);
  178.         Size += otherArray.Size;
  179.         return *this;
  180.     }
  181.     // adauga o lista pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
  182.  
  183.     const Array<T>& Delete(int index)
  184.     {
  185.         if (this->operator[](index) == *List) return *this;
  186.  
  187.         memcpy(List + index, List + index + 1, Size - index);
  188.         Size--;
  189.         return *this;
  190.     }// sterge un element de pe pozitia index, returneaza this. Daca index e invalid arunca o exceptie
  191.  
  192.     bool operator==(const Array<T> &otherArray)
  193.     {
  194.         if (this->Size != otherArray.Size)
  195.             return false;
  196.  
  197.         for (int i = 0; i < Size; i++)
  198.             if (List[i] != otherArray[i])
  199.                 return false;
  200.  
  201.         return true;
  202.     }
  203.  
  204.  
  205.     void Sort()
  206.     {
  207.     }// sorteaza folosind comparatia intre elementele din T
  208.  
  209.     void Sort(int(*compare)(const T&, const T&)); // sorteaza folosind o functie de comparatie
  210.  
  211.     void Sort(Compare *comparator); // sorteaza folosind un obiect de comparatie
  212.  
  213.  
  214.                                     // functii de cautare - returneaza pozitia elementului sau -1 daca nu exista
  215.  
  216.     int BinarySearch(const T& elem); // cauta un element folosind binary search in Array
  217.  
  218.     int BinarySearch(const T& elem, int(*compare)(const T&, const T&));//  cauta un element folosind binary search si o functie de comparatie
  219.  
  220.     int BinarySearch(const T& elem, Compare *comparator);//  cauta un element folosind binary search si un comparator
  221.  
  222.  
  223.     int Find(const T& elem); // cauta un element in Array
  224.  
  225.     int Find(const T& elem, int(*compare)(const T&, const T&));//  cauta un element folosind o functie de comparatie
  226.  
  227.     int Find(const T& elem, Compare *comparator);//  cauta un element folosind un comparator
  228.  
  229.  
  230.     int GetSize();
  231.  
  232.     int GetCapacity();
  233.  
  234.  
  235.     ArrayIterator<T> GetBeginIterator();
  236.  
  237.     ArrayIterator<T> GetEndIterator();
  238.  
  239.     void Print()
  240.     {
  241.         for (int i = 0; i < Size; i++)
  242.             cout << List[i] << " ";
  243.     }
  244.  
  245. };
  246.  
  247.  
  248.  
  249. int main()
  250. {
  251.     Array<int> arr;
  252.     arr += 2;
  253.     arr += 3;
  254.     arr.Insert(1, 3);
  255.     arr.Delete(2);
  256.  
  257.     Array<int> arr2(arr);
  258.     arr2.Insert(0, arr);
  259.     arr2.Print();
  260.    
  261.    
  262.    
  263.  
  264.     system("pause");
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement