Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Set.h"
  4.  
  5. template<typename T>
  6. Set<T>::Set () : ammount (sizeblock)
  7. {
  8.     this->mas = 0;
  9.     this->capasity = 0;
  10. }
  11.  
  12. template<typename T>
  13. Set<T>::Set (int count, ...) : ammount (sizeblock)
  14. {
  15.     int n = count / this->ammount + (1 - !(count%this->ammount));
  16.  
  17.     this->capacity = count;
  18.     this->mas = new T[n*this->ammount];
  19.  
  20.     T* p = (T*)(&count + 1);
  21.     for (int i = 0; i < this->capacity; i++)
  22.         this->mas[i] = p[i];
  23. }
  24.  
  25. template<typename T>
  26. Set<T>::Set (const Set<T>& s) : ammount (sizeblock)
  27. {
  28.     int n = s.capacity / s.ammount + (1 - !(s.capacity%s.ammount));
  29.  
  30.     this->capacity = s.capacity;
  31.     this->mas = new T[n*this->ammount];
  32.  
  33.     for (int i = 0; i < this->capacity; i++)
  34.         this->mas[i] = s.mas[i];
  35. }
  36.  
  37. template<typename T>
  38. Set<T>::Set (Set<T>&& s) : ammount (sizeblock)
  39. {
  40.     this->capacity = s.capacity;
  41.     this->mas = s.mas;
  42.  
  43.     s.mas = 0;
  44. }
  45.  
  46. template<typename T>
  47. Set<T>::~Set ()
  48. {
  49.     delete mas;
  50. }
  51.  
  52. template<typename T>
  53. Set<T>& Set<T>::operator=(const Set<T>& s)
  54. {
  55.     this->capacity = s.capacity;
  56.     int n = s.capacity / s.ammount + (1 - !(s.capacity%s.ammount));
  57.     delete this->mas;
  58.     this->mas = new T[n*this->ammount];
  59.     for (int i; i < this->capacity; i++)
  60.         this->mas[i] = s.mas[i];
  61. }
  62.  
  63. template<typename T>
  64. Set<T>& Set<T>::operator=(Set<T>&& s)
  65. {
  66.     this->capacity = s.capacity;
  67.     this->mas = s.mas;
  68.     s.mas = 0;
  69. }
  70.  
  71. template<typename T>
  72. inline void Set<T>::sortSet (int left, int right) const
  73. {
  74. }
  75.  
  76. //_________________________________________________//
  77.  
  78. template <typename T>
  79. bool Set<T>::empty() const
  80. {
  81.     if (this->mas == NULL)
  82.         return false;
  83.     else
  84.         return true;
  85. }
  86.  
  87. template <typename T>
  88. bool Set<T>::belong (const T& elem) const
  89. {
  90.     bool res = false;
  91.     for (int i = 0; i < this->capacity; i++)
  92.         if (this->mas[i] == elem)
  93.         {
  94.             res = true;
  95.             break;
  96.         }
  97.     return res;
  98. }
  99.  
  100. template <typename T>
  101. bool Set<T>::add(const T& elem)
  102. {
  103.     if (/*!(this->empty () ||*/ this->belong (elem)))
  104.         return false;
  105.     else
  106.     {
  107.         int n = (this->capacity + 1) / this->ammount + (1 - !((this->capacity + 1)%this->ammount));
  108.         if (n*this->ammount > this->ammount)
  109.         {  
  110.             this->ammount = n*this->ammount;
  111.             this->capacity++;
  112.             this->mas = (T*)realloc (this->mas, (this->ammount) * sizeof (T));
  113.             this->mas[(this->capacity - 1)] = el;
  114.         }
  115.         else
  116.             this->mas[this->capacity++] = el;
  117.        
  118.         return true;
  119.     }
  120.    
  121. }
  122.  
  123.  
  124. template <typename T>
  125. bool Set<T>::operator <(const T& elem) const
  126. {
  127.     return this->belong (elem);
  128. }
  129.  
  130. template <typename T>
  131. void Set<T>::sortSet (int left, int right) const
  132. {
  133.     int i = left;
  134.     int j = right;
  135.     T tmp = this->mas[(left + right) / 2];
  136.     T tmpSwap;
  137.     while (i <= j)
  138.     {
  139.         while (this->mas[i] < tmp)
  140.             i++;
  141.         while (this->mas[j] > tmp)
  142.             j--;
  143.         if (j <= j)
  144.         {
  145.             tmpSwap = this - < mas[i];
  146.             this->mas[i] = this->mas[j];
  147.             this->mas[j] = tmpSwap;
  148.             i++;
  149.             j--:
  150.         }
  151.     };
  152.     if (left < j) sortSet (left, j);
  153.     if (i < right) sortSet (i, right);
  154. }
  155.  
  156. template <typename T>
  157. Set<T> Set<T>::operator +(const Set<T>& s) const
  158. {
  159.     this->sortSet (0, this->capacity - 1);
  160.     s.sortSet (0, s.capacity - 1);
  161.     Set<T> res;
  162.     int i = 0, j = 0;
  163.     while (i < this->capacity && j < s.capacity)
  164.     {
  165.         if (this->mas[i] == s.mas[j])
  166.         {
  167.             res.add (this->mas[j]);
  168.             i++;
  169.         }
  170.         else
  171.         {
  172.             if (this->mas[i] < s.mas[j])
  173.             {
  174.                 res.add (this->mas[i]);
  175.                 i++;
  176.             }
  177.             else
  178.             {
  179.                 res.add (s.mas[j]);
  180.                 j++;
  181.             }
  182.         }
  183.     }
  184.     while (i < this->capacity)
  185.     {
  186.         res.add (this->mas[i]) :
  187.         i++;
  188.     }
  189.     while (j < s.capacity)
  190.     {
  191.         res.add (s.mas[j]);
  192.         j++;
  193.     }
  194.     return res;
  195. }
  196.  
  197. template <typename T>
  198. bool Set<T>::del (const T& elem)
  199. {
  200.     if (!this->belong (elem))
  201.         return false;
  202.     else
  203.     {
  204.         int tmp;
  205.         for (tmp = 0; tmp < this->capacity; i++)
  206.             if (elem == this->mas[tmp])
  207.                 break;
  208.  
  209.         for (int i = tmp; i < this->capacity - 1; i++)
  210.             this->mas[i] = this->mas[i + 1];
  211.         this->mas = (T*)realloc (this->mas, (this->capacity - 1)*sizeof (T));
  212.         this->capacity--;
  213.         int n = this->capacity / this->ammount + (1 - !(this->capacity % this->ammount));
  214.         this->mas = (T*)realloc (this->mas, n*this->ammount*sizeof (T));
  215.         return true;
  216.     }
  217. }
  218.  
  219. template <typename T>
  220. Set<T> Set<T>::operator +(const T& elem) const
  221. {
  222.     Set res;
  223.     res.add (res);
  224.     return res;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement