diff --git a/deque.h b/deque.h index 1e011c68..a373ddb7 100644 --- a/deque.h +++ b/deque.h @@ -102,7 +102,7 @@ namespace etl //************************************************************************* /// Assigns data to the deque. //************************************************************************* - explicit deque(size_t n, const value_type& value = value_type()) + explicit deque(size_t n, typename ideque::parameter_t value = value_type()) : ideque(&buffer[0], MAX_SIZE, BUFFER_SIZE) { ideque::assign(n, value); diff --git a/ideque.h b/ideque.h index 8b4f47c2..66389348 100644 --- a/ideque.h +++ b/ideque.h @@ -35,6 +35,7 @@ SOFTWARE. #include "type_traits.h" #include "deque_base.h" +#include "parameter_type.h" namespace etl { @@ -48,15 +49,17 @@ namespace etl { public: - typedef T* pointer; - typedef const T* const_pointer; + typedef T value_type; + typedef size_t size_type; typedef T& reference; typedef const T& const_reference; - typedef size_t size_type; - typedef T value_type; + typedef T* pointer; + typedef const T* const_pointer; typedef typename std::iterator_traits::difference_type difference_type; - private: + protected: + + typedef typename parameter_type::value || is_pointer::value>::type parameter_t; //************************************************************************* /// Test for an iterator. @@ -453,109 +456,13 @@ namespace etl typedef std::reverse_iterator const_reverse_iterator; //************************************************************************* - /// Clears the deque. + /// Assignment operator. //************************************************************************* - void clear() + ideque& operator =(const ideque& other) { - first = iterator(0, *this, p_buffer); - last = iterator(0, *this, p_buffer); - current_size = 0; - } + assign(other.begin(), other.end()); - //************************************************************************* - /// Gets an iterator to the beginning of the deque. - //************************************************************************* - iterator begin() - { - return first; - } - - //************************************************************************* - /// Gets an iterator to the end of the deque. - //************************************************************************* - iterator end() - { - return ++iterator(last); - } - - //************************************************************************* - /// Gets a const iterator to the beginning of the deque. - //************************************************************************* - const_iterator begin() const - { - return first; - } - - //************************************************************************* - /// Gets a const iterator to the end of the deque. - //************************************************************************* - const_iterator end() const - { - return ++iterator(last); - } - - //************************************************************************* - /// Gets a const iterator to the beginning of the deque. - //************************************************************************* - const_iterator cbegin() const - { - return first; - } - - //************************************************************************* - /// Gets a const iterator to the end of the deque. - //************************************************************************* - const_iterator cend() const - { - return ++const_iterator(last); - } - - //************************************************************************* - /// Gets a reverse iterator to the end of the deque. - //************************************************************************* - reverse_iterator rbegin() - { - return reverse_iterator(end()); - } - - //************************************************************************* - /// Gets a reverse iterator to the beginning of the deque. - //************************************************************************* - reverse_iterator rend() - { - return reverse_iterator(begin()); - } - - //************************************************************************* - /// Gets a const reverse iterator to the end of the deque. - //************************************************************************* - const_reverse_iterator rbegin() const - { - return const_reverse_iterator(end()); - } - - //************************************************************************* - /// Gets a const reverse iterator to the beginning of the deque. - //************************************************************************* - const_reverse_iterator rend() const - { - return const_reverse_iterator(begin()); - } - - //************************************************************************* - /// Gets a const reverse iterator to the end of the deque. - //************************************************************************* - const_reverse_iterator crbegin() const - { - return const_reverse_iterator(cend()); - } - - //************************************************************************* - /// Gets a const reverse iterator to the beginning of the deque. - //************************************************************************* - const_reverse_iterator crend() const - { - return const_reverse_iterator(cbegin()); + return *this; } //************************************************************************* @@ -563,7 +470,7 @@ namespace etl //************************************************************************* template typename etl::enable_if::value, void>::type - assign(TIterator range_begin, TIterator range_end) + assign(TIterator range_begin, TIterator range_end) { clear(); @@ -593,170 +500,74 @@ namespace etl std::fill_n(p_buffer, n, value); first.index = 0; - last.index = n - 1; - current_size = n; + last.index = n - 1; + current_size = n; } //************************************************************************* - /// Resizes the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is 'new_size' is too large. - ///\param new_size The new size of the deque. - ///\param value The value to assign if the new size is larger. Default = Default constructed value. + /// Gets a reference to the item at the index. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + ///\return A reference to the item at the index. //************************************************************************* - void resize(size_t new_size, const value_type& value = value_type()) + reference at(size_t index) { - if (new_size <= MAX_SIZE) - { - // Make it smaller? - if (new_size < current_size) - { - last -= (current_size - new_size); - } - // Make it larger? - else if (new_size > current_size) - { - size_t count = new_size - current_size; - - for (size_t i = 0; i < count; ++i) - { - *(++last) = value; - } - } - - current_size = new_size; - } #ifdef ETL_USE_EXCEPTIONS - else + if (index >= current_size) { throw deque_out_of_bounds(); } #endif + + iterator result(first); + result += index; + + return *result; } //************************************************************************* - /// Adds an item to the front of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. - ///\param item The item to push to the deque. + /// Gets a const reference to the item at the index. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + ///\return A const reference to the item at the index. //************************************************************************* - void push_front(const_reference item) + const_reference at(size_t index) const { - if (!full()) - { - if (!empty()) - { - --first; - } - - *first = item; - ++current_size; - } #ifdef ETL_USE_EXCEPTIONS - else + if (index >= current_size) { - throw deque_full(); - } -#endif - } - - //************************************************************************* - /// Adds one to the front of the deque and returns a reference to the new element. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. - ///\return A reference to the item to assign to. - //************************************************************************* - reference push_front() - { - if (!full()) - { - if (!empty()) - { - --first; - } - - ++current_size; - } -#ifdef ETL_USE_EXCEPTIONS - else - { - throw deque_full(); + throw deque_out_of_bounds(); } #endif - return *first; + iterator result(first); + result += index; + + return *result; + } + + //************************************************************************* + /// Gets a reference to the item at the index. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + ///\return A reference to the item at the index. + //************************************************************************* + reference operator [](size_t index) + { + iterator result(first); + result += index; + + return *result; } //************************************************************************* - /// Adds an item to the back of the deque. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. - ///\param "item The item to push to the deque. + /// Gets a const reference to the item at the index. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. + ///\return A const reference to the item at the index. //************************************************************************* - void push_back(const_reference item) + const_reference operator [](size_t index) const { - if (!full()) - { - if (!empty()) - { - ++last; - } + iterator result(first); + result += index; - *last = item; - ++current_size; - } -#ifdef ETL_USE_EXCEPTIONS - else - { - throw deque_full(); - } -#endif - } - - //************************************************************************* - /// Adds one to the front of the deque and returns a reference to the new element. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. - ///\return A reference to the item to assign to. - //************************************************************************* - reference push_back() - { - if (!full()) - { - if (!empty()) - { - ++last; - } - - ++current_size; - } -#ifdef ETL_USE_EXCEPTIONS - else - { - throw deque_full(); - } -#endif - - return *last; - } - - //************************************************************************* - /// Removes the oldest item from the deque. - //************************************************************************* - void pop_front() - { - if (!empty()) - { - ++first; - --current_size; - } - } - - //************************************************************************* - /// Removes the oldest item from the deque. - //************************************************************************* - void pop_back() - { - if (!empty()) - { - --last; - --current_size; - } + return *result; } //************************************************************************* @@ -828,43 +639,109 @@ namespace etl } //************************************************************************* - /// Gets a reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. - ///\return A reference to the item at the index. + /// Gets an iterator to the beginning of the deque. //************************************************************************* - reference operator [](size_t index) + iterator begin() { -#ifdef ETL_USE_EXCEPTIONS - if (index >= current_size) - { - throw deque_out_of_bounds(); - } -#endif - - iterator result(first); - result += index; - - return *result; + return first; } //************************************************************************* - /// Gets a const reference to the item at the index. - /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_out_of_bounds if the index is out of range. - ///\return A const reference to the item at the index. + /// Gets a const iterator to the beginning of the deque. //************************************************************************* - const_reference operator [](size_t index) const + const_iterator begin() const { -#ifdef ETL_USE_EXCEPTIONS - if (index >= current_size) - { - throw deque_out_of_bounds(); - } -#endif + return first; + } + + //************************************************************************* + /// Gets a const iterator to the beginning of the deque. + //************************************************************************* + const_iterator cbegin() const + { + return first; + } - iterator result(first); - result += index; + //************************************************************************* + /// Gets an iterator to the end of the deque. + //************************************************************************* + iterator end() + { + return ++iterator(last); + } - return *result; + //************************************************************************* + /// Gets a const iterator to the end of the deque. + //************************************************************************* + const_iterator end() const + { + return ++iterator(last); + } + + //************************************************************************* + /// Gets a const iterator to the end of the deque. + //************************************************************************* + const_iterator cend() const + { + return ++const_iterator(last); + } + + //************************************************************************* + /// Gets a reverse iterator to the end of the deque. + //************************************************************************* + reverse_iterator rbegin() + { + return reverse_iterator(end()); + } + + //************************************************************************* + /// Gets a const reverse iterator to the end of the deque. + //************************************************************************* + const_reverse_iterator rbegin() const + { + return const_reverse_iterator(end()); + } + + //************************************************************************* + /// Gets a const reverse iterator to the end of the deque. + //************************************************************************* + const_reverse_iterator crbegin() const + { + return const_reverse_iterator(cend()); + } + + //************************************************************************* + /// Gets a reverse iterator to the beginning of the deque. + //************************************************************************* + reverse_iterator rend() + { + return reverse_iterator(begin()); + } + + //************************************************************************* + /// Gets a const reverse iterator to the beginning of the deque. + //************************************************************************* + const_reverse_iterator rend() const + { + return const_reverse_iterator(begin()); + } + + //************************************************************************* + /// Gets a const reverse iterator to the beginning of the deque. + //************************************************************************* + const_reverse_iterator crend() const + { + return const_reverse_iterator(cbegin()); + } + + //************************************************************************* + /// Clears the deque. + //************************************************************************* + void clear() + { + first = iterator(0, *this, p_buffer); + last = iterator(0, *this, p_buffer); + current_size = 0; } //************************************************************************* @@ -1177,7 +1054,7 @@ namespace etl position += length; } else - // Must be closer to the back. + // Must be closer to the back. { // Move the items. std::copy(position + length, last + 1, position); @@ -1198,13 +1075,173 @@ namespace etl } //************************************************************************* - /// Assignment operator. + /// Adds an item to the back of the deque. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + ///\param item The item to push to the deque. //************************************************************************* - ideque& operator =(const ideque& other) + void push_back(parameter_t item) { - assign(other.begin(), other.end()); + if (!full()) + { + if (!empty()) + { + ++last; + } - return *this; + *last = item; + ++current_size; + } +#ifdef ETL_USE_EXCEPTIONS + else + { + throw deque_full(); + } +#endif + } + + //************************************************************************* + /// Adds one to the front of the deque and returns a reference to the new element. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + ///\return A reference to the item to assign to. + //************************************************************************* + reference push_back() + { + if (!full()) + { + if (!empty()) + { + ++last; + } + + ++current_size; + } +#ifdef ETL_USE_EXCEPTIONS + else + { + throw deque_full(); + } +#endif + + return *last; + } + + //************************************************************************* + /// Removes the oldest item from the deque. + //************************************************************************* + void pop_back() + { + if (!empty()) + { + --current_size; + + if (!empty()) + { + --last; + } + } + } + + //************************************************************************* + /// Adds an item to the front of the deque. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + ///\param item The item to push to the deque. + //************************************************************************* + void push_front(parameter_t item) + { + if (!full()) + { + if (!empty()) + { + --first; + } + + *first = item; + ++current_size; + } +#ifdef ETL_USE_EXCEPTIONS + else + { + throw deque_full(); + } +#endif + } + + //************************************************************************* + /// Adds one to the front of the deque and returns a reference to the new element. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is the deque is already full. + ///\return A reference to the item to assign to. + //************************************************************************* + reference push_front() + { + if (!full()) + { + if (!empty()) + { + --first; + } + + ++current_size; + } +#ifdef ETL_USE_EXCEPTIONS + else + { + throw deque_full(); + } +#endif + + return *first; + } + + //************************************************************************* + /// Removes the oldest item from the deque. + //************************************************************************* + void pop_front() + { + if (!empty()) + { + --current_size; + + if (!empty()) + { + ++first; + } + } + } + + //************************************************************************* + /// Resizes the deque. + /// If ETL_USE_EXCEPTIONS is defined, throws an etl::deque_full is 'new_size' is too large. + ///\param new_size The new size of the deque. + ///\param value The value to assign if the new size is larger. Default = Default constructed value. + //************************************************************************* + void resize(size_t new_size, const value_type& value = value_type()) + { + if (new_size <= MAX_SIZE) + { + // Make it smaller? + if (new_size < current_size) + { + last -= (current_size - new_size); + } + // Make it larger? + else if (new_size > current_size) + { + size_t count = new_size - current_size; + + for (size_t i = 0; i < count; ++i) + { + *(++last) = value; + } + } + + current_size = new_size; + } +#ifdef ETL_USE_EXCEPTIONS + else + { + throw deque_out_of_bounds(); + } +#endif } //************************************************************************* @@ -1239,37 +1276,21 @@ namespace etl return distance(lhs.base(), rhs.base()); } - //************************************************************************* - /// Equality operator. - //************************************************************************* - friend bool operator ==(const ideque& lhs, const ideque& rhs) - { - return (lhs.size() == rhs.size()) && std::equal(lhs.begin(), lhs.end(), rhs.begin()); - } - - //************************************************************************* - /// Inequality operator. - //************************************************************************* - friend bool operator !=(const ideque& lhs, const ideque& rhs) - { - return !(lhs == rhs); - } - protected: //************************************************************************* /// Constructor. //************************************************************************* - ideque(T* p_buffer, size_t max_size, size_t buffer_size) + ideque(pointer p_buffer, size_t max_size, size_t buffer_size) : deque_base(max_size, buffer_size), p_buffer(p_buffer) { clear(); } - iterator first; ///Iterator to the first item in the deque. - iterator last; ///Iterator to the last item in the deque. - T* p_buffer; ///The buffer. + iterator first; ///Iterator to the first item in the deque. + iterator last; ///Iterator to the last item in the deque. + pointer p_buffer; ///The buffer for the deque. private: @@ -1289,11 +1310,11 @@ namespace etl /// Measures the distance from the first iterator to the specified iterator. //************************************************************************* template - static difference_type distance(const TIterator& it) + static difference_type distance(const TIterator& other) { - const difference_type index = it.get_index(); - const difference_type reference_index = it.get_deque().first.index; - const size_t buffer_size = it.get_deque().BUFFER_SIZE; + const difference_type index = other.get_index(); + const difference_type reference_index = other.get_deque().first.index; + const size_t buffer_size = other.get_deque().BUFFER_SIZE; if (index < reference_index) { @@ -1309,8 +1330,8 @@ namespace etl //*************************************************************************** /// Equal operator. -///\param lhs Reference to the first array. -///\param rhs Reference to the second array. +///\param lhs Reference to the first deque. +///\param rhs Reference to the second deque. ///\return true if the arrays are equal, otherwise false ///\ingroup deque //*************************************************************************** @@ -1322,8 +1343,8 @@ bool operator ==(const etl::ideque& lhs, const etl::ideque& rhs) //*************************************************************************** /// Not equal operator. -///\param lhs Reference to the first array. -///\param rhs Reference to the second array. +///\param lhs Reference to the first deque. +///\param rhs Reference to the second deque. ///\return true if the arrays are not equal, otherwise false ///\ingroup deque //*************************************************************************** @@ -1335,9 +1356,9 @@ bool operator !=(const etl::ideque& lhs, const etl::ideque& rhs) //*************************************************************************** /// Less than operator. -///\param lhs Reference to the first array. -///\param rhs Reference to the second array. -///\return true if the first array is lexigraphically less than the second, otherwise false +///\param lhs Reference to the first deque. +///\param rhs Reference to the second deque. +///\return true if the first deque is lexigraphically less than the second, otherwise false ///\ingroup deque //*************************************************************************** template @@ -1349,11 +1370,24 @@ bool operator <(const etl::ideque& lhs, const etl::ideque& rhs) rhs.end()); } +//*************************************************************************** +/// Less than or equal operator. +///\param lhs Reference to the first deque. +///\param rhs Reference to the second deque. +///\return true if the first deque is lexigraphically less than or equal to the second, otherwise false +///\ingroup deque +//*************************************************************************** +template +bool operator <=(const etl::ideque& lhs, const etl::ideque& rhs) +{ + return !operator >(lhs, rhs); +} + //*************************************************************************** /// Greater than operator. -///\param lhs Reference to the first array. -///\param rhs Reference to the second array. -///\return true if the first array is lexigraphically greater than the second, otherwise false +///\param lhs Reference to the first deque. +///\param rhs Reference to the second deque. +///\return true if the first deque is lexigraphically greater than the second, otherwise false ///\ingroup deque //*************************************************************************** template @@ -1366,24 +1400,11 @@ bool operator >(const etl::ideque& lhs, const etl::ideque& rhs) std::greater()); } -//*************************************************************************** -/// Less than or equal operator. -///\param lhs Reference to the first array. -///\param rhs Reference to the second array. -///\return true if the first array is lexigraphically less than or equal to the second, otherwise false -///\ingroup deque -//*************************************************************************** -template -bool operator <=(const etl::ideque& lhs, const etl::ideque& rhs) -{ - return !operator >(lhs, rhs); -} - //*************************************************************************** /// Greater than or equal operator. -///\param "lhs Reference to the first array. -///\param "rhs Reference to the second array. -///\return true if the first array is lexigraphically greater than or equal to the second, otherwise false +///\param "lhs Reference to the first deque. +///\param "rhs Reference to the second deque. +///\return true if the first deque is lexigraphically greater than or equal to the second, otherwise false ///\ingroup deque //*************************************************************************** template diff --git a/ilist.h b/ilist.h index 9ebff971..01b131be 100644 --- a/ilist.h +++ b/ilist.h @@ -41,6 +41,8 @@ SOFTWARE. #include "nullptr.h" #include "list_base.h" +#include "type_traits.h" +#include "parameter_type.h" namespace etl { @@ -51,8 +53,20 @@ namespace etl template class ilist : public list_base { + public: + + typedef T value_type; + typedef T* pointer; + typedef const T* const_pointer; + typedef T& reference; + typedef const T& const_reference; + typedef size_t size_type; + protected: + typedef typename parameter_type::value || is_pointer::value>::type parameter_t; + + //************************************************************************* /// The node element in the list. //************************************************************************* @@ -146,13 +160,6 @@ namespace etl public: - typedef T value_type; - typedef T* pointer; - typedef const T* const_pointer; - typedef T& reference; - typedef const T& const_reference; - typedef size_t size_type; - //************************************************************************* /// iterator. //************************************************************************* @@ -330,11 +337,6 @@ namespace etl return ilist::data_cast(p_node)->value; } - inline Data_Node* operator ->() - { - return p_node; - } - inline const Data_Node* operator ->() const { return p_node; @@ -360,6 +362,16 @@ namespace etl typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; + //************************************************************************* + /// Assignment operator. + //************************************************************************* + inline ilist& operator = (const ilist& rhs) + { + assign(rhs.cbegin(), rhs.cend()); + + return *this; + } + //************************************************************************* /// Gets the beginning of the list. //************************************************************************* @@ -376,38 +388,6 @@ namespace etl return const_iterator(get_head()); } - //************************************************************************* - /// Gets the beginning of the list. - //************************************************************************* - inline const_iterator cbegin() const - { - return const_iterator(get_head()); - } - - //************************************************************************* - /// Gets the reverse beginning of the list. - //************************************************************************* - inline reverse_iterator rbegin() - { - return reverse_iterator(terminal_node); - } - - //************************************************************************* - /// Gets the reverse beginning of the list. - //************************************************************************* - inline const_reverse_iterator rbegin() const - { - return const_reverse_iterator(static_cast(terminal_node)); - } - - //************************************************************************* - /// Gets the reverse beginning of the list. - //************************************************************************* - inline const_reverse_iterator crbegin() const - { - return const_reverse_iterator(static_cast(terminal_node)); - } - //************************************************************************* /// Gets the end of the list. //************************************************************************* @@ -424,6 +404,14 @@ namespace etl return const_iterator(static_cast(terminal_node)); } + //************************************************************************* + /// Gets the beginning of the list. + //************************************************************************* + inline const_iterator cbegin() const + { + return const_iterator(get_head()); + } + //************************************************************************* /// Gets the end of the list. //************************************************************************* @@ -432,6 +420,22 @@ namespace etl return const_iterator(static_cast(terminal_node)); } + //************************************************************************* + /// Gets the reverse beginning of the list. + //************************************************************************* + inline reverse_iterator rbegin() + { + return reverse_iterator(terminal_node); + } + + //************************************************************************* + /// Gets the reverse beginning of the list. + //************************************************************************* + inline const_reverse_iterator rbegin() const + { + return const_reverse_iterator(static_cast(terminal_node)); + } + //************************************************************************* /// Gets the reverse end of the list. //************************************************************************* @@ -441,11 +445,11 @@ namespace etl } //************************************************************************* - /// Gets the reverse end of the list. + /// Gets the reverse beginning of the list. //************************************************************************* - inline const_reverse_iterator rend() const + inline const_reverse_iterator crbegin() const { - return const_reverse_iterator(get_head()); + return const_reverse_iterator(static_cast(terminal_node)); } //************************************************************************* @@ -456,24 +460,6 @@ namespace etl return const_reverse_iterator(get_head()); } - //************************************************************************* - /// Assignment operator. - //************************************************************************* - inline ilist& operator = (const ilist& rhs) - { - assign(rhs.cbegin(), rhs.cend()); - - return *this; - } - - //************************************************************************* - /// Clears the list. - //************************************************************************* - inline void clear() - { - initialise(); - } - //************************************************************************* /// Gets a reference to the first element. //************************************************************************* @@ -550,7 +536,7 @@ namespace etl //************************************************************************* /// Assigns 'n' copies of a value to the list. //************************************************************************* - void assign(size_t n, const_reference value) + void assign(size_t n, parameter_t value) { // Reset the links. join(terminal_node, terminal_node); @@ -606,7 +592,7 @@ namespace etl //************************************************************************* /// Pushes a value to the front of the list. //************************************************************************* - void push_front(const_reference value) + void push_front(parameter_t value) { if (!full()) { @@ -622,6 +608,17 @@ namespace etl #endif } + //************************************************************************* + /// Removes a value from the front of the list. + //************************************************************************* + void pop_front() + { + if (!empty()) + { + remove_node(get_head()); + } + } + //************************************************************************* /// Adds a node to the back of the list so a new value can be assigned to back(). //************************************************************************* @@ -643,7 +640,7 @@ namespace etl //************************************************************************* /// Pushes a value to the back of the list.. //************************************************************************* - void push_back(const_reference value) + void push_back(parameter_t value) { if (!full()) { @@ -659,17 +656,6 @@ namespace etl #endif } - //************************************************************************* - /// Removes a value from the front of the list. - //************************************************************************* - void pop_front() - { - if (!empty()) - { - remove_node(get_head()); - } - } - //************************************************************************* /// Removes a value from the back of the list. //************************************************************************* @@ -681,64 +667,6 @@ namespace etl } } - //************************************************************************* - /// Resizes the list. - //************************************************************************* - void resize(size_t n) - { - resize(n, T()); - } - - //************************************************************************* - /// Resizes the list. - //************************************************************************* - void resize(size_t n, T value) - { - if (n > MAX_SIZE) - { -#if ETL_USE_EXCEPTIONS - throw list_full(); -#else - n = MAX_SIZE; -#endif - } - - // Smaller? - if (n < size()) - { - iterator i_start = end(); - std::advance(i_start, -difference_type(size() - n)); - erase(i_start, end()); - } - // Larger? - else if (n > size()) - { - insert(end(), n - size(), value); - } - } - - //************************************************************************* - /// Reverses the list. - //************************************************************************* - void reverse() - { - if (is_trivial_list()) - { - return; - } - - iterator i_item = begin(); - - while (i_item != end()) - { - i_item.p_node->reverse(); - --i_item; // Now we've reversed it, we must decrement it. - } - - // Terminal node. - i_item.p_node->reverse(); - } - //************************************************************************* /// Inserts a value to the list at the specified position. //************************************************************************* @@ -856,6 +784,91 @@ namespace etl return last; } + //************************************************************************* + /// Resizes the list. + //************************************************************************* + void resize(size_t n) + { + resize(n, T()); + } + + //************************************************************************* + /// Resizes the list. + //************************************************************************* + void resize(size_t n, T value) + { + if (n > MAX_SIZE) + { +#if ETL_USE_EXCEPTIONS + throw list_full(); +#else + n = MAX_SIZE; +#endif + } + + // Smaller? + if (n < size()) + { + iterator i_start = end(); + std::advance(i_start, -difference_type(size() - n)); + erase(i_start, end()); + } + // Larger? + else if (n > size()) + { + insert(end(), n - size(), value); + } + } + + //************************************************************************* + /// Clears the list. + //************************************************************************* + inline void clear() + { + initialise(); + } + + //************************************************************************* + // Removes the values specified. + //************************************************************************* + void remove(const value_type& value) + { + iterator iValue = begin(); + + while (iValue != end()) + { + if (value == *iValue) + { + iValue = erase(iValue); + } + else + { + ++iValue; + } + } + } + + //************************************************************************* + /// Removes according to a predicate. + //************************************************************************* + template + void remove_if(TPredicate predicate) + { + iterator iValue = begin(); + + while (iValue != end()) + { + if (predicate(*iValue)) + { + iValue = erase(iValue); + } + else + { + ++iValue; + } + } + } + //************************************************************************* /// Removes all but the first element from every consecutive group of equal /// elements in the container. @@ -1018,44 +1031,25 @@ namespace etl } //************************************************************************* - // Removes the values specified. + /// Reverses the list. //************************************************************************* - void remove(const value_type& value) + void reverse() { - iterator iValue = begin(); - - while (iValue != end()) + if (is_trivial_list()) { - if (value == *iValue) - { - iValue = erase(iValue); - } - else - { - ++iValue; - } + return; } - } - //************************************************************************* - /// Removes according to a predicate. - //************************************************************************* - template - void remove_if(TPredicate predicate) - { - iterator iValue = begin(); + iterator i_item = begin(); - while (iValue != end()) + while (i_item != end()) { - if (predicate(*iValue)) - { - iValue = erase(iValue); - } - else - { - ++iValue; - } + i_item.p_node->reverse(); + --i_item; // Now we've reversed it, we must decrement it. } + + // Terminal node. + i_item.p_node->reverse(); } protected: diff --git a/iqueue.h b/iqueue.h index 34ae1983..8ffe35ee 100644 --- a/iqueue.h +++ b/iqueue.h @@ -33,6 +33,8 @@ SOFTWARE. #include #include "queue_base.h" +#include "type_traits.h" +#include "parameter_type.h" namespace etl { @@ -57,7 +59,13 @@ namespace etl typedef T& reference; ///< A reference to the type used in the queue. typedef const T& const_reference; ///< A const reference to the type used in the queue. typedef T* pointer; ///< A pointer to the type used in the queue. - typedef const T* const_pointer; ///< A const pointer to the type used in the queue. + typedef const T* const_pointer; ///< A const pointer to the type used in the qu + + private: + + typedef typename parameter_type::value || is_pointer::value>::type parameter_t; + + public: //************************************************************************* /// Adds an item to the queue. @@ -65,7 +73,7 @@ namespace etl /// otherwise does nothing if full. ///\param item The item to push to the queue. //************************************************************************* - void push(const_reference item) + void push(parameter_t item) { if (!full()) { diff --git a/vector.h b/vector.h index d30facf1..3d4e95b7 100644 --- a/vector.h +++ b/vector.h @@ -81,7 +81,7 @@ namespace etl ///\param initialSize The initial size of the vector. ///\param value The value to fill the vector with. //************************************************************************* - vector(size_t initialSize, const T& value) + vector(size_t initialSize, typename ivector::parameter_t value) : ivector(buffer, MAX_SIZE) { ivector::resize(initialSize, value);