From 1c0561753565a95dff3a0a1bd7c419bd5c8d23a6 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sun, 13 Nov 2016 16:26:19 +0000 Subject: [PATCH] Moved counter type to common class. Changed some parameter names. --- src/intrusive_list.h | 172 ++++++++++--------------------------------- 1 file changed, 38 insertions(+), 134 deletions(-) diff --git a/src/intrusive_list.h b/src/intrusive_list.h index 005c1511..9e151c8f 100644 --- a/src/intrusive_list.h +++ b/src/intrusive_list.h @@ -49,6 +49,7 @@ SOFTWARE. #include "intrusive_links.h" #include "static_assert.h" #include "algorithm.h" +#include "private/counter_type.h" #undef ETL_FILE #define ETL_FILE "21" @@ -351,6 +352,14 @@ namespace etl initialise(); } + //************************************************************************* + /// Destructor. + //************************************************************************* + ~intrusive_list() + { + clear(); + } + //************************************************************************* /// Constructor from range //************************************************************************* @@ -413,6 +422,11 @@ namespace etl //************************************************************************* void clear() { + if (TLink::OPTION == etl::link_option::CHECKED) + { + erase(begin(), end()); + } + initialise(); } @@ -537,33 +551,21 @@ namespace etl //************************************************************************* /// Inserts a value to the intrusive_list before the specified position. - /// Checks that the value is unlinked if CHECKED //************************************************************************* iterator insert(iterator position, value_type& value) { - if (TLink::OPTION == etl::link_option::CHECKED) - { - ETL_ASSERT(!value.TLink::is_linked(), ETL_ERROR(etl::not_unlinked_exception)); - } - insert_link(position.p_value->link_type::etl_previous, value); return iterator(value); } //************************************************************************* /// Inserts a range of values to the intrusive_list after the specified position. - /// Checks that the values are unlinked if CHECKED. //************************************************************************* template void insert(iterator position, TIterator first, TIterator last) { while (first != last) { - if (TLink::OPTION == etl::link_option::CHECKED) - { - ETL_ASSERT(!position.p_value->TLink::is_linked(), ETL_ERROR(etl::not_unlinked_exception)); - } - // Set up the next free link. insert_link(*position.p_value->link_type::etl_previous, *first++); } @@ -844,21 +846,21 @@ namespace etl //************************************************************************* /// Splice another list into this one. //************************************************************************* - void splice(iterator position, list_type& list) + void splice(iterator position, list_type& other) { // No point splicing to ourself! - if (&list != this) + if (&other != this) { - if (!list.empty()) + if (!other.empty()) { - link_type& first = *list.get_head(); - link_type& last = *list.get_tail(); + link_type& first = *other.get_head(); + link_type& last = *other.get_tail(); if (COUNT_OPTION == etl::count_option::FAST_COUNT) { - if (&list != this) + if (&other != this) { - current_size += list.size(); + current_size += other.size(); } } @@ -868,7 +870,7 @@ namespace etl etl::link(before, first); etl::link(last, after); - list.clear(); + other.initialise(); } } } @@ -876,7 +878,7 @@ namespace etl //************************************************************************* /// Splice an element from another list into this one. //************************************************************************* - void splice(iterator position, list_type& list, iterator isource) + void splice(iterator position, list_type& other, iterator isource) { link_type& before = *position.p_value->link_type::etl_previous; @@ -885,10 +887,10 @@ namespace etl if (COUNT_OPTION == etl::count_option::FAST_COUNT) { - if (&list != this) + if (&other != this) { ++current_size; - --list.current_size; + --other.current_size; } } } @@ -896,17 +898,17 @@ namespace etl //************************************************************************* /// Splice a range of elements from another list into this one. //************************************************************************* - void splice(iterator position, list_type& list, iterator begin_, iterator end_) + void splice(iterator position, list_type& other, iterator begin_, iterator end_) { - if (!list.empty()) + if (!other.empty()) { if (COUNT_OPTION == etl::count_option::FAST_COUNT) { - if (&list != this) + if (&other != this) { size_t n = std::distance(begin_, end_); current_size += n; - list.current_size -= n; + other.current_size -= n; } } @@ -926,26 +928,26 @@ namespace etl //************************************************************************* /// Merge another list into this one. Both lists should be sorted. //************************************************************************* - void merge(list_type& list) + void merge(list_type& other) { - merge(list, std::less()); + merge(other, std::less()); } //************************************************************************* /// Merge another list into this one. Both lists should be sorted. //************************************************************************* template - void merge(list_type& list, TCompare compare) + void merge(list_type& other, TCompare compare) { - if (!list.empty()) + if (!other.empty()) { #if _DEBUG - ETL_ASSERT(etl::is_sorted(list.begin(), list.end(), compare), ETL_ERROR(intrusive_list_unsorted)); + ETL_ASSERT(etl::is_sorted(other.begin(), other.end(), compare), ETL_ERROR(intrusive_list_unsorted)); ETL_ASSERT(etl::is_sorted(begin(), end(), compare), ETL_ERROR(intrusive_list_unsorted)); #endif - value_type* other_begin = static_cast(list.get_head()); - value_type* other_end = static_cast(&list.terminal_link); + value_type* other_begin = static_cast(other.get_head()); + value_type* other_end = static_cast(&other.terminal_link); value_type* begin = static_cast(get_head()); value_type* end = static_cast(&terminal_link); @@ -979,10 +981,10 @@ namespace etl if (COUNT_OPTION == etl::count_option::FAST_COUNT) { - current_size += list.size(); + current_size += other.size(); } - list.clear(); + other.initialise(); } } @@ -991,104 +993,6 @@ namespace etl /// The link that acts as the intrusive_list start & end. link_type terminal_link; - //************************************************************************* - /// Counter type based on count option. - //************************************************************************* - template - class counter_type - { - }; - - //************************************************************************* - /// Slow type. - //************************************************************************* - template - class counter_type - { - public: - - counter_type& operator ++() - { - return *this; - } - - counter_type& operator --() - { - return *this; - } - - counter_type& operator =(size_t new_count) - { - return *this; - } - - counter_type& operator +=(size_t diff) - { - return *this; - } - - counter_type& operator -=(size_t diff) - { - return *this; - } - - size_t get_count() const - { - return 0; - } - }; - - //************************************************************************* - /// Fast type. - //************************************************************************* - template - class counter_type - { - public: - - counter_type() - : count(0) - { - } - - counter_type& operator ++() - { - ++count; - return *this; - } - - counter_type& operator --() - { - --count; - return *this; - } - - counter_type& operator =(size_t new_count) - { - count = new_count; - return *this; - } - - counter_type& operator +=(size_t diff) - { - count += diff; - return *this; - } - - counter_type& operator -=(size_t diff) - { - count -= diff; - return *this; - } - - size_t get_count() const - { - return count; - } - - size_t count; - }; - counter_type current_size; ///< Counts the number of elements in the list. //*************************************************************************