Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef ARRAY_HPP
- #define ARRAY_HPP
- #include<iterator>
- #include <ostream>
- #include <memory>
- #include "Messages.hpp"
- /* Optionally add std::variant for store array of T or pointer to aray in dynamic memory instead only static array.
- * NOT USE union instead std::variant.
- * Optionally array size can be constructor parameter instead template argument
- * Optionally insert operation can expand array if not enough memory in insert moment
- * Optionally use std::unique_ptr in std::variant for store pointer to dynamic allocated memory instead raw pointer.
- * If I had VS2017 instead of my outdated VS2015(which doesn;y support C++17 features),
- * I would use some of its features (which actually are missing in 2015) and write better code.
- */
- template <typename T, size_t arraySize = 20>
- class CArray
- {
- public:
- class CIterator :public std::iterator<std::random_access_iterator_tag, T>
- {
- public:
- using iterator_category = typename std::random_access_iterator_tag;
- explicit CIterator(T * _pointer = nullptr) :m_pointer(_pointer) {};
- CIterator(const CIterator & _iterator);
- const T & operator * () const { return *m_pointer; };
- T & operator *() { return *m_pointer; };
- T * get()const { return m_pointer; };
- bool operator == (CIterator _iterator)const;
- bool operator != (CIterator _iterator)const;
- bool operator > (CIterator _iterator)const;
- bool operator < (CIterator _iterator)const;
- bool operator >= (CIterator _iterator)const;
- bool operator <= (CIterator _iterator)const;
- CIterator operator + (ptrdiff_t _index)const;
- CIterator & operator += (ptrdiff_t _index);
- CIterator & operator -= (ptrdiff_t _index);
- CIterator operator - (ptrdiff_t _index)const;
- ptrdiff_t operator - (CIterator const& other)const;
- CIterator & operator ++ ();
- CIterator operator ++(int);
- CIterator & operator --();
- CIterator operator --(int)const;
- private:
- T * m_pointer;
- };
- CArray();
- CArray(std::initializer_list<T> _list);
- template<typename U, size_t OTHER_SIZE >
- CArray(const CArray<U, OTHER_SIZE> & _array);
- template<typename U, size_t OTHER_SIZE >
- CArray<T> & operator = (const CArray<U, OTHER_SIZE> & _array);
- /* template<typename U, int OTHER_SIZE >
- CArray ( CArray<U,OTHER_SIZE> && _array);
- template<typename U, int OTHER_SIZE >
- CArray & operator = ( CArray<U,OTHER_SIZE> && _array);*/
- ~CArray()= default;
- T & operator[ ](size_t _index);
- T & operator[ ] (size_t _index)const;
- T front();
- T back();
- void push_back(const T & _value);
- void insert(unsigned int _index, const T & _value);
- void erase(unsigned int _index);
- void clear();
- T const * data() const;
- size_t size();
- CIterator begin();
- CIterator end();
- private:
- T m_array[arraySize]{};
- size_t m_size;
- size_t m_backIndex;
- };
- /*****************************************************************************/
- template <typename T, size_t arraySize>
- std::ostream & operator << (std::ostream & _stream, CArray<T,arraySize> & _array)
- {
- _stream << "----------------------------------------------------------------------------------------"<<std::endl;
- for (auto const & x : _array)
- {
- _stream << '[' << x << ']'<< '\t';
- }
- _stream << std::endl;
- _stream << "----------------------------------------------------------------------------------------" << std::endl;
- return _stream;
- }
- template<typename T, size_t arraySize>
- inline CArray<T, arraySize>::CIterator::CIterator(const CIterator & _iterator)
- {
- m_pointer = _iterator.get();
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator==(CIterator _iterator) const
- {
- return m_pointer == _iterator.get();
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator!=(CIterator _iterator) const
- {
- return !(*this == _iterator);
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator>(CIterator _iterator) const
- {
- return _iterator<this;
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator<(CIterator _iterator) const
- {
- return m_pointer< _iterator.get();
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator>=(CIterator _iterator) const
- {
- return (this>_iterator) || (this == _iterator);
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline bool CArray<T, arraySize>::CIterator::operator<=(CIterator _iterator) const
- {
- return (this<_iterator) || (this == _iterator);
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator+(ptrdiff_t _index) const
- {
- return CIterator(m_pointer + _index);
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator+=(ptrdiff_t _index)
- {
- m_pointer += _index;
- return *this;
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator-=(ptrdiff_t _index)
- {
- m_pointer -= _index;
- return *this;
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator-(ptrdiff_t _index) const
- {
- return CIterator(m_pointer - _index);
- }
- template<typename T, size_t arraySize>
- inline ptrdiff_t CArray<T, arraySize>::CIterator::operator-(typename CArray<T, arraySize>::CIterator const& other) const
- {
- return m_pointer - other.m_pointer;
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator++()
- {
- ++m_pointer;
- return *this;
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator++(int)
- {
- return CIterator(m_pointer++);
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator & CArray<T, arraySize>::CIterator::operator--()
- {
- --m_pointer;
- return *this;
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::CIterator::operator--(int)const
- {
- return CIterator(m_pointer--);
- }
- /*****************************************************************************/
- template<typename T, size_t arraySize >
- inline CArray<T, arraySize>::CArray() :m_size{ arraySize }, m_backIndex{}
- {
- //TODO - add assert for invalid template arguments
- }
- template<typename T, size_t arraySize>
- inline CArray<T, arraySize>::CArray(std::initializer_list<T> _list) : CArray<T, arraySize>::CArray()
- {
- if (m_size < _list.size())
- {
- throw std::logic_error(Messages::InitializerListSizeError);
- }
- for (auto const & x : _list)
- {
- push_back(x);
- }
- }
- template<typename T, size_t arraySize>
- inline T & CArray<T, arraySize>::operator[](size_t _index)
- {
- if (_index > m_size-1)
- {
- throw std::logic_error(Messages::InvalidAcessIndex);
- }
- return *(m_array + _index);
- }
- template<typename T, size_t arraySize>
- inline T & CArray<T, arraySize>::operator[](size_t _index) const
- {
- if (_index > m_size-1)
- {
- throw std::logic_error(Messages::InvalidAcessIndex);
- }
- return *(m_array + _index);
- }
- template<typename T, size_t arraySize>
- inline T CArray<T, arraySize>::front()
- {
- return *begin();
- }
- template<typename T, size_t arraySize>
- inline T CArray<T, arraySize>::back()
- {
- return *(--end());
- }
- template<typename T, size_t arraySize>
- inline void CArray<T, arraySize>::push_back(const T & _value)
- {
- if (m_backIndex == m_size)
- {
- throw std::logic_error(Messages::ArrayOverflow);
- }
- m_array[m_backIndex] = _value;
- m_backIndex++;
- }
- template<typename T, size_t arraySize>
- inline void CArray<T, arraySize>::insert(unsigned int _index, const T & _value)
- {
- if (_index > m_size)
- {
- throw std::logic_error(Messages::InvalidAcessInsert);
- }
- CArray<T, arraySize> tempArray;
- std::copy(begin() + _index, end(), tempArray.begin());
- m_array[_index] = _value;
- std::copy(tempArray.begin(), tempArray.end(), begin() + _index + 1);
- }
- template<typename T, size_t arraySize>
- inline void CArray<T, arraySize>::erase(unsigned int _index)
- {
- if (_index > m_size)
- {
- throw std::logic_error(Messages::InvalidAcessErase);
- }
- CArray<T, arraySize> tempArray;
- size_t l_backIndex = m_backIndex;
- std::copy(begin() + _index + 1, end(), tempArray.begin());
- std::copy(tempArray.begin(), tempArray.end() - _index, begin() + _index);
- m_size = arraySize;
- m_backIndex = l_backIndex - 1;
- }
- template<typename T, size_t arraySize>
- inline void CArray<T, arraySize>::clear()
- {
- for (auto & x : m_array)
- {
- x = 0;
- }
- m_backIndex = 0;
- }
- template<typename T, size_t arraySize>
- inline T const * CArray<T, arraySize>::data() const
- {
- return m_array;
- }
- template<typename T, size_t arraySize>
- inline size_t CArray<T, arraySize>::size()
- {
- return m_size;
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::begin()
- {
- return CIterator(m_array);
- }
- template<typename T, size_t arraySize>
- inline typename CArray<T, arraySize>::CIterator CArray<T, arraySize>::end()
- {
- return CIterator(m_array + arraySize);
- }
- template<typename T, size_t arraySize>
- template<typename U, size_t OTHER_SIZE>
- inline CArray<T, arraySize>::CArray(const CArray<U, OTHER_SIZE> & _array)
- {
- std::copy(_array.begin(), _array.end() , begin());
- //m_size = _array.size();
- }
- template<typename T, size_t arraySize>
- template<typename U, size_t OTHER_SIZE>
- inline CArray<T> & CArray<T, arraySize>::operator=(const CArray<U, OTHER_SIZE>& _array)
- {
- if (static_cast<const void *>(this) == static_cast<const void *> (&_array))
- return *this;
- m_array = _array.data();
- m_backIndex = _array.m_backIndex;
- m_size = _array.size;
- return *this;
- }
- #endif // !ARRAY_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement