Different fixes

This commit is contained in:
Alexander Zaitsev
2017-08-30 20:25:15 +03:00
parent 32016b3c79
commit 7df35ffd56
4 changed files with 50 additions and 35 deletions

View File

@ -19,8 +19,7 @@
#define BOOST_ALGORITHM_APPLY_PERMUTATION_HPP
#include <algorithm>
#include <utility>
#include <iterator>
#include <type_traits>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
@ -40,17 +39,18 @@ namespace boost { namespace algorithm
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void
apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin)
RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end)
{
typedef typename std::iterator_traits<RandomAccessIterator1>::difference_type Diff;
using Diff = typename std::iterator_traits<RandomAccessIterator1>::difference_type;
using std::swap;
Diff size = std::distance(item_begin, item_end);
for (Diff i = 0; i < size; ++i)
for (Diff i = 0; i < size; i++)
{
Diff current = i;
auto current = i;
while (i != ind_begin[current])
{
Diff next = static_cast<Diff>(ind_begin[current]);
std::swap(item_begin[current], item_begin[next]);
auto next = ind_begin[current];
swap(item_begin[current], item_begin[next]);
ind_begin[current] = current;
current = next;
}
@ -69,18 +69,22 @@ apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_e
/// Complexity: O(N).
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
void
apply_reverse_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin)
apply_reverse_permutation(
RandomAccessIterator1 item_begin,
RandomAccessIterator1 item_end,
RandomAccessIterator2 ind_begin,
RandomAccessIterator2 ind_end)
{
typedef typename std::iterator_traits<RandomAccessIterator1>::difference_type Diff;
using Diff = typename std::iterator_traits<RandomAccessIterator2>::difference_type;
using std::swap;
Diff length = std::distance(item_begin, item_end);
for (Diff i = 0; i < length; ++i)
for (Diff i = 0; i < length; i++)
{
while (i != ind_begin[i])
{
Diff next = ind_begin[i];
std::swap(item_begin[i], item_begin[next]);
std::swap(ind_begin[i], ind_begin[next]);
swap(item_begin[i], item_begin[next]);
swap(ind_begin[i], ind_begin[next]);
}
}
}
@ -98,7 +102,7 @@ void
apply_permutation(Range1& item_range, Range2& ind_range)
{
apply_permutation(boost::begin(item_range), boost::end(item_range),
boost::begin(ind_range));
boost::begin(ind_range), boost::end(ind_range));
}
/// \fn apply_reverse_permutation ( Range1 item_range, Range2 ind_range )
@ -114,7 +118,7 @@ void
apply_reverse_permutation(Range1& item_range, Range2& ind_range)
{
apply_reverse_permutation(boost::begin(item_range), boost::end(item_range),
boost::begin(ind_range));
boost::begin(ind_range), boost::end(ind_range));
}
}}