Fixed issue for incorrect operation of erase(const_iterator, const_iterator)

when the terminating iterator was end() for etl::unordered_map, etl::unordered_multimap, etl::unordered_set and etl::unordered_multiset.
This commit is contained in:
John Wellbelove 2020-08-15 21:00:18 +01:00
parent dc501fa2c4
commit bb52c37eca
14 changed files with 271 additions and 42 deletions

View File

@ -1033,14 +1033,19 @@ namespace etl
//*********************************************************************
iterator erase(const_iterator first_, const_iterator last_)
{
// Make a note of the last.
iterator result((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
// Erasing everything?
if ((first_ == begin()) && (last_ == end()))
{
clear();
return end();
}
// Get the starting point.
bucket_t* pbucket = first_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
bucket_t* pbucket = first_.get_bucket_list_iterator();
bucket_t* pend_bucket = last_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
// Find the node previous to the first one.
while (iprevious->etl_next != &*icurrent)
@ -1048,9 +1053,9 @@ namespace etl
++iprevious;
}
while (icurrent != iend)
// Until we reach the end.
while ((icurrent != iend) || (pbucket != pend_bucket))
{
local_iterator inext = pbucket->erase_after(iprevious); // Unlink from the bucket.
icurrent->key_value_pair.~value_type(); // Destroy the value.
pnodepool->release(&*icurrent); // Release it back to the pool.
@ -1059,8 +1064,8 @@ namespace etl
icurrent = inext;
// Are we there yet?
if (icurrent != iend)
// Have we not reached the end?
if ((icurrent != iend) || (pbucket != pend_bucket))
{
// At the end of this bucket?
if ((icurrent == pbucket->end()))
@ -1072,12 +1077,12 @@ namespace etl
} while (pbucket->empty());
iprevious = pbucket->before_begin();
icurrent = pbucket->begin();
icurrent = pbucket->begin();
}
}
}
return result;
return iterator((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
}
//*************************************************************************

View File

@ -909,14 +909,22 @@ namespace etl
//*********************************************************************
iterator erase(const_iterator first_, const_iterator last_)
{
// Erasing everything?
if ((first_ == begin()) && (last_ == end()))
{
clear();
return end();
}
// Make a note of the last.
iterator result((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
// Get the starting point.
bucket_t* pbucket = first_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
bucket_t* pbucket = first_.get_bucket_list_iterator();
bucket_t* pend_bucket = last_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
// Find the node previous to the first one.
while (iprevious->etl_next != &*icurrent)
@ -924,7 +932,8 @@ namespace etl
++iprevious;
}
while (icurrent != iend)
// Until we reach the end.
while ((icurrent != iend) || (pbucket != pend_bucket))
{
local_iterator inext = pbucket->erase_after(iprevious); // Unlink from the bucket.
@ -935,8 +944,8 @@ namespace etl
icurrent = inext;
// Are we there yet?
if (icurrent != iend)
// Have we not reached the end?
if ((icurrent != iend) || (pbucket != pend_bucket))
{
// At the end of this bucket?
if ((icurrent == pbucket->end()))
@ -948,7 +957,7 @@ namespace etl
} while (pbucket->empty());
iprevious = pbucket->before_begin();
icurrent = pbucket->begin();
icurrent = pbucket->begin();
}
}
}

View File

@ -894,22 +894,31 @@ namespace etl
//*********************************************************************
iterator erase(const_iterator first_, const_iterator last_)
{
// Erasing everything?
if ((first_ == begin()) && (last_ == end()))
{
clear();
return end();
}
// Make a note of the last.
iterator result((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
// Get the starting point.
bucket_t* pbucket = first_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
bucket_t* pbucket = first_.get_bucket_list_iterator();
bucket_t* pend_bucket = last_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
// Find the node previous to the first one.
// Find the node previous to the first one.
while (iprevious->etl_next != &*icurrent)
{
++iprevious;
}
while (icurrent != iend)
// Until we reach the end.
while ((icurrent != iend) || (pbucket != pend_bucket))
{
local_iterator inext = pbucket->erase_after(iprevious); // Unlink from the bucket.
@ -920,8 +929,8 @@ namespace etl
icurrent = inext;
// Are we there yet?
if (icurrent != iend)
// Have we not reached the end?
if ((icurrent != iend) || (pbucket != pend_bucket))
{
// At the end of this bucket?
if ((icurrent == pbucket->end()))
@ -933,7 +942,7 @@ namespace etl
} while (pbucket->empty());
iprevious = pbucket->before_begin();
icurrent = pbucket->begin();
icurrent = pbucket->begin();
}
}
}

View File

@ -912,22 +912,31 @@ namespace etl
//*********************************************************************
iterator erase(const_iterator first_, const_iterator last_)
{
// Erasing everything?
if ((first_ == begin()) && (last_ == end()))
{
clear();
return end();
}
// Make a note of the last.
iterator result((pbuckets + number_of_buckets), last_.get_bucket_list_iterator(), last_.get_local_iterator());
// Get the starting point.
bucket_t* pbucket = first_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
bucket_t* pbucket = first_.get_bucket_list_iterator();
bucket_t* pend_bucket = last_.get_bucket_list_iterator();
local_iterator iprevious = pbucket->before_begin();
local_iterator icurrent = first_.get_local_iterator();
local_iterator iend = last_.get_local_iterator(); // Note: May not be in the same bucket as icurrent.
// Find the node previous to the first one.
// Find the node previous to the first one.
while (iprevious->etl_next != &*icurrent)
{
++iprevious;
}
while (icurrent != iend)
// Until we reach the end.
while ((icurrent != iend) || (pbucket != pend_bucket))
{
local_iterator inext = pbucket->erase_after(iprevious); // Unlink from the bucket.
@ -938,8 +947,8 @@ namespace etl
icurrent = inext;
// Are we there yet?
if (icurrent != iend)
// Have we not reached the end?
if ((icurrent != iend) || (pbucket != pend_bucket))
{
// At the end of this bucket?
if ((icurrent == pbucket->end()))
@ -951,7 +960,7 @@ namespace etl
} while (pbucket->empty());
iprevious = pbucket->before_begin();
icurrent = pbucket->begin();
icurrent = pbucket->begin();
}
}
}

View File

@ -39,7 +39,7 @@ SOFTWARE.
#define ETL_VERSION_MAJOR 18
#define ETL_VERSION_MINOR 12
#define ETL_VERSION_PATCH 4
#define ETL_VERSION_PATCH 5
#define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_W ETL_STRINGIFY(ETL_VERSION_MAJOR) L"." ETL_STRINGIFY(ETL_VERSION_MINOR) L"." ETL_STRINGIFY(ETL_VERSION_PATCH)
#define ETL_VERSION_U16 ETL_STRINGIFY(ETL_VERSION_MAJOR) u"." ETL_STRINGIFY(ETL_VERSION_MINOR) u"." ETL_STRINGIFY(ETL_VERSION_PATCH)

View File

@ -1,6 +1,6 @@
{
"name": "Embedded Template Library",
"version": "18.12.4",
"version": "18.12.5",
"authors": {
"name": "John Wellbelove",
"email": "john.wellbelove@etlcpp.com"

View File

@ -1,5 +1,5 @@
name=Embedded Template Library
version=18.12.4
version=18.12.5
author= John Wellbelove <john.wellbelove@etlcpp.com>
maintainer=John Wellbelove <john.wellbelove@etlcpp.com>
license=MIT

View File

@ -1,3 +1,9 @@
===============================================================================
18.12.5
Fixed issue for incorrect operation of erase(const_iterator, const_iterator) when
the terminating iterator was end() for etl::unordered_map, etl::unordered_multimap,
etl::unordered_set and etl::unordered_multiset.
===============================================================================
18.12.4
Resolve clang 9 compatibility issues.
@ -84,7 +90,7 @@ Allows set_callback() function to be given run-time and compile-time pointers to
===============================================================================
18.3.4
Changed std::move to etl::move in std::optional and std::queue
Changed std::move to etl::move in etl::optional and etl::queue
Fixed etl::span subspan with etl::dynamic_extent
===============================================================================

View File

@ -628,6 +628,51 @@ namespace
CHECK(idata != data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_first_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator end = data.begin();
etl::advance(end, data.size() / 2);
auto itr = data.erase(data.begin(), end);
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == end);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_last_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator begin = data.begin();
etl::advance(begin, data.size() / 2);
auto itr = data.erase(begin, data.end());
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_all)
{
DataNDC data(initial_data.begin(), initial_data.end());
auto itr = data.erase(data.begin(), data.end());
CHECK_EQUAL(0U, data.size());
CHECK(!data.full());
CHECK(data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_clear)
{

View File

@ -541,6 +541,52 @@ namespace
CHECK(idata != data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_first_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator end = data.begin();
etl::advance(end, data.size() / 2);
auto itr = data.erase(data.begin(), end);
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == end);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_last_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator begin = data.begin();
etl::advance(begin, data.size() / 2);
auto itr = data.erase(begin, data.end());
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_all)
{
DataNDC data(initial_data.begin(), initial_data.end());
auto itr = data.erase(data.begin(), data.end());
CHECK_EQUAL(0U, data.size());
CHECK(!data.full());
CHECK(data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_clear)
{

View File

@ -479,6 +479,52 @@ namespace
CHECK(idata != data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_first_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator end = data.begin();
etl::advance(end, data.size() / 2);
auto itr = data.erase(data.begin(), end);
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == end);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_last_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator begin = data.begin();
etl::advance(begin, data.size() / 2);
auto itr = data.erase(begin, data.end());
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_all)
{
DataNDC data(initial_data.begin(), initial_data.end());
auto itr = data.erase(data.begin(), data.end());
CHECK_EQUAL(0U, data.size());
CHECK(!data.full());
CHECK(data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_clear)
{

View File

@ -459,6 +459,52 @@ namespace
CHECK(idata != data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_first_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator end = data.begin();
etl::advance(end, data.size() / 2);
auto itr = data.erase(data.begin(), end);
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == end);
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_last_half)
{
DataNDC data(initial_data.begin(), initial_data.end());
DataNDC::iterator begin = data.begin();
etl::advance(begin, data.size() / 2);
auto itr = data.erase(begin, data.end());
CHECK_EQUAL(initial_data.size() / 2, data.size());
CHECK(!data.full());
CHECK(!data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_erase_range_all)
{
DataNDC data(initial_data.begin(), initial_data.end());
auto itr = data.erase(data.begin(), data.end());
CHECK_EQUAL(0U, data.size());
CHECK(!data.full());
CHECK(data.empty());
CHECK(itr == data.end());
}
//*************************************************************************
TEST_FIXTURE(SetupFixture, test_clear)
{

View File

@ -1687,9 +1687,11 @@
<None Include="..\..\library.json" />
<None Include="..\..\library.properties" />
<None Include="..\..\README.md" />
<None Include="..\cmake\unit-test_external_project.cmake" />
<None Include="cpp.hint" />
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
<Text Include="..\..\include\etl\file_error_numbers.txt" />
<Text Include="..\..\support\Release notes.txt" />
<Text Include="..\..\todo.txt" />

View File

@ -1444,6 +1444,9 @@
<None Include="..\..\.github\workflows\main.yml">
<Filter>Resource Files\CI\Github</Filter>
</None>
<None Include="..\cmake\unit-test_external_project.cmake">
<Filter>Resource Files\Make</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\include\etl\file_error_numbers.txt">
@ -1458,6 +1461,9 @@
<Text Include="..\..\todo.txt">
<Filter>Resource Files</Filter>
</Text>
<Text Include="..\..\CMakeLists.txt">
<Filter>Resource Files\Make</Filter>
</Text>
</ItemGroup>
<ItemGroup>
<Image Include="..\..\etl.ico">