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