Update contrib.
1 // (C) Copyright Jeremy Siek 2001.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_PERMUTATION_HPP
7 #define BOOST_PERMUTATION_HPP
13 #include <boost/graph/detail/shadow_iterator.hpp>
17 template <class Iter1, class Iter2>
18 void permute_serial(Iter1 permuter, Iter1 last, Iter2 result)
20 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
21 typedef std::ptrdiff_t D:
23 typedef typename std::iterator_traits<Iter1>::difference_type D;
27 while (permuter != last) {
28 std::swap(result[n], result[*permuter]);
34 template <class InIter, class RandIterP, class RandIterR>
35 void permute_copy(InIter first, InIter last, RandIterP p, RandIterR result)
37 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
38 typedef std::ptrdiff_t i = 0;
40 typename std::iterator_traits<RandIterP>::difference_type i = 0;
42 for (; first != last; ++first, ++i)
43 result[p[i]] = *first;
48 template <class RandIter, class RandIterPerm, class D, class T>
49 void permute_helper(RandIter first, RandIter last, RandIterPerm p, D, T)
51 D i = 0, pi, n = last - first, cycle_start;
53 std::vector<int> visited(n, false);
55 while (i != n) { // continue until all elements have been processed
58 do { // walk around a cycle
61 std::swap(tmp, first[pi]);
63 } while (i != cycle_start);
65 // find the next cycle
66 for (i = 0; i < n; ++i)
67 if (visited[i] == false)
74 template <class RandIter, class RandIterPerm>
75 void permute(RandIter first, RandIter last, RandIterPerm p)
77 detail::permute_helper(first, last, p, last - first, *first);
81 // Knuth 1.3.3, Vol. 1 p 176
82 // modified for zero-based arrays
85 // WARNING: T must be a signed integer!
86 template <class PermIter>
87 void invert_permutation(PermIter X, PermIter Xend)
89 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
90 typedef std::ptrdiff_t T:
92 typedef typename std::iterator_traits<PermIter>::value_type T;
114 // Takes a "normal" permutation array (and its inverse), and turns it
115 // into a BLAS-style permutation array (which can be thought of as a
116 // serialized permutation).
117 template <class Iter1, class Iter2, class Iter3>
118 inline void serialize_permutation(Iter1 q, Iter1 q_end, Iter2 q_inv, Iter3 p)
120 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
121 typedef std::ptrdiff_t P1;
122 typedef std::ptrdiff_t P2;
123 typedef std::ptrdiff_t D;
125 typedef typename std::iterator_traits<Iter1>::value_type P1;
126 typedef typename std::iterator_traits<Iter2>::value_type P2;
127 typedef typename std::iterator_traits<Iter1>::difference_type D;
130 for (D i = 0; i < n; ++i) {
134 std::swap(q[i], q[qii]);
135 std::swap(q_inv[i], q_inv[qi]);
139 // Not used anymore, leaving it here for future reference.
140 template <typename Iter, typename Compare>
141 void merge_sort(Iter first, Iter last, Compare cmp)
143 if (first + 1 < last) {
144 Iter mid = first + (last - first)/2;
145 merge_sort(first, mid, cmp);
146 merge_sort(mid, last, cmp);
147 std::inplace_merge(first, mid, last, cmp);
152 // time: N log N + 3N + ?
154 template <class Iter, class IterP, class Cmp, class Alloc>
155 inline void sortp(Iter first, Iter last, IterP p, Cmp cmp, Alloc alloc)
157 typedef typename std::iterator_traits<IterP>::value_type P;
158 typedef typename std::iterator_traits<IterP>::difference_type D;
160 std::vector<P, Alloc> q(n);
161 for (D i = 0; i < n; ++i)
163 std::sort(make_shadow_iter(first, q.begin()),
164 make_shadow_iter(last, q.end()),
165 shadow_cmp<Cmp>(cmp));
166 invert_permutation(q.begin(), q.end());
167 std::copy(q.begin(), q.end(), p);
170 template <class Iter, class IterP, class Cmp>
171 inline void sortp(Iter first, Iter last, IterP p, Cmp cmp)
173 typedef typename std::iterator_traits<IterP>::value_type P;
174 sortp(first, last, p, cmp, std::allocator<P>());
177 template <class Iter, class IterP>
178 inline void sortp(Iter first, Iter last, IterP p)
180 typedef typename std::iterator_traits<Iter>::value_type T;
181 typedef typename std::iterator_traits<IterP>::value_type P;
182 sortp(first, last, p, std::less<T>(), std::allocator<P>());
185 template <class Iter, class IterP, class Cmp, class Alloc>
186 inline void sortv(Iter first, Iter last, IterP p, Cmp cmp, Alloc alloc)
188 typedef typename std::iterator_traits<IterP>::value_type P;
189 typedef typename std::iterator_traits<IterP>::difference_type D;
191 std::vector<P, Alloc> q(n), q_inv(n);
192 for (D i = 0; i < n; ++i)
194 std::sort(make_shadow_iter(first, q_inv.begin()),
195 make_shadow_iter(last, q_inv.end()),
196 shadow_cmp<Cmp>(cmp));
197 std::copy(q_inv, q_inv.end(), q.begin());
198 invert_permutation(q.begin(), q.end());
199 serialize_permutation(q.begin(), q.end(), q_inv.end(), p);
205 #endif // BOOST_PERMUTATION_HPP