Chris@101
|
1 /*
|
Chris@101
|
2 Copyright (c) Marshall Clow 2014.
|
Chris@16
|
3
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7
|
Chris@101
|
8 /// \file is_permutation.hpp
|
Chris@101
|
9 /// \brief Is a sequence a permutation of another sequence (four iterator versions)
|
Chris@16
|
10 /// \author Marshall Clow
|
Chris@16
|
11
|
Chris@101
|
12 #ifndef BOOST_ALGORITHM_IS_PERMUTATION14_HPP
|
Chris@101
|
13 #define BOOST_ALGORITHM_IS_PERMUTATION14_HPP
|
Chris@16
|
14
|
Chris@101
|
15 #include <algorithm> // for std::less, tie, mismatch and is_permutation (if available)
|
Chris@101
|
16 #include <utility> // for std::make_pair
|
Chris@101
|
17 #include <functional> // for std::equal_to
|
Chris@101
|
18 #include <iterator>
|
Chris@101
|
19
|
Chris@101
|
20 #include <boost/algorithm/cxx11/is_permutation.hpp>
|
Chris@101
|
21 #include <boost/algorithm/cxx14/mismatch.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost { namespace algorithm {
|
Chris@16
|
24
|
Chris@101
|
25 /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
|
Chris@101
|
26 /// ForwardIterator2 first2, ForwardIterator2 last2 )
|
Chris@101
|
27 /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
|
Chris@101
|
28 ///
|
Chris@101
|
29 /// \param first1 The start of the input sequence
|
Chris@101
|
30 /// \param last2 One past the end of the input sequence
|
Chris@101
|
31 /// \param first2 The start of the second sequence
|
Chris@101
|
32 /// \param last1 One past the end of the second sequence
|
Chris@101
|
33 /// \note This function is part of the C++2014 standard library.
|
Chris@101
|
34 /// We will use the standard one if it is available,
|
Chris@101
|
35 /// otherwise we have our own implementation.
|
Chris@101
|
36 template< class ForwardIterator1, class ForwardIterator2 >
|
Chris@101
|
37 bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
|
Chris@101
|
38 ForwardIterator2 first2, ForwardIterator2 last2 )
|
Chris@101
|
39 {
|
Chris@101
|
40 // How should I deal with the idea that ForwardIterator1::value_type
|
Chris@101
|
41 // and ForwardIterator2::value_type could be different? Define my own comparison predicate?
|
Chris@101
|
42 std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
|
Chris@101
|
43 ( first1, last1, first2, last2 );
|
Chris@101
|
44 if ( eq.first == last1 && eq.second == last2)
|
Chris@101
|
45 return true;
|
Chris@101
|
46 return boost::algorithm::detail::is_permutation_tag (
|
Chris@101
|
47 eq.first, last1, eq.second, last2,
|
Chris@101
|
48 std::equal_to<typename std::iterator_traits<ForwardIterator1>::value_type> (),
|
Chris@101
|
49 typename std::iterator_traits<ForwardIterator1>::iterator_category (),
|
Chris@101
|
50 typename std::iterator_traits<ForwardIterator2>::iterator_category ());
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@101
|
53 /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
|
Chris@101
|
54 /// ForwardIterator2 first2, ForwardIterator2 last2,
|
Chris@101
|
55 /// BinaryPredicate p )
|
Chris@101
|
56 /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
|
Chris@101
|
57 ///
|
Chris@101
|
58 /// \param first1 The start of the input sequence
|
Chris@101
|
59 /// \param last1 One past the end of the input sequence
|
Chris@101
|
60 /// \param first2 The start of the second sequence
|
Chris@101
|
61 /// \param last2 One past the end of the second sequence
|
Chris@101
|
62 /// \param pred The predicate to compare elements with
|
Chris@101
|
63 ///
|
Chris@101
|
64 /// \note This function is part of the C++2014 standard library.
|
Chris@101
|
65 /// We will use the standard one if it is available,
|
Chris@101
|
66 /// otherwise we have our own implementation.
|
Chris@101
|
67 template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
|
Chris@101
|
68 bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
|
Chris@101
|
69 ForwardIterator2 first2, ForwardIterator2 last2,
|
Chris@101
|
70 BinaryPredicate pred )
|
Chris@16
|
71 {
|
Chris@101
|
72 std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
|
Chris@101
|
73 ( first1, last1, first2, last2, pred );
|
Chris@101
|
74 if ( eq.first == last1 && eq.second == last2)
|
Chris@101
|
75 return true;
|
Chris@101
|
76 return boost::algorithm::detail::is_permutation_tag (
|
Chris@101
|
77 first1, last1, first2, last2, pred,
|
Chris@101
|
78 typename std::iterator_traits<ForwardIterator1>::iterator_category (),
|
Chris@101
|
79 typename std::iterator_traits<ForwardIterator2>::iterator_category ());
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@101
|
82 }}
|
Chris@16
|
83
|
Chris@101
|
84 #endif // BOOST_ALGORITHM_IS_PERMUTATION14_HPP
|