From 6ac2a3c6ec12514064a6caa504117f35a8908343 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Wed, 19 Nov 2014 18:38:09 +0000 Subject: [PATCH] Updated array --- array.h | 364 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 192 insertions(+), 172 deletions(-) diff --git a/array.h b/array.h index 505f7231..7a4aa238 100644 --- a/array.h +++ b/array.h @@ -35,6 +35,8 @@ SOFTWARE. #include #include "exception.h" +#include "type_traits.h" +#include "parameter_type.h" ///\defgroup array array /// A replacement for std::array if you haven't got C++0x11. @@ -79,118 +81,27 @@ namespace etl template class array { + private: + + typedef typename parameter_type::value || is_pointer::value>::type parameter_t; + public: - //************************************************************************* - /// STL style typedefs. - //************************************************************************* - typedef T* iterator; + typedef T value_type; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef T& reference; + typedef const T& const_reference; + typedef T* pointer; + typedef const T* const_pointer; + typedef T* iterator; typedef const T* const_iterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; - typedef T* pointer; - typedef const T* const_pointer; - typedef T& reference; - typedef const T& const_reference; - typedef std::ptrdiff_t difference_type; - typedef std::size_t size_type; - typedef T value_type; - //************************************************************************* - /// Returns an iterator to the beginning of the array. - //************************************************************************* - inline iterator begin() - { - return &_buffer[0]; - } - - //************************************************************************* - /// Returns a const iterator to the beginning of the array. - //************************************************************************* - inline const_iterator begin() const - { - return &_buffer[0]; - } - - //************************************************************************* - /// Returns a const iterator to the beginning of the array. - //************************************************************************* - inline const_iterator cbegin() const - { - return begin(); - } - - //************************************************************************* - // Returns an reverse iterator to the reverse beginning of the array. - //************************************************************************* - inline reverse_iterator rbegin() - { - return reverse_iterator(end()); - } - - //************************************************************************* - /// Returns a const reverse iterator to the reverse beginning of the array. - //************************************************************************* - inline const_reverse_iterator rbegin() const - { - return const_reverse_iterator(end()); - } - - //************************************************************************* - /// Returns a const reverse iterator to the reverse beginning of the array. - //************************************************************************* - inline const_reverse_iterator crbegin() const - { - return const_reverse_iterator(end()); - } - - //************************************************************************* - /// Returns an iterator to the end of the array. - //************************************************************************* - inline iterator end() - { - return &_buffer[SIZE]; - } - - //************************************************************************* - /// Returns a const iterator to the end of the array. - //************************************************************************* - inline const_iterator end() const - { - return &_buffer[SIZE]; - } - - //************************************************************************* - // Returns a const iterator to the end of the array. - //************************************************************************* - inline const_iterator cend() const - { - return &_buffer[SIZE]; - } - - //************************************************************************* - /// Returns a reverse iterator to the end of the array. - //************************************************************************* - inline reverse_iterator rend() - { - return reverse_iterator(begin()); - } - - //************************************************************************* - /// Returns a const reverse iterator to the end of the array. - //************************************************************************* - inline const_reverse_iterator rend() const - { - return const_reverse_iterator(begin()); - } - - //************************************************************************* - /// Returns a const reverse iterator to the end of the array. - //************************************************************************* - inline const_reverse_iterator crend() const - { - return const_reverse_iterator(begin()); - } + //************************************************************************* + // Element access + //************************************************************************* //************************************************************************* /// Returns a reference to the value at index 'i'. @@ -248,6 +159,158 @@ namespace etl return _buffer[i]; } + //************************************************************************* + /// Returns a reference to the first element. + //************************************************************************* + inline reference front() + { + return _buffer[0]; + } + + //************************************************************************* + /// Returns a const reference to the first element. + //************************************************************************* + inline const_reference front() const + { + return _buffer[0]; + } + + //************************************************************************* + /// Returns a reference to the last element. + //************************************************************************* + inline reference back() + { + return _buffer[SIZE - 1]; + } + + //************************************************************************* + /// Returns a const reference to the last element. + //************************************************************************* + inline const_reference back() const + { + return _buffer[SIZE - 1]; + } + + //************************************************************************* + /// Returns a pointer to the first element of the internal buffer. + //************************************************************************* + inline pointer data() + { + return &_buffer[0]; + } + + //************************************************************************* + /// Returns a const pointer to the first element of the internal buffer. + //************************************************************************* + inline const_pointer data() const + { + return &_buffer[0]; + } + + //************************************************************************* + // Iterators + //************************************************************************* + + //************************************************************************* + /// Returns an iterator to the beginning of the array. + //************************************************************************* + inline iterator begin() + { + return &_buffer[0]; + } + + //************************************************************************* + /// Returns a const iterator to the beginning of the array. + //************************************************************************* + inline const_iterator begin() const + { + return &_buffer[0]; + } + + //************************************************************************* + /// Returns a const iterator to the beginning of the array. + //************************************************************************* + inline const_iterator cbegin() const + { + return begin(); + } + + //************************************************************************* + /// Returns an iterator to the end of the array. + //************************************************************************* + inline iterator end() + { + return &_buffer[SIZE]; + } + + //************************************************************************* + /// Returns a const iterator to the end of the array. + //************************************************************************* + inline const_iterator end() const + { + return &_buffer[SIZE]; + } + + //************************************************************************* + // Returns a const iterator to the end of the array. + //************************************************************************* + inline const_iterator cend() const + { + return &_buffer[SIZE]; + } + + //************************************************************************* + // Returns an reverse iterator to the reverse beginning of the array. + //************************************************************************* + inline reverse_iterator rbegin() + { + return reverse_iterator(end()); + } + + //************************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the array. + //************************************************************************* + inline const_reverse_iterator rbegin() const + { + return const_reverse_iterator(end()); + } + + //************************************************************************* + /// Returns a const reverse iterator to the reverse beginning of the array. + //************************************************************************* + inline const_reverse_iterator crbegin() const + { + return const_reverse_iterator(end()); + } + + //************************************************************************* + /// Returns a reverse iterator to the end of the array. + //************************************************************************* + inline reverse_iterator rend() + { + return reverse_iterator(begin()); + } + + //************************************************************************* + /// Returns a const reverse iterator to the end of the array. + //************************************************************************* + inline const_reverse_iterator rend() const + { + return const_reverse_iterator(begin()); + } + + //************************************************************************* + /// Returns a const reverse iterator to the end of the array. + //************************************************************************* + inline const_reverse_iterator crend() const + { + return const_reverse_iterator(begin()); + } + + //************************************************************************* + // Capacity + //************************************************************************* + //************************************************************************* /// Returns true if the array size is zero. /// @@ -273,11 +336,15 @@ namespace etl return SIZE; } + //************************************************************************* + // Operations + //************************************************************************* + //************************************************************************* /// Fills the array with the specified value. ///\param value The value to fill the array with. //************************************************************************* - void fill(const T& value) + void fill(parameter_t value) { std::fill(begin(), end(), value); } @@ -293,62 +360,15 @@ namespace etl std::swap(_buffer[i], other._buffer[i]); } } - - //************************************************************************* - /// Returns a reference to the first element. - //************************************************************************* - inline reference front() - { - return _buffer[0]; - } - //************************************************************************* - /// Returns a const reference to the first element. - //************************************************************************* - inline const_reference front() const - { - return _buffer[0]; - } - - //************************************************************************* - /// Returns a reference to the last element. - //************************************************************************* - inline reference back() - { - return _buffer[SIZE - 1]; - } - - //************************************************************************* - /// Returns a const reference to the last element. - //************************************************************************* - inline const_reference back() const - { - return _buffer[SIZE - 1]; - } - - //************************************************************************* - /// Returns a pointer to the first element of the internal buffer. - //************************************************************************* - inline pointer data() - { - return &_buffer[0]; - } - - //************************************************************************* - /// Returns a const pointer to the first element of the internal buffer. - //************************************************************************* - inline const_pointer data() const - { - return &_buffer[0]; - } - - T _buffer[SIZE]; ///< The buffer that stores the array data. - }; + /// The array data. + T _buffer[SIZE]; + }; //************************************************************************* - /// Overloaded std::swap for etl::array - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + /// Overloaded swap for etl::array + ///\param lhs The first array. + ///\param rhs The second array. //************************************************************************* template void swap(etl::array &lhs, etl::array &rhs) @@ -358,8 +378,8 @@ namespace etl //************************************************************************* /// Equal operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the arrays are equal, otherwise false //************************************************************************* template @@ -370,8 +390,8 @@ namespace etl //************************************************************************* /// Not equal operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the arrays are not equal, otherwise false //************************************************************************* template @@ -382,8 +402,8 @@ namespace etl //************************************************************************* /// Less than operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the first array is lexicographically less than the second, otherwise false //************************************************************************* template @@ -397,8 +417,8 @@ namespace etl //************************************************************************* /// Less than or equal operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the first array is lexicographically less than or equal to the second, otherwise false //************************************************************************* template @@ -413,8 +433,8 @@ namespace etl //************************************************************************* /// Greater than operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the first array is lexicographically greater than the second, otherwise false template //************************************************************************* @@ -429,8 +449,8 @@ namespace etl //************************************************************************* /// Greater than or equal operator. - ///\param lhs Reference to the first array. - ///\param rhs Reference to the second array. + ///\param lhs The first array. + ///\param rhs The second array. ///\return true if the first array is lexicographically greater than or equal to the second, otherwise false //************************************************************************* template