mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-26 20:38:45 +08:00
Moved counter type to common class.
Changed some parameter names.
This commit is contained in:
parent
e75f4bd248
commit
1c05617535
@ -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 <typename TIterator>
|
||||
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<link_type>(before, first);
|
||||
etl::link<link_type>(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<value_type>());
|
||||
merge(other, std::less<value_type>());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Merge another list into this one. Both lists should be sorted.
|
||||
//*************************************************************************
|
||||
template <typename TCompare>
|
||||
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<value_type*>(list.get_head());
|
||||
value_type* other_end = static_cast<value_type*>(&list.terminal_link);
|
||||
value_type* other_begin = static_cast<value_type*>(other.get_head());
|
||||
value_type* other_end = static_cast<value_type*>(&other.terminal_link);
|
||||
|
||||
value_type* begin = static_cast<value_type*>(get_head());
|
||||
value_type* end = static_cast<value_type*>(&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 <const size_t OPTION, bool dummy = true>
|
||||
class counter_type
|
||||
{
|
||||
};
|
||||
|
||||
//*************************************************************************
|
||||
/// Slow type.
|
||||
//*************************************************************************
|
||||
template <bool dummy>
|
||||
class counter_type<etl::count_option::SLOW_COUNT, dummy>
|
||||
{
|
||||
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 <bool dummy>
|
||||
class counter_type<etl::count_option::FAST_COUNT, dummy>
|
||||
{
|
||||
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<COUNT_OPTION> current_size; ///< Counts the number of elements in the list.
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user