Use predicate in calculation of is_permutation consistently

In case of predicate not equal_to, the calculation previously
returned wron results
This commit is contained in:
Roland Reichwein 2026-03-07 19:18:16 +01:00
parent 95018edcf5
commit faa94f3ba2
2 changed files with 37 additions and 4 deletions

View File

@ -1928,9 +1928,9 @@ namespace etl
{
if (i == etl::find_if(begin1, i, etl::bind1st(predicate, *i)))
{
size_t n = etl::count(begin2, end2, *i);
size_t n = etl::count_if(begin2, end2, etl::bind1st(predicate, *i));
if (n == 0 || size_t(etl::count(i, end1, *i)) != n)
if (n == 0 || size_t(etl::count_if(i, end1, etl::bind1st(predicate, *i))) != n)
{
return false;
}
@ -1954,6 +1954,11 @@ namespace etl
TIterator2 begin2,
TIterator2 end2)
{
if (etl::distance(begin1, end1) != etl::distance(begin2, end2))
{
return false;
}
if (begin1 != end1)
{
for (TIterator1 i = begin1; i != end1; ++i)
@ -1987,15 +1992,20 @@ namespace etl
TIterator2 end2,
TBinaryPredicate predicate)
{
if (etl::distance(begin1, end1) != etl::distance(begin2, end2))
{
return false;
}
if (begin1 != end1)
{
for (TIterator1 i = begin1; i != end1; ++i)
{
if (i == etl::find_if(begin1, i, etl::bind1st(predicate, *i)))
{
size_t n = etl::count(begin2, end2, *i);
size_t n = etl::count_if(begin2, end2, etl::bind1st(predicate, *i));
if (n == 0 || size_t(etl::count(i, end1, *i)) != n)
if (n == 0 || size_t(etl::count_if(i, end1, etl::bind1st(predicate, *i))) != n)
{
return false;
}

View File

@ -1405,6 +1405,29 @@ namespace
CHECK(!is_permutation);
}
//*************************************************************************
TEST(is_permutation_different_lengths)
{
int shorter[] = { 1, 2 };
int longer[] = { 1, 2, 3 };
// Four-iterator: range2 longer than range1 (extra elements only in range2)
bool result = etl::is_permutation(std::begin(shorter), std::end(shorter), std::begin(longer), std::end(longer));
CHECK(!result);
// Four-iterator: range1 longer than range2
result = etl::is_permutation(std::begin(longer), std::end(longer), std::begin(shorter), std::end(shorter));
CHECK(!result);
// Four-iterator with predicate: range2 longer than range1
result = etl::is_permutation(std::begin(shorter), std::end(shorter), std::begin(longer), std::end(longer), etl::equal_to<int>());
CHECK(!result);
// Four-iterator with predicate: range1 longer than range2
result = etl::is_permutation(std::begin(longer), std::end(longer), std::begin(shorter), std::end(shorter), etl::equal_to<int>());
CHECK(!result);
}
//*************************************************************************
TEST(is_partitioned)
{