tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
is_permutation.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_IS_PERMUTATION_HPP
4#define TETL_ALGORITHM_IS_PERMUTATION_HPP
5
14
15namespace etl {
16
22template <typename ForwardIt1, typename ForwardIt2>
23[[nodiscard]] constexpr auto is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 first2) -> bool
24{
25 // skip common prefix
26 auto const [fDiff1, fDiff2] = etl::mismatch(first, last, first2);
27
28 // iterate over the rest, counting how many times each element
29 // from `[first, last)` appears in [first2, last2)
30 if (fDiff1 != last) {
31 auto last2 = etl::next(fDiff2, etl::distance(fDiff1, last));
32 for (auto i = fDiff1; i != last; ++i) {
33 // this *i has been checked
34 if (i != etl::find(fDiff1, i, *i)) {
35 continue;
36 }
37
38 auto m = etl::count(fDiff2, last2, *i);
39 if (m == 0 or etl::count(i, last, *i) != m) {
40 return false;
41 }
42 }
43 }
44
45 return true;
46}
47
49template <typename ForwardIt1, typename ForwardIt2>
50[[nodiscard]] constexpr auto is_permutation(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2)
51 -> bool
52{
54
57
58 if constexpr (lhsIsRandomIt and rhsIsRandomIt) {
59 if (etl::distance(first1, last1) != etl::distance(first2, last2)) {
60 return false;
61 }
62 }
63 return etl::is_permutation(first1, last1, first2);
64}
65
66} // namespace etl
67
68#endif // TETL_ALGORITHM_IS_PERMUTATION_HPP
constexpr auto mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate pred) -> pair< InputIt1, InputIt2 >
Returns the first mismatching pair of elements from two ranges: one defined by [first1,...
Definition mismatch.hpp:25
constexpr auto is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 first2) -> bool
Returns true if there exists a permutation of the elements in the range [first1, last1) that makes th...
Definition is_permutation.hpp:23
constexpr auto count(InputIt first, InputIt last, T const &value) -> typename iterator_traits< InputIt >::difference_type
Returns the number of elements in the range [first, last) satisfying specific criteria....
Definition count.hpp:21
constexpr auto find(InputIt first, InputIt last, T const &value) noexcept -> InputIt
Searches for an element equal to value.
Definition find.hpp:18
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:16
Definition adjacent_find.hpp:8
constexpr bool is_base_of_v
Definition is_base_of.hpp:39
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:36