Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. #ifndef ARRAY_HPP
  2. #define ARRAY_HPP
  3. #include<iterator>
  4. #include <ostream>
  5. #include <memory>
  6.  
  7. #include "Messages.hpp"
  8.  
  9.  
  10. /* Optionally add std::variant for store array of T or pointer to aray in dynamic memory instead only static array.
  11. * NOT USE union instead std::variant.
  12. * Optionally array size can be constructor parameter instead template argument
  13. * Optionally insert operation can expand array if not enough memory in insert moment
  14. * Optionally use std::unique_ptr in std::variant for store pointer to dynamic allocated memory instead raw pointer.
  15. * If I had VS2017 instead of my outdated VS2015(which doesn;y support C++17 features),
  16. * I would use some of its features (which actually are missing in 2015) and write better code.
  17. */
  18. template <typename T, size_t arraySize = 20>
  19. class CArray
  20. {
  21. public:
  22.  
  23. class CIterator :public std::iterator<std::random_access_iterator_tag, T>
  24. {
  25.  
  26. public:
  27.  
  28. using iterator_category = typename std::random_access_iterator_tag;
  29.  
  30. explicit CIterator(T * _pointer = nullptr) :m_pointer(_pointer) {};
  31.  
  32. CIterator(const CIterator & _iterator);
  33.  
  34. const T & operator * () const { return *m_pointer; };
  35.  
  36. T & operator *() { return *m_pointer; };
  37.  
  38. T * get()const { return m_pointer; };
  39.  
  40. bool operator == (CIterator _iterator)const;
  41.  
  42. bool operator != (CIterator _iterator)const;
  43.  
  44. bool operator > (CIterator _iterator)const;
  45.  
  46. bool operator < (CIterator _iterator)const;
  47.  
  48. bool operator >= (CIterator _iterator)const;
  49.  
  50. bool operator <= (CIterator _iterator)const;
  51.  
  52. CIterator operator + (ptrdiff_t _index)const;
  53.  
  54. CIterator & operator += (ptrdiff_t _index);
  55.  
  56. CIterator & operator -= (ptrdiff_t _index);
  57.  
  58. CIterator operator - (ptrdiff_t _index)const;
  59. ptrdiff_t operator - (CIterator const& other)const;
  60.  
  61. CIterator & operator ++ ();
  62.  
  63. CIterator operator ++(int);
  64.  
  65. CIterator & operator --();
  66.  
  67. CIterator operator --(int)const;
  68.  
  69. private:
  70. T * m_pointer;
  71. };
  72.  
  73.  
  74. CArray();
  75.  
  76. CArray(std::initializer_list<T> _list);
  77.  
  78. template<typename U, size_t OTHER_SIZE >
  79. CArray(const CArray<U, OTHER_SIZE> & _array);
  80.  
  81. template<typename U, size_t OTHER_SIZE >
  82. CArray<T> & operator = (const CArray<U, OTHER_SIZE> & _array);
  83.  
  84. /* template<typename U, int OTHER_SIZE >
  85. CArray ( CArray<U,OTHER_SIZE> && _array);
  86.  
  87. template<typename U, int OTHER_SIZE >
  88. CArray & operator = ( CArray<U,OTHER_SIZE> && _array);*/
  89.  
  90.  
  91. ~CArray()= default;
  92.  
  93. T & operator[ ](size_t _index);
  94.  
  95. T & operator[ ] (size_t _index)const;
  96.  
  97. T front();
  98.  
  99. T back();
  100.  
  101. void push_back(const T & _value);
  102.  
  103. void insert(unsigned int _index, const T & _value);
  104.  
  105. void erase(unsigned int _index);
  106.  
  107. void clear();
  108.  
  109. T const * data() const;
  110.  
  111. size_t size();
  112.  
  113. CIterator begin();
  114. CIterator end();
  115.  
  116.  
  117. private:
  118.  
  119. T m_array[arraySize]{};
  120. size_t m_size;
  121. size_t m_backIndex;
  122.  
  123. };
  124.  
  125. /*****************************************************************************/
  126.  
  127.  
  128. template <typename T, size_t arraySize>
  129. std::ostream & operator << (std::ostream & _stream, CArray<T,arraySize> & _array)
  130. {
  131. _stream << "----------------------------------------------------------------------------------------"<<std::endl;
  132. for (auto const & x : _array)
  133. {
  134. _stream << '[' << x << ']'<< '\t';
  135. }
  136. _stream << std::endl;
  137. _stream << "----------------------------------------------------------------------------------------" << std::endl;
  138.  
  139. return _stream;
  140. }
  141.  
  142.  
  143. template<typename T, size_t arraySize>
  144. inline CArray<T, arraySize>::CIterator::CIterator(const CIterator & _iterator)
  145. {
  146.  
  147. m_pointer = _iterator.get();
  148.  
  149. }
  150.  
  151.  
  152. /*****************************************************************************/
  153. template<typename T, size_t arraySize>
  154. inline bool CArray<T, arraySize>::CIterator::operator==(CIterator _iterator) const
  155. {
  156.  
  157. return m_pointer == _iterator.get();
  158.  
  159. }
  160.  
  161.  
  162. /*****************************************************************************/
  163. template<typename T, size_t arraySize>
  164. inline bool CArray<T, arraySize>::CIterator::operator!=(CIterator _iterator) const
  165. {
  166.  
  167. return !(*this == _iterator);
  168.  
  169. }
  170.  
  171.  
  172. /*****************************************************************************/
  173. template<typename T, size_t arraySize>
  174. inline bool CArray<T, arraySize>::CIterator::operator>(CIterator _iterator) const
  175. {
  176.  
  177. return _iterator<this;
  178.  
  179. }
  180.  
  181.  
  182. /*****************************************************************************/
  183. template<typename T, size_t arraySize>
  184. inline bool CArray<T, arraySize>::CIterator::operator<(CIterator _iterator) const
  185. {
  186.  
  187. return m_pointer< _iterator.get();
  188.  
  189. }
  190.  
  191.  
  192. /*****************************************************************************/
  193. template<typename T, size_t arraySize>
  194. inline bool CArray<T, arraySize>::CIterator::operator>=(CIterator _iterator) const
  195. {
  196.  
  197. return (this>_iterator) || (this == _iterator);
  198.  
  199. }
  200.  
  201.  
  202. /*****************************************************************************/
  203. template<typename T, size_t arraySize>
  204. inline bool CArray<T, arraySize>::CIterator::operator<=(CIterator _iterator) const
  205. {
  206.  
  207. return (this<_iterator) || (this == _iterator);
  208.  
  209. }
  210.  
  211. template<typename T, size_t arraySize>
  212. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator+(ptrdiff_t _index) const
  213. {
  214. return CIterator(m_pointer + _index);
  215. }
  216.  
  217. template<typename T, size_t arraySize>
  218. inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator+=(ptrdiff_t _index)
  219. {
  220. m_pointer += _index;
  221. return *this;
  222. }
  223.  
  224. template<typename T, size_t arraySize>
  225. inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator-=(ptrdiff_t _index)
  226. {
  227. m_pointer -= _index;
  228. return *this;
  229. }
  230.  
  231. template<typename T, size_t arraySize>
  232. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator-(ptrdiff_t _index) const
  233. {
  234. return CIterator(m_pointer - _index);
  235. }
  236.  
  237. template<typename T, size_t arraySize>
  238. inline ptrdiff_t CArray<T, arraySize>::CIterator::operator-(typename CArray<T, arraySize>::CIterator const& other) const
  239. {
  240. return m_pointer - other.m_pointer;
  241. }
  242.  
  243.  
  244.  
  245.  
  246. /*****************************************************************************/
  247. template<typename T, size_t arraySize>
  248. typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator++()
  249. {
  250. ++m_pointer;
  251. return *this;
  252. }
  253.  
  254.  
  255. /*****************************************************************************/
  256. template<typename T, size_t arraySize>
  257. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator++(int)
  258. {
  259. return CIterator(m_pointer++);
  260. }
  261.  
  262.  
  263. /*****************************************************************************/
  264. template<typename T, size_t arraySize>
  265. inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator--()
  266. {
  267. --m_pointer;
  268. return *this;
  269. }
  270.  
  271.  
  272. /*****************************************************************************/
  273. template<typename T, size_t arraySize>
  274. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator--(int)const
  275. {
  276. return CIterator(m_pointer--);
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. /*****************************************************************************/
  284. template<typename T, size_t arraySize >
  285. inline CArray<T, arraySize>::CArray() :m_size{ arraySize }, m_backIndex{}
  286. {
  287. //TODO - add assert for invalid template arguments
  288. }
  289.  
  290. template<typename T, size_t arraySize>
  291. inline CArray<T, arraySize>::CArray(std::initializer_list<T> _list) : CArray<T, arraySize>::CArray()
  292. {
  293. if (m_size < _list.size())
  294. {
  295. throw std::logic_error(Messages::InitializerListSizeError);
  296. }
  297.  
  298. for (auto const & x : _list)
  299. {
  300. push_back(x);
  301. }
  302.  
  303. }
  304.  
  305. template<typename T, size_t arraySize>
  306. inline T & CArray<T, arraySize>::operator[](size_t _index)
  307. {
  308. if (_index > m_size-1)
  309. {
  310. throw std::logic_error(Messages::InvalidAcessIndex);
  311. }
  312.  
  313. return *(m_array + _index);
  314. }
  315.  
  316. template<typename T, size_t arraySize>
  317. inline T & CArray<T, arraySize>::operator[](size_t _index) const
  318. {
  319. if (_index > m_size-1)
  320. {
  321. throw std::logic_error(Messages::InvalidAcessIndex);
  322. }
  323. return *(m_array + _index);
  324. }
  325.  
  326. template<typename T, size_t arraySize>
  327. inline T CArray<T, arraySize>::front()
  328. {
  329. return *begin();
  330. }
  331.  
  332. template<typename T, size_t arraySize>
  333. inline T CArray<T, arraySize>::back()
  334. {
  335. return *(--end());
  336. }
  337.  
  338. template<typename T, size_t arraySize>
  339. inline void CArray<T, arraySize>::push_back(const T & _value)
  340. {
  341. if (m_backIndex == m_size)
  342. {
  343. throw std::logic_error(Messages::ArrayOverflow);
  344. }
  345. m_array[m_backIndex] = _value;
  346. m_backIndex++;
  347. }
  348.  
  349. template<typename T, size_t arraySize>
  350. inline void CArray<T, arraySize>::insert(unsigned int _index, const T & _value)
  351. {
  352. if (_index > m_size)
  353. {
  354. throw std::logic_error(Messages::InvalidAcessInsert);
  355. }
  356.  
  357. CArray<T, arraySize> tempArray;
  358. std::copy(begin() + _index, end(), tempArray.begin());
  359. m_array[_index] = _value;
  360. std::copy(tempArray.begin(), tempArray.end(), begin() + _index + 1);
  361. }
  362.  
  363. template<typename T, size_t arraySize>
  364. inline void CArray<T, arraySize>::erase(unsigned int _index)
  365. {
  366. if (_index > m_size)
  367. {
  368. throw std::logic_error(Messages::InvalidAcessErase);
  369. }
  370.  
  371. CArray<T, arraySize> tempArray;
  372. size_t l_backIndex = m_backIndex;
  373.  
  374.  
  375. std::copy(begin() + _index + 1, end(), tempArray.begin());
  376.  
  377. std::copy(tempArray.begin(), tempArray.end() - _index, begin() + _index);
  378.  
  379. m_size = arraySize;
  380. m_backIndex = l_backIndex - 1;
  381. }
  382.  
  383. template<typename T, size_t arraySize>
  384. inline void CArray<T, arraySize>::clear()
  385. {
  386. for (auto & x : m_array)
  387. {
  388. x = 0;
  389. }
  390. m_backIndex = 0;
  391. }
  392.  
  393. template<typename T, size_t arraySize>
  394. inline T const * CArray<T, arraySize>::data() const
  395. {
  396. return m_array;
  397. }
  398.  
  399. template<typename T, size_t arraySize>
  400. inline size_t CArray<T, arraySize>::size()
  401. {
  402. return m_size;
  403. }
  404.  
  405. template<typename T, size_t arraySize>
  406. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::begin()
  407. {
  408. return CIterator(m_array);
  409. }
  410.  
  411. template<typename T, size_t arraySize>
  412. inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::end()
  413. {
  414. return CIterator(m_array + arraySize);
  415. }
  416.  
  417. template<typename T, size_t arraySize>
  418. template<typename U, size_t OTHER_SIZE>
  419. inline CArray<T, arraySize>::CArray(const CArray<U, OTHER_SIZE> & _array)
  420. {
  421. std::copy(_array.begin(), _array.end() , begin());
  422. //m_size = _array.size();
  423. }
  424.  
  425. template<typename T, size_t arraySize>
  426. template<typename U, size_t OTHER_SIZE>
  427. inline CArray<T> & CArray<T, arraySize>::operator=(const CArray<U, OTHER_SIZE>& _array)
  428. {
  429. if (static_cast<const void *>(this) == static_cast<const void *> (&_array))
  430. return *this;
  431.  
  432. m_array = _array.data();
  433. m_backIndex = _array.m_backIndex;
  434. m_size = _array.size;
  435. return *this;
  436. }
  437.  
  438. #endif // !ARRAY_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement