Added iterator comparisons

This commit is contained in:
John Wellbelove 2020-08-06 20:45:52 +01:00
parent 8b334c8a97
commit a0a86aa748
2 changed files with 86 additions and 13 deletions

View File

@ -252,7 +252,7 @@ namespace etl
};
public:
//*************************************************************************
/// Iterator
//*************************************************************************
@ -377,12 +377,6 @@ namespace etl
return &p_buffer[index];
}
//***************************************************
bool operator <(const iterator& other) const
{
return ideque::distance(*this, other) > 0;
}
//***************************************************
friend iterator operator +(const iterator& lhs, difference_type offset)
{
@ -411,6 +405,38 @@ namespace etl
return !(lhs == rhs);
}
//***************************************************
friend bool operator < (const iterator& lhs, const iterator& rhs)
{
const difference_type lhs_index = lhs.get_index();
const difference_type rhs_index = rhs.get_index();
const difference_type reference_index = lhs.get_deque().begin().get_index();
const size_t buffer_size = lhs.get_deque().max_size() + 1;
const difference_type lhs_distance = (lhs_index < reference_index) ? buffer_size + lhs_index - reference_index : lhs_index - reference_index;
const difference_type rhs_distance = (rhs_index < reference_index) ? buffer_size + rhs_index - reference_index : rhs_index - reference_index;
return lhs_distance < rhs_distance;
}
//***************************************************
friend bool operator <= (const iterator& lhs, const iterator& rhs)
{
return !(lhs > rhs);
}
//***************************************************
friend bool operator > (const iterator& lhs, const iterator& rhs)
{
return (rhs < lhs);
}
//***************************************************
friend bool operator >= (const iterator& lhs, const iterator& rhs)
{
return !(lhs < rhs);
}
//***************************************************
difference_type get_index() const
{
@ -439,6 +465,19 @@ namespace etl
private:
//***************************************************
difference_type distance(difference_type firstIndex, difference_type index_) const
{
if (index_ < firstIndex)
{
return p_deque->BUFFER_SIZE + index_ - firstIndex;
}
else
{
return index_ - firstIndex;
}
}
//***************************************************
iterator(difference_type index_, ideque& the_deque, pointer p_buffer_)
: index(index_)
@ -580,11 +619,7 @@ namespace etl
return &p_buffer[index];
}
//***************************************************
bool operator <(const const_iterator& other) const
{
return ideque::distance(*this, other) > 0;
}
//***************************************************
friend const_iterator operator +(const const_iterator& lhs, difference_type offset)
@ -614,6 +649,38 @@ namespace etl
return !(lhs == rhs);
}
//***************************************************
friend bool operator < (const const_iterator& lhs, const const_iterator& rhs)
{
const difference_type lhs_index = lhs.get_index();
const difference_type rhs_index = rhs.get_index();
const difference_type reference_index = lhs.get_deque().begin().get_index();
const size_t buffer_size = lhs.get_deque().max_size() + 1;
const difference_type lhs_distance = (lhs_index < reference_index) ? buffer_size + lhs_index - reference_index : lhs_index - reference_index;
const difference_type rhs_distance = (rhs_index < reference_index) ? buffer_size + rhs_index - reference_index : rhs_index - reference_index;
return lhs_distance < rhs_distance;
}
//***************************************************
friend bool operator <= (const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs > rhs);
}
//***************************************************
friend bool operator > (const const_iterator& lhs, const const_iterator& rhs)
{
return (rhs < lhs);
}
//***************************************************
friend bool operator >= (const const_iterator& lhs, const const_iterator& rhs)
{
return !(lhs < rhs);
}
//***************************************************
difference_type get_index() const
{
@ -641,7 +708,7 @@ namespace etl
private:
//***************************************************
difference_type distance(difference_type firstIndex, difference_type index_)
difference_type distance(difference_type firstIndex, difference_type index_) const
{
if (index_ < firstIndex)
{

View File

@ -538,6 +538,9 @@ namespace
CHECK(first < second);
CHECK(!(second < first));
CHECK(second > first);
CHECK(!(first > second));
}
//*************************************************************************
@ -550,6 +553,9 @@ namespace
CHECK(first < second);
CHECK(!(second < first));
CHECK(second > first);
CHECK(!(first > second));
}
//*************************************************************************