--sas

MyVector

Jun 5th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 KB | None | 0 0
  1. //
  2. // Created by stas on 6/3/19.
  3. //
  4.  
  5. #pragma once
  6.  
  7. #include <iterator>
  8.  
  9. template <typename T>
  10. class MyVector {
  11. public:
  12.     MyVector() = default;
  13.     explicit MyVector(unsigned int size);
  14.     MyVector(unsigned int size, const T& value);
  15.  
  16.     template <typename InputIt>
  17.     MyVector(InputIt fst, InputIt lst);
  18.  
  19.     MyVector(const MyVector& other);
  20.  
  21.     ~MyVector();
  22.  
  23.     MyVector<T>& operator= (const MyVector& other);
  24.  
  25.     void assign(unsigned int count, const T& value);
  26.  
  27.     template <typename InputIt>
  28.     void assign(InputIt fst, InputIt lst);
  29.  
  30.     T& operator[] (unsigned int pos);
  31.     const T& operator[] (unsigned int pos) const;
  32.  
  33.     T& at(unsigned int pos);
  34.     const T& at(unsigned pos) const;
  35.  
  36.     T& back();
  37.     const T& back() const;
  38.  
  39.     T& front();
  40.     const T& front() const;
  41.  
  42.     T* data();
  43.     const T* data() const;
  44.  
  45.     bool empty() const;
  46.     unsigned int size() const;
  47.     unsigned int capacity() const;
  48.     void resize(unsigned int new_size);
  49.     void resize(unsigned int new_size, const T& value);
  50.  
  51.     void reserve(unsigned int new_cap);
  52.     void shrink_to_fit();
  53.     void clear();
  54.     void push_back(const T& value);
  55.     void pop_back();
  56.  
  57. private:
  58.     unsigned int size_ = 0u;
  59.     unsigned int capacity_ = 0u;
  60.     T* data_ = nullptr;
  61. };
  62.  
  63. template <typename T>
  64. bool operator== (const MyVector<T>& lhs, const MyVector<T>& rhs);
  65.  
  66. template <typename T>
  67. bool operator!= (const MyVector<T>& lhs, const MyVector<T>& rhs);
  68.  
  69. template <typename T>
  70. bool operator< (const MyVector<T>& lhs, const MyVector<T>& rhs);
  71.  
  72. template <typename T>
  73. bool operator<= (const MyVector<T>& lhs, const MyVector<T>& rhs);
  74.  
  75. template <typename T>
  76. bool operator> (const MyVector<T>& lhs, const MyVector<T>& rhs);
  77.  
  78. template <typename T>
  79. bool operator>= (const MyVector<T>& lhs, const MyVector<T>& rhs);
  80.  
  81. template <typename T>
  82. MyVector<T>::MyVector(unsigned int size) :
  83.         size_(size),
  84.         capacity_(size)
  85. {
  86.     data_ = new T[capacity_];
  87. }
  88.  
  89. template <typename T>
  90. MyVector<T>::MyVector(unsigned int size, const T& value) :
  91.     MyVector(size)
  92. {
  93.     for (unsigned i = 0; i < size; ++i)
  94.         data_[i] = value;
  95. }
  96.  
  97. template <typename T>
  98. template <typename InputIt>
  99. MyVector<T>::MyVector(InputIt fst, InputIt lst) :
  100.     size_(std::distance(fst, lst)),
  101.     capacity_(size_)
  102. {
  103.     data_ = new T[capacity_];
  104.     auto it = fst;
  105.     T* a = data_;
  106.     while (it != lst) {
  107.         *a = *it;
  108.         ++a;
  109.         ++it;
  110.     }
  111. }
  112.  
  113. template <typename T>
  114. MyVector<T>::MyVector(const MyVector<T>& other) :
  115.     MyVector(other.size_, other.capacity_)
  116. {
  117.     for (unsigned i = 0; i < size_; ++i)
  118.         data_[i] = other.data_[i];
  119. }
  120. template <typename T>
  121. MyVector<T>::~MyVector() {
  122.     delete[] data_;
  123. }
  124.  
  125. template <typename T>
  126. MyVector<T>& MyVector<T>::operator= (const MyVector<T>& other) {
  127.     if (this == &other)
  128.         return *this;
  129.  
  130.     delete[] data_;
  131.  
  132.     size_ = other.size_;
  133.     capacity_ = other.capacity_;
  134.     data_ = new T[capacity_];
  135.  
  136.     for (unsigned i = 0; i < size_; ++i)
  137.         data_[i] = other.data_[i];
  138.  
  139.     return *this;
  140. }
  141.  
  142. template <typename T>
  143. void MyVector<T>::assign(unsigned int count, const T& value) {
  144.     delete[] data_;
  145.     size_ = count;
  146.     capacity_ = count;
  147.     data_ = new T[capacity_];
  148.     for (unsigned i = 0; i < count; ++i)
  149.         data_[i] = value;
  150. }
  151.  
  152. template <typename T>
  153. template <typename InputIt>
  154. void MyVector<T>::assign(InputIt fst, InputIt lst) {
  155.     delete[] data_;
  156.     size_ = std::distance(fst, lst);
  157.     capacity_ = size_;
  158.     data_ = new T[capacity_];
  159.     auto it = fst;
  160.     T* a = data_;
  161.     while (it != lst) {
  162.         *a = *it;
  163.         ++a;
  164.         ++it;
  165.     }
  166. }
  167.  
  168. template <typename T>
  169. const T& MyVector<T>::operator[] (unsigned int pos) const {
  170.     return *(data_ + pos);
  171. }
  172.  
  173. template <typename T>
  174. T& MyVector<T>::operator[] (unsigned int pos) {
  175.     const T& res = *this[pos];
  176.     return const_cast<T&>(res);
  177. }
  178.  
  179. template <typename T>
  180. const T& MyVector<T>::at (unsigned int pos) const {
  181.     if (pos >= size_)
  182.         throw std::out_of_range(std::to_string(pos) + " > " + std::to_string(size_));
  183.     else
  184.         return *(data_ + pos);
  185. }
  186.  
  187. template <typename T>
  188. T& MyVector<T>::at (unsigned int pos) {
  189.     const T& res = this->at(pos);
  190.     return const_cast<T&>(res);
  191. }
  192.  
  193. template <typename T>
  194. const T& MyVector<T>::back() const {
  195.     if (size_ == 0)
  196.         throw std::out_of_range("The vector is empty");
  197.     else
  198.         return data_[size_ - 1];
  199. }
  200.  
  201. template <typename T>
  202. T& MyVector<T>::back() {
  203.     const T& res = this->back();
  204.     return const_cast<T&>(res);
  205. }
  206.  
  207. template <typename T>
  208. const T& MyVector<T>::front() const {
  209.     if (size_ == 0)
  210.         throw std::out_of_range("The vector is empty");
  211.     else
  212.         return *data_;
  213. }
  214.  
  215. template <typename T>
  216. T& MyVector<T>::front() {
  217.     const T& res = this->front();
  218.     return const_cast<T&>(res);
  219. }
  220.  
  221. template <typename T>
  222. const T* MyVector<T>::data() const {
  223.     return data_;
  224. }
  225.  
  226. template <typename T>
  227. T* MyVector<T>::data() {
  228.     return data_;
  229. }
  230.  
  231. template <typename T>
  232. bool MyVector<T>::empty() const {
  233.     return size_ == 0;
  234. }
  235.  
  236. template <typename T>
  237. unsigned int MyVector<T>::size() const {
  238.     return size_;
  239. }
  240.  
  241. template <typename T>
  242. unsigned int MyVector<T>::capacity() const {
  243.     return capacity_;
  244. }
  245.  
  246. template <typename T>
  247. void MyVector<T>::reserve(unsigned int new_cap) {
  248.     if (capacity_ >= new_cap)
  249.         return;
  250.  
  251.     T*& old_data = data_;
  252.     data_ = new T[new_cap];
  253.     for (unsigned int i = 0; i < size_; ++i)
  254.         data_[i] = old_data[i];
  255.     delete[] old_data;
  256.     capacity_ = new_cap;
  257. }
  258.  
  259. template <typename T>
  260. void MyVector<T>::shrink_to_fit() {
  261.     if (capacity_ > size_) {
  262.         delete[] (data_ + size_, capacity_ - size_);
  263.         capacity_ = size_;
  264.     }
  265. }
  266.  
  267. template <typename T>
  268. void MyVector<T>::clear() {
  269.     size_ = 0;
  270. }
  271.  
  272. template <typename T>
  273. void MyVector<T>::resize (unsigned int new_size) {
  274.     reserve(new_size);
  275.     size_ = new_size;
  276. }
  277.  
  278. template <typename T>
  279. void MyVector<T>::resize(unsigned int new_size, const T& value) {
  280.     reserve(new_size);
  281.     for (unsigned i = size_; i < new_size; ++i)
  282.         data_[i] = value;
  283.     size_ = new_size;
  284. }
  285.  
  286. template <typename T>
  287. void MyVector<T>::push_back(const T& value) {
  288.     if (size_ == capacity_)
  289.         reserve(capacity_ *= 1.3);
  290.     data_[size_] = value;
  291.     ++size_;
  292. }
  293.  
  294. template <typename T>
  295. void MyVector<T>::pop_back() {
  296.     if (size_ == 0)
  297.         throw std::out_of_range("The vector is empty");
  298.     else
  299.         --size_;
  300. }
  301.  
  302. template <typename T>
  303. bool operator== (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  304.     if (lhs.size() != rhs.size())
  305.         return false;
  306.     const T* lhdata = lhs.data(), rhdata = rhs.data();
  307.     for (unsigned i = 0; i < lhs.size(); ++i) {
  308.         if (lhdata[i] != rhdata[i])
  309.             return false;
  310.     }
  311.     return true;
  312. }
  313.  
  314. template <typename T>
  315. bool operator!= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  316.     return !(lhs == rhs);
  317. }
  318.  
  319. template <typename T>
  320. bool operator< (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  321.     const T* lhdata = lhs.data(), rhdata = rhs.data();
  322.     unsigned int size = std::min(lhs.size(), rhs.size());
  323.     for (unsigned i = 0; i < size; ++i) {
  324.         if (lhdata[i] < rhdata[i])
  325.             return true;
  326.     }
  327.     return lhs.size() < rhs.size();
  328. }
  329.  
  330. template <typename T>
  331. bool operator>= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  332.     return !(lhs < rhs);
  333. }
  334.  
  335. template <typename T>
  336. bool operator<= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  337.     return rhs >= lhs;
  338. }
  339.  
  340. template <typename T>
  341. bool operator> (const MyVector<T>& lhs, const MyVector<T>& rhs) {
  342.     return rhs < lhs;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment