Advertisement
Five_NT

aaaaaaaa

Apr 11th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.04 KB | None | 0 0
  1.  
  2. class Compare
  3. {
  4. public:
  5.     virtual int CompareElements(void* e1, void* e2) = 0;
  6. };
  7.  
  8. class CompareInt :public Compare
  9. {
  10.     int CompareElements(void* e1, void* e2)
  11.     {
  12.         int a = *((int*)e1);
  13.         int b = *((int*)e2);
  14.  
  15.         return a > b ? 1 : (a == b ? 0 : -1);
  16.     }
  17. };
  18.  
  19. template<class T>
  20.  
  21. class ArrayIterator
  22. {
  23. private:
  24.  
  25.     T** Current;
  26.  
  27. public:
  28.     ArrayIterator(T** p) { Current = p; }
  29.     ArrayIterator<T>& operator ++ ()
  30.     {
  31.         Current++;
  32.         return *this;
  33.     }
  34.  
  35.     ArrayIterator<T>& operator -- ()
  36.     {
  37.         Current--;
  38.         return *this;
  39.     }
  40.  
  41.     bool operator= (ArrayIterator<T> & start)
  42.     {
  43.         Current = start.Current;
  44.         return true;
  45.     }
  46.  
  47.     bool operator!=(ArrayIterator<T> &start)
  48.     {
  49.         if (Current != (start.Current + 1))
  50.             return true;
  51.         return false;
  52.     }
  53.  
  54.     T* GetElement() { return *Current; }
  55.  
  56. };
  57.  
  58. template<class T>
  59.  
  60. class Array
  61.  
  62. {
  63.  
  64. private:
  65.  
  66.     T** List; // lista cu pointeri la obiecte de tipul T*
  67.  
  68.     int Capacity; // dimensiunea listei de pointeri
  69.  
  70.     int Size; // cate elemente sunt in lista
  71.  
  72. public:
  73.  
  74.     Array() { Capacity = Size = 0; } // Lista nu e alocata, Capacity si Size = 0
  75.  
  76.     ~Array() {}; // destructor
  77.  
  78.     Array(int capacity)// Lista e alocata cu 'capacity' elemente
  79.     {
  80.         List = new T*[capacity];
  81.         for (int i = 0; i < capacity; i++)
  82.             List[i] = new T;
  83.         Capacity = capacity;
  84.         Size = 0;
  85.     }
  86.  
  87.     Array(const Array<T>& vector)// constructor de copiere
  88.     {
  89.         Capacity = vector.Capacity;
  90.         Size = vector.Size;
  91.         List = new T*[Capacity];
  92.         for (int i = 0; i < Capacity; i++)
  93.             List[i] = new T;
  94.  
  95.         for (int k = 0; k < Size; k++)
  96.             memcpy(List[k], vector.List[k++], sizeof(T));
  97.     }
  98.  
  99.  
  100.  
  101.     T& operator[] (int index) // arunca exceptie daca index este out of range
  102.     {
  103.         if (index < 0 || index >= Size)
  104.             throw "Index out of range";
  105.  
  106.         return *List[index];
  107.     }
  108.  
  109.     const Array<T>& operator+=(const T& element) // adauga un element de tipul T la sfarsitul listei si returneaza this
  110.     {
  111.         if (Size >= Capacity - 1)
  112.             throw "No more space";
  113.  
  114.         memcpy(List[Size++], &element, sizeof(T));
  115.         //List[Size++] = (T*)element;
  116.         return *this;
  117.     }
  118.  
  119.     const Array<T>& Insert(int index, const T& element) // adauga un element pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
  120.     {
  121.         if (index < 0 || index > Size)
  122.             throw std::out_of_range("OoR la Inserare");
  123.  
  124.         if (Size + 1 > Capacity)
  125.             throw "Overflow.";
  126.  
  127.  
  128.         for (int i = Size; i > index; i--)
  129.             memcpy(List[i], List[i - 1], sizeof(T));
  130.  
  131.         memcpy(List[index], &element, sizeof(T));
  132.  
  133.         Size++;
  134.  
  135.         return *this;
  136.     }
  137.  
  138.     const Array<T>& Insert(int index, const Array<T>& elemente) // adauga o lista pe pozitia index, retureaza this. Daca index e invalid arunca o exceptie
  139.     {
  140.         if (index < 0 || index > Size)
  141.             throw std::out_of_range("OoR la Inserare");
  142.  
  143.         if (Size + elemente.Size > Capacity)
  144.             throw "Overflow.";
  145.  
  146.         //0 1 2
  147.         //1 2 3 - 12 11
  148.         //12 11 1 2 3
  149.         // 0  1 2 3 4
  150.         for (int i = (Size + elemente.Size - 1); i > (index + elemente.Size - 1); i--)
  151.             memcpy(List[i], List[i - elemente.Size], sizeof(T));
  152.  
  153.  
  154.         for (int i = 0; i < elemente.Size; i++)
  155.             memcpy(List[i + index], elemente.List[i], sizeof(T));
  156.  
  157.         Size += elemente.Size;
  158.  
  159.         return *this;
  160.     }
  161.  
  162.     const Array<T>& Delete(int index) // sterge un element de pe pozitia index, returneaza this. Daca index e invalid arunca o exceptie
  163.     {
  164.         if (index < 0 || index >= Size)
  165.             throw std::out_of_range("OoR la stergere");
  166.  
  167.         for (int i = index; i < Size; i++)
  168.             memcpy(List[i], List[i + 1], sizeof(T));
  169.  
  170.         Size--;
  171.  
  172.         return *this;
  173.     }
  174.  
  175.  
  176.  
  177.     const Array<T> & operator=(const Array<T> & vector)
  178.     {
  179.         Array<T> arr;
  180.         arr.Capacity = vector.Capacity;
  181.         arr.Size = vector.Size;
  182.         arr.List = new T*[Capacity];
  183.         for (int i = 0; i < Capacity; i++)
  184.             arr.List[i] = new T;
  185.  
  186.         for (int k = 0; k < arr.Size; k++)
  187.             memcpy(arr.List[k], vector.List[k], sizeof(T));
  188.  
  189.         return arr;
  190.     }
  191.  
  192.     bool operator==(const Array<T> &vector)
  193.     {
  194.         if (Size != vector.Size)
  195.             return false;
  196.  
  197.         for (int i = 0; i < Size; i++)
  198.             if (List[i] != vector.List[i])
  199.                 return false;
  200.  
  201.         return true;
  202.     }
  203.  
  204.  
  205.  
  206.     void Sort(); // sorteaza folosind comparatia intre elementele din T
  207.  
  208.     void Sort(int(*compare)(const T&, const T&)); // sorteaza folosind o functie de comparatie
  209.  
  210.     void Sort(Compare *comparator); // sorteaza folosind un obiect de comparatie
  211.  
  212.  
  213.  
  214.                                     // functii de cautare - returneaza pozitia elementului sau -1 daca nu exista
  215.  
  216.     int BinarySearch(const T& element) // cauta un element folosind binary search in Array
  217.     {
  218.         int st, dr, m;
  219.         st = 0;
  220.         dr = Size - 1;
  221.  
  222.         while (st <= dr)
  223.         {
  224.             m = (st + dr) / 2;
  225.             if (element < *List[m])
  226.                 dr = m - 1;
  227.             else if (element > *List[m])
  228.                 st = m + 1;
  229.             else
  230.                 return m;
  231.         }
  232.  
  233.         return -1;
  234.     }
  235.  
  236.     int BinarySearch(const T& element, int(*compare)(const T&, const T&))//  cauta un element folosind binary search si o functie de comparatie
  237.     {
  238.         int st, dr, m, rez;
  239.         st = 0;
  240.         dr = Size - 1;
  241.  
  242.         while (st <= dr)
  243.         {
  244.             m = (st + dr) / 2;
  245.             rez = compare(element, *List[m]);
  246.             if (rez == -1)
  247.                 dr = m - 1;
  248.             else if (rez == 1)
  249.                 st = m + 1;
  250.             else
  251.                 return m;
  252.         }
  253.  
  254.         return -1;
  255.     }
  256.  
  257.     int BinarySearch(const T& element, Compare *comparator)//  cauta un element folosind binary search si un comparator
  258.     {
  259.         int st, dr, m, rez;
  260.         st = 0;
  261.         dr = Size - 1;
  262.  
  263.         while (st <= dr)
  264.         {
  265.             m = (st + dr) / 2;
  266.             rez = comparator->CompareElements((void*)(&element), List[m]);
  267.             if (rez == -1)
  268.                 dr = m - 1;
  269.             else if (rez == 1)
  270.                 st = m + 1;
  271.             else
  272.                 return m;
  273.         }
  274.  
  275.         return -1;
  276.     }
  277.  
  278.  
  279.     //cautare normala, folosind operatorul de comparatie '=='
  280.     int Find(const T& x) // cauta un element in Array - returneaza pozitia
  281.     {
  282.         for (int i = 0; i < Size; i++)
  283.             if (x == *List[i])
  284.                 return i;
  285.         return -1;
  286.     }
  287.  
  288.     //cautare folosind ca argument o functie de comparatie.
  289.     //Trebuie definita - in main - o functie de comparatie pentru fiecare tip(una pentru int, una pentru char etc) si data ca argument din main)
  290.     //CONVENTIE: functia de compare returneaza: 0 - elemente egale, 1 - primul e mai mare, -1 - al doilea e mai mare
  291.     int Find(const T& element, int(*compare)(const T&, const T&))//  cauta un element folosind o functie de comparatie
  292.     {
  293.         for (int i = 0; i < Size; i++)
  294.             if (compare(element, *List[i]) == 0)
  295.                 return i;
  296.         return -1;
  297.     }
  298.  
  299.     //cautare folosind ca argument o clasa de comparatie
  300.     //trebuie definita pentru fiecare TIP o clasa derivata din clasa compare care sa implementeze functia CompareElements
  301.     //CONVENTIE: 0 = elemente egale, 1 = primul mai mare, -1 = al doilea mai mare
  302.     int Find(const T& element, Compare *comparator)//  cauta un element folosind un comparator
  303.     {
  304.         for (int i = 0; i < Size; i++)
  305.             if (comparator->CompareElements((void*)(&element), List[i]) == 0)
  306.                 return i;
  307.         return -1;
  308.     }
  309.  
  310.  
  311.  
  312.     int GetSize()
  313.     {
  314.         return Size;
  315.     }
  316.  
  317.     int GetCapacity()
  318.     {
  319.         return Capacity;
  320.     }
  321.  
  322.  
  323.  
  324.     ArrayIterator<T> GetBeginIterator()
  325.     {
  326.         ArrayIterator<T> it(List);
  327.         return it;
  328.     }
  329.  
  330.     ArrayIterator<T> GetEndIterator()
  331.     {
  332.         ArrayIterator<T> it(List + Size - 1);
  333.         return it;
  334.     }
  335.  
  336. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement