Chris@16
|
1 // (C) Copyright Jeremy Siek 2001.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_PERMUTATION_HPP
|
Chris@16
|
7 #define BOOST_PERMUTATION_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #include <vector>
|
Chris@16
|
10 #include <memory>
|
Chris@16
|
11 #include <functional>
|
Chris@16
|
12 #include <algorithm>
|
Chris@16
|
13 #include <boost/graph/detail/shadow_iterator.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 namespace boost {
|
Chris@16
|
16
|
Chris@16
|
17 template <class Iter1, class Iter2>
|
Chris@16
|
18 void permute_serial(Iter1 permuter, Iter1 last, Iter2 result)
|
Chris@16
|
19 {
|
Chris@16
|
20 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
|
Chris@16
|
21 typedef std::ptrdiff_t D:
|
Chris@16
|
22 #else
|
Chris@16
|
23 typedef typename std::iterator_traits<Iter1>::difference_type D;
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 D n = 0;
|
Chris@16
|
27 while (permuter != last) {
|
Chris@16
|
28 std::swap(result[n], result[*permuter]);
|
Chris@16
|
29 ++n;
|
Chris@16
|
30 ++permuter;
|
Chris@16
|
31 }
|
Chris@16
|
32 }
|
Chris@16
|
33
|
Chris@16
|
34 template <class InIter, class RandIterP, class RandIterR>
|
Chris@16
|
35 void permute_copy(InIter first, InIter last, RandIterP p, RandIterR result)
|
Chris@16
|
36 {
|
Chris@16
|
37 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
|
Chris@16
|
38 typedef std::ptrdiff_t i = 0;
|
Chris@16
|
39 #else
|
Chris@16
|
40 typename std::iterator_traits<RandIterP>::difference_type i = 0;
|
Chris@16
|
41 #endif
|
Chris@16
|
42 for (; first != last; ++first, ++i)
|
Chris@16
|
43 result[p[i]] = *first;
|
Chris@16
|
44 }
|
Chris@16
|
45
|
Chris@16
|
46 namespace detail {
|
Chris@16
|
47
|
Chris@16
|
48 template <class RandIter, class RandIterPerm, class D, class T>
|
Chris@16
|
49 void permute_helper(RandIter first, RandIter last, RandIterPerm p, D, T)
|
Chris@16
|
50 {
|
Chris@16
|
51 D i = 0, pi, n = last - first, cycle_start;
|
Chris@16
|
52 T tmp;
|
Chris@16
|
53 std::vector<int> visited(n, false);
|
Chris@16
|
54
|
Chris@16
|
55 while (i != n) { // continue until all elements have been processed
|
Chris@16
|
56 cycle_start = i;
|
Chris@16
|
57 tmp = first[i];
|
Chris@16
|
58 do { // walk around a cycle
|
Chris@16
|
59 pi = p[i];
|
Chris@16
|
60 visited[pi] = true;
|
Chris@16
|
61 std::swap(tmp, first[pi]);
|
Chris@16
|
62 i = pi;
|
Chris@16
|
63 } while (i != cycle_start);
|
Chris@16
|
64
|
Chris@16
|
65 // find the next cycle
|
Chris@16
|
66 for (i = 0; i < n; ++i)
|
Chris@16
|
67 if (visited[i] == false)
|
Chris@16
|
68 break;
|
Chris@16
|
69 }
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 } // namespace detail
|
Chris@16
|
73
|
Chris@16
|
74 template <class RandIter, class RandIterPerm>
|
Chris@16
|
75 void permute(RandIter first, RandIter last, RandIterPerm p)
|
Chris@16
|
76 {
|
Chris@16
|
77 detail::permute_helper(first, last, p, last - first, *first);
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80
|
Chris@16
|
81 // Knuth 1.3.3, Vol. 1 p 176
|
Chris@16
|
82 // modified for zero-based arrays
|
Chris@16
|
83 // time complexity?
|
Chris@16
|
84 //
|
Chris@16
|
85 // WARNING: T must be a signed integer!
|
Chris@16
|
86 template <class PermIter>
|
Chris@16
|
87 void invert_permutation(PermIter X, PermIter Xend)
|
Chris@16
|
88 {
|
Chris@16
|
89 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
|
Chris@16
|
90 typedef std::ptrdiff_t T:
|
Chris@16
|
91 #else
|
Chris@16
|
92 typedef typename std::iterator_traits<PermIter>::value_type T;
|
Chris@16
|
93 #endif
|
Chris@16
|
94 T n = Xend - X;
|
Chris@16
|
95 T m = n;
|
Chris@16
|
96 T j = -1;
|
Chris@16
|
97
|
Chris@16
|
98 while (m > 0) {
|
Chris@16
|
99 T i = X[m-1] + 1;
|
Chris@16
|
100 if (i > 0) {
|
Chris@16
|
101 do {
|
Chris@16
|
102 X[m-1] = j - 1;
|
Chris@16
|
103 j = -m;
|
Chris@16
|
104 m = i;
|
Chris@16
|
105 i = X[m-1] + 1;
|
Chris@16
|
106 } while (i > 0);
|
Chris@16
|
107 i = j;
|
Chris@16
|
108 }
|
Chris@16
|
109 X[m-1] = -i - 1;
|
Chris@16
|
110 --m;
|
Chris@16
|
111 }
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 // Takes a "normal" permutation array (and its inverse), and turns it
|
Chris@16
|
115 // into a BLAS-style permutation array (which can be thought of as a
|
Chris@16
|
116 // serialized permutation).
|
Chris@16
|
117 template <class Iter1, class Iter2, class Iter3>
|
Chris@16
|
118 inline void serialize_permutation(Iter1 q, Iter1 q_end, Iter2 q_inv, Iter3 p)
|
Chris@16
|
119 {
|
Chris@16
|
120 #ifdef BOOST_NO_STD_ITERATOR_TRAITS
|
Chris@16
|
121 typedef std::ptrdiff_t P1;
|
Chris@16
|
122 typedef std::ptrdiff_t P2;
|
Chris@16
|
123 typedef std::ptrdiff_t D;
|
Chris@16
|
124 #else
|
Chris@16
|
125 typedef typename std::iterator_traits<Iter1>::value_type P1;
|
Chris@16
|
126 typedef typename std::iterator_traits<Iter2>::value_type P2;
|
Chris@16
|
127 typedef typename std::iterator_traits<Iter1>::difference_type D;
|
Chris@16
|
128 #endif
|
Chris@16
|
129 D n = q_end - q;
|
Chris@16
|
130 for (D i = 0; i < n; ++i) {
|
Chris@16
|
131 P1 qi = q[i];
|
Chris@16
|
132 P2 qii = q_inv[i];
|
Chris@16
|
133 *p++ = qii;
|
Chris@16
|
134 std::swap(q[i], q[qii]);
|
Chris@16
|
135 std::swap(q_inv[i], q_inv[qi]);
|
Chris@16
|
136 }
|
Chris@16
|
137 }
|
Chris@16
|
138
|
Chris@16
|
139 // Not used anymore, leaving it here for future reference.
|
Chris@16
|
140 template <typename Iter, typename Compare>
|
Chris@16
|
141 void merge_sort(Iter first, Iter last, Compare cmp)
|
Chris@16
|
142 {
|
Chris@16
|
143 if (first + 1 < last) {
|
Chris@16
|
144 Iter mid = first + (last - first)/2;
|
Chris@16
|
145 merge_sort(first, mid, cmp);
|
Chris@16
|
146 merge_sort(mid, last, cmp);
|
Chris@16
|
147 std::inplace_merge(first, mid, last, cmp);
|
Chris@16
|
148 }
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151
|
Chris@16
|
152 // time: N log N + 3N + ?
|
Chris@16
|
153 // space: 2N
|
Chris@16
|
154 template <class Iter, class IterP, class Cmp, class Alloc>
|
Chris@16
|
155 inline void sortp(Iter first, Iter last, IterP p, Cmp cmp, Alloc alloc)
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef typename std::iterator_traits<IterP>::value_type P;
|
Chris@16
|
158 typedef typename std::iterator_traits<IterP>::difference_type D;
|
Chris@16
|
159 D n = last - first;
|
Chris@16
|
160 std::vector<P, Alloc> q(n);
|
Chris@16
|
161 for (D i = 0; i < n; ++i)
|
Chris@16
|
162 q[i] = i;
|
Chris@16
|
163 std::sort(make_shadow_iter(first, q.begin()),
|
Chris@16
|
164 make_shadow_iter(last, q.end()),
|
Chris@16
|
165 shadow_cmp<Cmp>(cmp));
|
Chris@16
|
166 invert_permutation(q.begin(), q.end());
|
Chris@16
|
167 std::copy(q.begin(), q.end(), p);
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 template <class Iter, class IterP, class Cmp>
|
Chris@16
|
171 inline void sortp(Iter first, Iter last, IterP p, Cmp cmp)
|
Chris@16
|
172 {
|
Chris@16
|
173 typedef typename std::iterator_traits<IterP>::value_type P;
|
Chris@16
|
174 sortp(first, last, p, cmp, std::allocator<P>());
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 template <class Iter, class IterP>
|
Chris@16
|
178 inline void sortp(Iter first, Iter last, IterP p)
|
Chris@16
|
179 {
|
Chris@16
|
180 typedef typename std::iterator_traits<Iter>::value_type T;
|
Chris@16
|
181 typedef typename std::iterator_traits<IterP>::value_type P;
|
Chris@16
|
182 sortp(first, last, p, std::less<T>(), std::allocator<P>());
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 template <class Iter, class IterP, class Cmp, class Alloc>
|
Chris@16
|
186 inline void sortv(Iter first, Iter last, IterP p, Cmp cmp, Alloc alloc)
|
Chris@16
|
187 {
|
Chris@16
|
188 typedef typename std::iterator_traits<IterP>::value_type P;
|
Chris@16
|
189 typedef typename std::iterator_traits<IterP>::difference_type D;
|
Chris@16
|
190 D n = last - first;
|
Chris@16
|
191 std::vector<P, Alloc> q(n), q_inv(n);
|
Chris@16
|
192 for (D i = 0; i < n; ++i)
|
Chris@16
|
193 q_inv[i] = i;
|
Chris@16
|
194 std::sort(make_shadow_iter(first, q_inv.begin()),
|
Chris@16
|
195 make_shadow_iter(last, q_inv.end()),
|
Chris@16
|
196 shadow_cmp<Cmp>(cmp));
|
Chris@16
|
197 std::copy(q_inv, q_inv.end(), q.begin());
|
Chris@16
|
198 invert_permutation(q.begin(), q.end());
|
Chris@16
|
199 serialize_permutation(q.begin(), q.end(), q_inv.end(), p);
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202
|
Chris@16
|
203 } // namespace boost
|
Chris@16
|
204
|
Chris@16
|
205 #endif // BOOST_PERMUTATION_HPP
|