#include <algorithm>
#include <array>
#include <concepts>
#include <list>
#include <ranges>
#include "almost_satisfies_types.h"
#include "counting_predicates.h"
#include "counting_projection.h"
#include "test_iterators.h"
template <class Iter1, class Sent1 = int*, class Iter2 = int*, class Sent2 = int*>
concept HasIsPermutationIt = requires(Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2) {
std::ranges::is_permutation(first1, last1, first2, last2);
};
template <class Range1, class Range2 = UncheckedRange<int*>>
concept HasIsPermutationR = requires(Range1 range1, Range2 range2) {
std::ranges::is_permutation(range1, range2);
};
static_assert(HasIsPermutationIt<int*>);
static_assert(!HasIsPermutationIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasIsPermutationIt<ForwardIteratorNotIncrementable>);
static_assert(!HasIsPermutationIt<int*, SentinelForNotSemiregular>);
static_assert(!HasIsPermutationIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasIsPermutationIt<int*, int*, ForwardIteratorNotDerivedFrom>);
static_assert(!HasIsPermutationIt<int*, int*, ForwardIteratorNotIncrementable>);
static_assert(!HasIsPermutationIt<int*, int*, int*, SentinelForNotSemiregular>);
static_assert(!HasIsPermutationIt<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasIsPermutationIt<int*, int*, int**, int**>);
static_assert(HasIsPermutationR<UncheckedRange<int*>>);
static_assert(!HasIsPermutationR<ForwardRangeNotDerivedFrom>);
static_assert(!HasIsPermutationR<ForwardRangeNotIncrementable>);
static_assert(!HasIsPermutationR<int*, ForwardRangeNotSentinelSemiregular>);
static_assert(!HasIsPermutationR<int*, ForwardRangeNotSentinelEqualityComparableWith>);
static_assert(!HasIsPermutationR<UncheckedRange<int*>, ForwardRangeNotDerivedFrom>);
static_assert(!HasIsPermutationR<UncheckedRange<int*>, ForwardRangeNotIncrementable>);
static_assert(!HasIsPermutationR<UncheckedRange<int*>, ForwardRangeNotSentinelSemiregular>);
static_assert(!HasIsPermutationR<UncheckedRange<int*>, ForwardRangeNotSentinelEqualityComparableWith>);
static_assert(!HasIsPermutationIt<UncheckedRange<int*>, UncheckedRange<int**>>);
template <int N, int M>
struct Data {
std::array<int, N> input1;
std::array<int, M> input2;
bool expected;
};
template <class Iter1, class Sent1, class Iter2, class Sent2, int N, int M>
constexpr void test(Data<N, M> d) {
{
std::same_as<bool> decltype(auto) ret = std::ranges::is_permutation(Iter1(d.input1.data()),
Sent1(Iter1(d.input1.data() + N)),
Iter1(d.input2.data()),
Sent1(Iter1(d.input2.data() + M)));
assert(ret == d.expected);
}
{
auto range1 = std::ranges::subrange(Iter1(d.input1.data()), Sent1(Iter1(d.input1.data() + N)));
auto range2 = std::ranges::subrange(Iter1(d.input2.data()), Sent1(Iter1(d.input2.data() + M)));
std::same_as<bool> decltype(auto) ret = std::ranges::is_permutation(range1, range2);
assert(ret == d.expected);
}
}
template <class Iter1, class Sent1, class Iter2, class Sent2 = Iter2>
constexpr void test_iterators() {
test<Iter1, Sent1, Iter2, Sent2, 4, 4>({.input1 = {1, 2, 3, 4}, .input2 = {1, 2, 3, 4}, .expected = true});
test<Iter1, Sent1, Iter2, Sent2, 4, 4>({.input1 = {1, 2, 3, 4}, .input2 = {4, 3, 2, 1}, .expected = true});
test<Iter1, Sent1, Iter2, Sent2, 4, 4>({.input1 = {4, 2, 3, 1}, .input2 = {1, 2, 3, 4}, .expected = true});
test<Iter1, Sent1, Iter2, Sent2, 4, 5>({.input1 = {4, 2, 3, 1}, .input2 = {4, 3, 2, 1, 5}, .expected = false});
test<Iter1, Sent1, Iter2, Sent2, 5, 4>({.input1 = {4, 2, 3, 1, 5}, .input2 = {4, 3, 2, 1}, .expected = false});
test<Iter1, Sent1, Iter2, Sent2, 0, 4>({.input1 = {}, .input2 = {4, 3, 2, 1}, .expected = false});
test<Iter1, Sent1, Iter2, Sent2, 5, 0>({.input1 = {4, 2, 3, 1, 5}, .input2 = {}, .expected = false});
test<Iter1, Sent1, Iter2, Sent2, 0, 0>({.input1 = {}, .input2 = {}, .expected = true});
test<Iter1, Sent1, Iter2, Sent2, 1, 1>({.input1 = {1}, .input2 = {1}, .expected = true});
test<Iter1, Sent1, Iter2, Sent2, 1, 1>({.input1 = {1}, .input2 = {2}, .expected = false});
}
template <class Iter1, class Sent1 = Iter1>
constexpr void test_iterators1() {
test_iterators<Iter1, Sent1, forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
test_iterators<Iter1, Sent1, forward_iterator<int*>>();
test_iterators<Iter1, Sent1, bidirectional_iterator<int*>>();
test_iterators<Iter1, Sent1, random_access_iterator<int*>>();
test_iterators<Iter1, Sent1, contiguous_iterator<int*>>();
test_iterators<Iter1, Sent1, int*>();
test_iterators<Iter1, Sent1, const int*>();
}
constexpr bool test() {
test_iterators1<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
test_iterators1<forward_iterator<int*>>();
test_iterators1<bidirectional_iterator<int*>>();
test_iterators1<random_access_iterator<int*>>();
test_iterators1<contiguous_iterator<int*>>();
test_iterators1<int*>();
test_iterators1<const int*>();
{
struct A {
int a;
constexpr bool pred(const A& rhs) const { return a == rhs.a; }
};
std::array in1 = {A{2}, A{3}, A{1}};
std::array in2 = {A{1}, A{2}, A{3}};
{
auto ret = std::ranges::is_permutation(in1.begin(), in1.end(), in2.begin(), in2.end(), &A::pred);
assert(ret);
}
{
auto ret = std::ranges::is_permutation(in1, in2, &A::pred);
assert(ret);
}
}
{
struct A {
int a;
constexpr bool operator==(const A&) const = default;
constexpr A x2() const { return A{a * 2}; }
constexpr A div2() const { return A{a / 2}; }
};
std::array in1 = {A{1}, A{2}, A{3}};
std::array in2 = {A{4}, A{8}, A{12}};
{
auto ret = std::ranges::is_permutation(
in1.begin(), in1.end(), in2.begin(), in2.end(), {}, &A::x2, &A::div2);
assert(ret);
}
{
auto ret = std::ranges::is_permutation(in1, in2, {}, &A::x2, &A::div2);
assert(ret);
}
}
{
int predCount = 0;
int proj1Count = 0;
int proj2Count = 0;
auto reset_counters = [&] {
predCount = proj1Count = proj2Count = 0;
};
counting_predicate pred(std::ranges::equal_to{}, predCount);
counting_projection<> proj1(proj1Count);
counting_projection<> proj2(proj2Count);
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 4};
auto b_begin = random_access_iterator<int*>(std::begin(b));
auto b_end = random_access_iterator<int*>(std::end(b));
{
auto ret = std::ranges::is_permutation(a, a + 5, b_begin, b_end, pred, proj1, proj2);
assert(!ret);
assert(predCount == 0);
assert(proj1Count == 0);
assert(proj2Count == 0);
reset_counters();
}
{
auto ret = std::ranges::is_permutation(a, std::ranges::subrange(b_begin, b_end), pred, proj1, proj2);
assert(!ret);
assert(predCount == 0);
assert(proj1Count == 0);
assert(proj2Count == 0);
reset_counters();
}
}
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 4, 5};
int expected = 5;
{
auto ret = std::ranges::is_permutation(a, a + 5, b, b + 5, pred, proj1, proj2);
assert(ret);
assert(predCount == expected);
assert(proj1Count == expected);
assert(proj2Count == expected);
reset_counters();
}
{
auto ret = std::ranges::is_permutation(a, b, pred, proj1, proj2);
assert(ret);
assert(predCount == expected);
assert(proj1Count == expected);
assert(proj2Count == expected);
reset_counters();
}
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}