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