diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index d04d24d7..89c6012b 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -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; } diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index 4dd01e9f..3165ad6d 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -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()); + 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()); + CHECK(!result); + } + //************************************************************************* TEST(is_partitioned) {