File indexing completed on 2025-01-18 09:28:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #ifndef BOOST_ALGORITHM_APPLY_PERMUTATION_HPP
0019 #define BOOST_ALGORITHM_APPLY_PERMUTATION_HPP
0020
0021 #include <algorithm>
0022
0023 #include <boost/config.hpp>
0024 #include <boost/range/begin.hpp>
0025 #include <boost/range/end.hpp>
0026
0027 namespace boost { namespace algorithm
0028 {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 template<typename RandomAccessIterator1, typename RandomAccessIterator2>
0040 void
0041 apply_permutation(RandomAccessIterator1 item_begin, RandomAccessIterator1 item_end,
0042 RandomAccessIterator2 ind_begin, RandomAccessIterator2 ind_end)
0043 {
0044 typedef typename std::iterator_traits<RandomAccessIterator1>::difference_type Diff;
0045 typedef typename std::iterator_traits<RandomAccessIterator2>::difference_type Index;
0046 using std::swap;
0047 Diff size = std::distance(item_begin, item_end);
0048 for (Diff i = 0; i < size; i++)
0049 {
0050 Diff current = i;
0051 while (i != ind_begin[current])
0052 {
0053 Index next = ind_begin[current];
0054 swap(item_begin[current], item_begin[next]);
0055 ind_begin[current] = current;
0056 current = next;
0057 }
0058 ind_begin[current] = current;
0059 }
0060 }
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 template<typename RandomAccessIterator1, typename RandomAccessIterator2>
0072 void
0073 apply_reverse_permutation(
0074 RandomAccessIterator1 item_begin,
0075 RandomAccessIterator1 item_end,
0076 RandomAccessIterator2 ind_begin,
0077 RandomAccessIterator2 ind_end)
0078 {
0079 typedef typename std::iterator_traits<RandomAccessIterator2>::difference_type Diff;
0080 using std::swap;
0081 Diff length = std::distance(item_begin, item_end);
0082 for (Diff i = 0; i < length; i++)
0083 {
0084 while (i != ind_begin[i])
0085 {
0086 Diff next = ind_begin[i];
0087 swap(item_begin[i], item_begin[next]);
0088 swap(ind_begin[i], ind_begin[next]);
0089 }
0090 }
0091 }
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 template<typename Range1, typename Range2>
0102 void
0103 apply_permutation(Range1& item_range, Range2& ind_range)
0104 {
0105 apply_permutation(boost::begin(item_range), boost::end(item_range),
0106 boost::begin(ind_range), boost::end(ind_range));
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 template<typename Range1, typename Range2>
0118 void
0119 apply_reverse_permutation(Range1& item_range, Range2& ind_range)
0120 {
0121 apply_reverse_permutation(boost::begin(item_range), boost::end(item_range),
0122 boost::begin(ind_range), boost::end(ind_range));
0123 }
0124
0125 }}
0126 #endif