14 #ifndef OR_TOOLS_LP_DATA_PERMUTATION_H_
15 #define OR_TOOLS_LP_DATA_PERMUTATION_H_
17 #include "absl/random/random.h"
42 template <
typename IndexType>
49 IndexType
size()
const {
return IndexType(perm_.
size()); }
64 const IndexType
operator[](IndexType i)
const {
return perm_[i]; }
102 template <
typename IndexType,
typename ITIVectorType>
104 const ITIVectorType&
b, ITIVectorType* result);
108 template <
typename IndexType,
typename ITIVectorType>
110 const ITIVectorType&
b, ITIVectorType* result);
114 template <
typename RowIndexedVector>
117 RowIndexedVector temp_v = *v;
125 template <
typename IndexType>
127 const size_t size = inverse.perm_.
size();
129 for (IndexType i(0); i < size; ++i) {
130 perm_[inverse[i]] = i;
134 template <
typename IndexType>
136 const size_t size = perm_.size();
137 perm_.resize(size, IndexType(0));
138 for (IndexType i(0); i < size; ++i) {
143 template <
typename IndexType>
145 PopulateFromIdentity();
146 std::shuffle(perm_.begin(), perm_.end());
149 template <
typename IndexType>
151 const size_t size = perm_.size();
153 for (IndexType i(0); i < size; ++i) {
154 if (perm_[i] < 0 || perm_[i] >= size) {
157 visited[perm_[i]] =
true;
159 for (IndexType i(0); i < size; ++i) {
167 template <
typename IndexType>
169 const size_t size = perm_.size();
173 for (IndexType i(0); i < size; ++i) {
182 if ((cycle_size & 1) == 0) {
183 signature = -signature;
190 template <
typename IndexType,
typename ITIVectorType>
192 const ITIVectorType&
b, ITIVectorType* result) {
194 const IndexType size(perm.
size());
195 if (size == 0)
return;
196 DCHECK_EQ(size.value(),
b.size().value());
197 result->resize(
b.size(),
b.back());
198 for (IndexType i(0); i < size; ++i) {
199 const typename ITIVectorType::IndexType ith_index(i.value());
200 const typename ITIVectorType::IndexType permuted(perm[i].
value());
201 (*result)[permuted] =
b[ith_index];
205 template <
typename IndexType,
typename ITIVectorType>
207 const ITIVectorType&
b, ITIVectorType* result) {
209 const IndexType size(perm.
size().value());
210 if (size == 0)
return;
211 DCHECK_EQ(size.value(),
b.size().value());
212 result->resize(
b.size(),
b.back());
213 for (IndexType i(0); i < size; ++i) {
214 const typename ITIVectorType::IndexType ith_index(i.value());
215 const typename ITIVectorType::IndexType permuted(perm[i].
value());
216 (*result)[ith_index] =
b[permuted];
223 #endif // OR_TOOLS_LP_DATA_PERMUTATION_H_