Advertisement
meta1211

back up

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