Advertisement
meta1211

Array class

Sep 25th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.46 KB | None | 0 0
  1. //Array.h
  2.  
  3. #pragma once
  4. #include <iostream>
  5.  
  6. const enum sortType
  7. {
  8.     bubbleSort = 0,         //Пузырьковая сортировка
  9.     insertionSort = 1,      //Сортировка прямой вставкой
  10.     shellSort = 2,          //Сортировка Шелла
  11.     pyramidSort = 3,        //Пирамидальная сортировка
  12.     quickSort = 4,          //Быстрая сортировка
  13.     byteSort                //Битовая сортировка
  14. };
  15.  
  16. class Array
  17. {
  18. private:
  19.     int *arr;
  20.     int capacity;   //max elements count
  21.  
  22.     void CopyElements(int *, int);
  23.     void CopyElements(int *, int, int);
  24.     void CopyElements(int * a, int * source, int len, int start = 0);
  25.     void Shift(int);
  26.     void Shift(int *, int, int);
  27.     //void Swap(int *, int *);
  28.     bool IsArraysEq(int *, int);
  29.  
  30.     void BubbleSort(int *, int);
  31.     void BubbleSort(int *, int, int, int);
  32.     void InsertionSort(int *, int);
  33.     void ShellSort(int *, int);
  34.     //void MoveElement(int *, int);
  35.     void PyramidSort(int *, int);
  36.     void QuickSort(int *, int, int, int );
  37.     void ByteSort(int *, int, int, int, int);
  38.  
  39. public:
  40.     void Scan(int);
  41.     void Print();
  42.     int Max();
  43.     int Min();
  44.     int Find(int);
  45.     void Sort(sortType);
  46.  
  47.     int & operator [](int);
  48.     Array & operator = (Array &x);
  49.     Array operator += (const int);
  50.     Array operator + (int);
  51.     Array operator += (Array);
  52.     Array operator + (Array);
  53.     Array operator -= (int);
  54.     Array operator -(int);
  55.     bool operator ==(Array);
  56.     bool operator !=(Array);
  57.     friend std::ostream& operator << (std::ostream &r, const Array &x)
  58.     {
  59.         for (int i = 0; i < x.capacity; i++)
  60.         {
  61.             r << x.arr[i] << ' ';
  62.         }
  63.         return r;
  64.     }
  65.     friend std::istream & operator >> (std::istream &r, Array &x)
  66.     {
  67.         int tempNum;
  68.         r >> tempNum;
  69.         x += tempNum;
  70.         return r;
  71.     }
  72.    
  73.  
  74.     Array(int Capacity = 0);
  75.     Array(int *, int);
  76.     Array(const Array &x);
  77.     ~Array();
  78. };
  79.  
  80. //Array.cpp
  81.  
  82. #include <bitset>  
  83. #include "Array.h"
  84. #define INT16_MAX  2147483647
  85.  
  86.  
  87. #pragma region PrivateFunctions
  88.  
  89. void Array::CopyElements(int *source, int len)
  90. {
  91.     for (int i = 0; i < len; i++)
  92.     {
  93.         arr[i] = source[i];
  94.     }
  95. }
  96.  
  97. void Array::CopyElements(int *source, int len, int start)
  98. {
  99.     for (int i = 0; i < len; i++)
  100.     {
  101.         arr[i + start] = source[i];
  102.     }
  103. }
  104.  
  105. void Array::CopyElements(int *a, int *source, int len, int start)
  106. {
  107.     for (int i = 0; i < len; i++)
  108.     {
  109.         a[i + start] = source[i];
  110.     }
  111. }
  112.  
  113. void Array::Shift(int *a, int start, int end)
  114. {
  115.     for (int i = end - 1; i >= start; i--)
  116.     {
  117.         a[i + 1] = a[i];
  118.     }
  119. }
  120.  
  121. void Array::Shift(int index)
  122. {
  123.     for (int i = index; i < capacity; i++)
  124.     {
  125.         arr[i] = arr[i + 1];
  126.     }
  127.     capacity--;
  128. }
  129.  
  130. bool Array::IsArraysEq(int *a, int len)
  131. {
  132.     for (int i = 0; i < len; i++)
  133.     {
  134.         if (arr[i] != a[i])
  135.             return false;
  136.     }
  137.     return true;
  138. }
  139.  
  140. void Swap(int *xp, int *yp)
  141. {
  142.     int temp = *xp;
  143.     *xp = *yp;
  144.     *yp = temp;
  145. }
  146.  
  147. void Array::BubbleSort(int *a, int n)
  148. {
  149.     int i, j;
  150.     bool isSwapped;
  151.     for (i = 0; i < n - 1; i++)
  152.     {
  153.         isSwapped = false;
  154.         for (j = 0; j < n - i - 1; j++)
  155.         {
  156.             if (a[j] > a[j + 1])
  157.             {
  158.                 Swap(&a[j], &a[j + 1]);
  159.                 isSwapped = true;
  160.             }
  161.         }
  162.         if (isSwapped == false)
  163.             break;
  164.     }
  165. }
  166.  
  167. void Array::InsertionSort(int *a, int n)
  168. {
  169.     int curentValue;
  170.     for (int i = 1; i < n; i++)
  171.     {
  172.         for (int j = 0; j < i; j++)
  173.         {
  174.             curentValue = a[i];
  175.             if (a[i] < a[j])
  176.             {
  177.                 Shift(a, j, i);
  178.                 a[j] = curentValue;
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184. void Array::ShellSort(int *a, int n)
  185. {
  186.     int i;
  187.     int value;
  188.     for (int p = n / 2; p >= 0; p--)
  189.     {
  190.         for (int q = 0; q < p; q++)
  191.         {
  192.             for (int j = q + p; j < n; j += p)
  193.             {
  194.                 for (i = j - p, value = a[j]; i >= 0 && a[i] > value; i -= p)
  195.                 {
  196.                     a[i + p] = a[i];
  197.                 }
  198.                 a[i + p] = value;
  199.             }
  200.         }
  201.     }
  202. }
  203.  
  204. void MoveElement(int *a, int n, int index)
  205. {
  206.     int j = 2 * index + 1;
  207.     if (j > n)
  208.         return;
  209.     int value = a[index];
  210.     int i = index;
  211.     if (a[j + 1] > a[j] && j + 1 < n)
  212.     {
  213.         j++;
  214.     }
  215.     while (a[j] > value)
  216.     {
  217.         a[i] = a[j];
  218.         i = j;
  219.         j = 2 * i + 1;
  220.         if (j >= n)
  221.         {
  222.             break;
  223.         }
  224.         if (j + 1 < n && a[j + 1] > a[j])
  225.         {
  226.             j++;
  227.         }
  228.     }
  229.     a[i] = value;
  230. }
  231.  
  232. void BuildPyramid(int *a, int n)
  233. {
  234.     for (int curentIndex = n / 2 - 1; curentIndex >= 0; curentIndex--)
  235.     {
  236.         MoveElement(a, n, curentIndex);
  237.     }
  238. }
  239.  
  240. void Array::PyramidSort(int *a, int n)
  241. {
  242.     BuildPyramid(a, n);
  243.     for (int N = n; N > 0; )
  244.     {
  245.         Swap(&a[0], &a[N - 1]);
  246.         N--;
  247.         MoveElement(a, N, 0);
  248.         //BuildPyramid(a, N);
  249.     }
  250.     Swap(&a[0], &a[1]);
  251. }
  252.  
  253. void Array::BubbleSort(int *a, int n, int start, int end)
  254. {
  255.     int i, j;
  256.     bool isSwapped;
  257.     for (i = start; i < end - 1; i++)
  258.     {
  259.         isSwapped = false;
  260.         for (j = 0; j < n - i - 1; j++)
  261.         {
  262.             if (a[j] > a[j + 1])
  263.             {
  264.                 Swap(&a[j], &a[j + 1]);
  265.                 isSwapped = true;
  266.             }
  267.         }
  268.         if (isSwapped == false)
  269.             break;
  270.     }
  271. }
  272.  
  273. void Array::QuickSort(int *a, int n, int start, int end)
  274. {
  275.     int i = start;
  276.     int j = end;
  277.     int pillar = a[(end + start) / 2];
  278.    
  279.     while(i < j)
  280.     {
  281.         for (; a[i] < pillar && i <= end; i++);
  282.         for (; a[j] > pillar && j >= start; j--);
  283.         if (i <= j)
  284.         {
  285.             Swap(&a[i], &a[j]);
  286.             i++;
  287.             j--;
  288.         }
  289.     }
  290.     if (start < j)
  291.     {
  292.         QuickSort(a, n, start, j);
  293.     }
  294.     if (end > i)
  295.     {
  296.         QuickSort(a, n, i, end);
  297.     }
  298. }
  299.  
  300. void PrintArray(int *a, int n, int mask)
  301. {
  302.     std::cout << "Mask: \n" << std::bitset<4>(mask) << "\n\n";
  303.     for (int i = 0; i < n; i++)
  304.     {
  305.         std::cout << std::bitset<4>( a[i]);
  306.         std::cout << '\n';
  307.     }
  308.     std::cout << '\n';
  309. }
  310.  
  311. void Array::ByteSort(int *a, int n, int start, int end, int cmpByte)
  312. {
  313.     int i = start;
  314.     int j = end;
  315.     if (i <= j)
  316.     {
  317.         int mask = 1 << cmpByte;
  318.         if (mask <= 16)
  319.         {
  320.             //PrintArray(a, n, mask);
  321.         }
  322.         while (i <= j)
  323.         {
  324.             for (; i < j && (a[i] & mask); i++);
  325.             for (; i < j && !(a[j] & mask); j--);
  326.             if (i <= j)
  327.             {
  328.                 //std::cout << "Swap " << a[i] << " and " << a[j] << '\n';
  329.                 Swap(&a[i], &a[j]);
  330.                 i++;
  331.                 j--;
  332.             }
  333.         }
  334.         if (start < j)
  335.         {
  336.             ByteSort(a, n, start, j, cmpByte - 1);
  337.         }
  338.         if (i < end)
  339.         {
  340.             ByteSort(a, n, i, end, cmpByte - 1);
  341.         }
  342.     }
  343. }
  344.  
  345. #pragma endregion
  346.  
  347. #pragma region PublicFunctions
  348.  
  349. void Array::Scan(int m)
  350. {
  351.     int *buffer = new int[m + capacity];
  352.     for (int i = 0; i < m; i++)
  353.     {
  354.         std::cin >> buffer[i + capacity];
  355.     }
  356.     capacity += m;
  357.     delete arr;
  358.     arr = buffer;
  359. }
  360.  
  361. void Array::Print()
  362. {
  363.     for (int i = 0; i < capacity; i++)
  364.     {
  365.         std::cout << arr[i] << ' ';
  366.     }
  367.     std::cout << '\n';
  368. }
  369.  
  370. int Array::Max()
  371. {
  372.     int maxIndex = 0;
  373.     for (int i = 1; i < capacity; i++)
  374.     {
  375.         if (arr[i] > arr[maxIndex])
  376.         {
  377.             maxIndex = i;
  378.         }
  379.     }
  380.     return maxIndex;
  381. }
  382.  
  383. int Array::Min()
  384. {
  385.     int minIndex = 0;
  386.     for (int i = 1; i < capacity; i++)
  387.     {
  388.         if (arr[i] < arr[minIndex])
  389.         {
  390.             minIndex = i;
  391.         }
  392.     }
  393.     return minIndex;
  394. }
  395.  
  396. int Array::Find(int key)
  397. {
  398.     for (int i = 0; i < capacity; i++)
  399.     {
  400.         if (arr[i] == key)
  401.         {
  402.             return i;
  403.         }
  404.     }
  405.     return -1;
  406. }
  407.  
  408. void Array::Sort(sortType a = sortType::bubbleSort)
  409. {
  410.     switch (a)
  411.     {
  412.     case bubbleSort:
  413.     {
  414.         BubbleSort(arr, capacity);
  415.         break;
  416.     }
  417.     case insertionSort:
  418.     {
  419.         InsertionSort(arr, capacity);
  420.         break;
  421.     }
  422.     case shellSort:
  423.     {
  424.         ShellSort(arr, capacity);
  425.         break;
  426.     }
  427.     case pyramidSort:
  428.     {
  429.         PyramidSort(arr, capacity);
  430.         break;
  431.     }
  432.     case quickSort:
  433.     {
  434.         QuickSort(arr, capacity, 0, capacity - 1);
  435.         break;
  436.     }
  437.     case byteSort:
  438.     {
  439.         ByteSort(arr, capacity, 0, capacity - 1, 4);
  440.         //ByteSort(arr, size, 0, size - 1, 31);
  441.         break;
  442.     }
  443.     default:
  444.     {
  445.         std::cout << "Unexpected argument" << std::endl;
  446.         throw std::exception("Couldnt find function for this argument");
  447.         break;
  448.     }
  449.     }
  450. }
  451.  
  452. #pragma endregion
  453.  
  454. #pragma region Operators
  455.  
  456. int &Array::operator [] (int i)
  457. {
  458.     return arr[i];
  459.     //throw std::exception("Index out of array!");
  460. }
  461.  
  462. Array Array::operator += (const int key)
  463. {
  464.     int *buffer = new int[capacity + 1];
  465.     CopyElements(buffer, arr, capacity);
  466.     buffer[capacity] = key;
  467.     delete arr;
  468.     arr = buffer;
  469.     capacity++;
  470.     return *this;
  471. }
  472.  
  473. Array Array::operator + (int key)
  474. {
  475.     Array result(*this);
  476.     result += key;
  477.     return result;
  478. }
  479.  
  480. Array Array::operator += (Array a)
  481. {
  482.     int *buffer = new int[capacity + a.capacity];
  483.     CopyElements(buffer, arr, capacity);
  484.     delete arr;
  485.     arr = buffer;
  486.     CopyElements(arr, a.arr, a.capacity, capacity);
  487.     capacity = capacity + a.capacity;
  488.     return *this;
  489. }
  490.  
  491. Array Array::operator + (Array a)
  492. {
  493.     Array result(capacity + a.capacity);
  494.     CopyElements(result.arr, arr, capacity);
  495.     CopyElements(result.arr, a.arr, a.capacity, capacity);
  496.     return result;
  497. }
  498.  
  499. Array & Array::operator = (Array &x)
  500. {
  501.     delete arr;
  502.     arr = new int[x.capacity];
  503.     CopyElements(x.arr, x.capacity);
  504.     capacity = x.capacity;
  505.     return *this;
  506. }
  507.  
  508. Array Array::operator -= (int index)
  509. {
  510.     Shift(index);
  511.     return *this;
  512. }
  513.  
  514. Array Array::operator -(int key)
  515. {
  516.     for (int i = 0; i < capacity; i++)
  517.     {
  518.         if (key == arr[i])
  519.         {
  520.             Shift(i);
  521.             break;
  522.         }
  523.     }
  524.     return *this;
  525. }
  526.  
  527. bool Array::operator ==(Array a)
  528. {
  529.     if (a.capacity != capacity)
  530.         return false;
  531.     return IsArraysEq(a.arr, a.capacity);
  532. }
  533.  
  534. bool Array::operator !=(Array a)
  535. {
  536.     if (a.capacity != capacity)
  537.         return true;
  538.     return !IsArraysEq(a.arr, a.capacity);
  539. }
  540.  
  541. #pragma endregion
  542. //Сделано
  543. #pragma region Contructors
  544.  
  545. Array::Array(int Capacity)
  546. {
  547.     arr = new int[Capacity];
  548.     capacity = Capacity;
  549. }
  550.  
  551. Array::Array(int *a, int Capacity)
  552. {
  553.     arr = a;
  554.     capacity = Capacity;
  555. }
  556.  
  557. Array::Array(const Array & x)
  558. {
  559.     if (!arr)
  560.         delete arr;
  561.     arr = new int[x.capacity];
  562.     CopyElements(x.arr, x.capacity);
  563.     capacity = x.capacity;
  564. }
  565.  
  566. Array::~Array()
  567. {
  568.     delete arr;
  569. }
  570.  
  571. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement