14#ifndef OR_TOOLS_LP_DATA_PERMUTATION_H_
15#define OR_TOOLS_LP_DATA_PERMUTATION_H_
19#include "absl/log/check.h"
20#include "absl/random/random.h"
46template <
typename IndexType>
57 IndexType
size()
const {
return perm_.size(); }
58 bool empty()
const {
return perm_.empty(); }
63 perm_.resize(
size.value(), value);
70 IndexType
operator[](IndexType i)
const {
return perm_[i]; }
95 const IndexType*
data()
const {
return perm_.data(); }
114template <
typename IndexType,
typename ITIVectorType>
116 const ITIVectorType& b, ITIVectorType* result);
120template <
typename IndexType,
typename ITIVectorType>
122 const ITIVectorType& b, ITIVectorType* result);
126template <
typename RowIndexedVector>
129 RowIndexedVector* tmp) {
131 const int size = col_perm.
size().value();
132 if (size == 0)
return;
135 DCHECK_EQ(size, v->size());
137 tmp->resize(RowIndex(size));
138 const auto* from = v->data();
139 auto*
to = tmp->data();
140 for (
int i = 0; i < size; ++i) {
141 to[col_perm[ColIndex(i)].value()] = from[i];
150template <
typename IndexType>
152 const IndexType size = inverse.perm_.size();
154 for (IndexType i(0); i <
size; ++i) {
155 perm_[inverse[i]] = i;
159template <
typename IndexType>
161 const IndexType size = perm_.size();
162 perm_.resize(
size, IndexType(0));
163 for (IndexType i(0); i <
size; ++i) {
168template <
typename IndexType>
170 PopulateFromIdentity();
171 std::shuffle(perm_.begin(), perm_.end(), absl::BitGen());
174template <
typename IndexType>
176 const size_t size = perm_.size().value();
178 for (IndexType i(0); i <
size; ++i) {
179 if (perm_[i] < 0 || perm_[i] >=
size) {
182 visited[perm_[i]] =
true;
184 for (IndexType i(0); i < size; ++i) {
192template <
typename IndexType>
194 const size_t size = perm_.size().value();
198 for (IndexType i(0); i <
size; ++i) {
207 if ((cycle_size & 1) == 0) {
208 signature = -signature;
215template <
typename IndexType,
typename ITIVectorType>
217 const ITIVectorType& b, ITIVectorType* result) {
219 const IndexType size(perm.size());
225 DCHECK_EQ(size.value(), b.size().value());
226 result->resize(b.size(), b.back());
227 for (IndexType i(0); i < size; ++i) {
228 const typename ITIVectorType::IndexType ith_index(i.value());
229 const typename ITIVectorType::IndexType permuted(perm[i].value());
230 (*result)[permuted] = b[ith_index];
234template <
typename IndexType,
typename ITIVectorType>
236 const ITIVectorType& b, ITIVectorType* result) {
238 const IndexType size(perm.size());
244 DCHECK_EQ(size.value(), b.size().value());
245 result->resize(b.size(), b.back());
246 for (IndexType i(0); i < size; ++i) {
247 const typename ITIVectorType::IndexType ith_index(i.value());
248 const typename ITIVectorType::IndexType permuted(perm[i].value());
249 (*result)[ith_index] = b[permuted];
StrictITISpan< IndexType, const IndexType > const_view() const
int ComputeSignature() const
void PopulateFromInverse(const Permutation &inverse)
Permutation(IndexType size)
const IndexType * data() const
void PopulateFromIdentity()
Populates the calling object with the identity permutation.
Permutation & operator=(const Permutation &)=delete
IndexType & operator[](IndexType i)
IndexType operator[](IndexType i) const
void resize(IndexType size, IndexType value)
bool Check() const
Returns true if the calling object contains a permutation, false otherwise.
void PopulateRandomly()
Populates the calling object with a random permutation.
void assign(IndexType size, IndexType value)
Permutation(const Permutation &)=delete
This type is neither copyable nor movable.
Permutation< ColIndex > ColumnPermutation
Permutation< RowIndex > RowPermutation
void ApplyPermutation(const Permutation< IndexType > &perm, const ITIVectorType &b, ITIVectorType *result)
void ApplyColumnPermutationToRowIndexedVector(StrictITISpan< ColIndex, const ColIndex > col_perm, RowIndexedVector *v, RowIndexedVector *tmp)
void ApplyInversePermutation(const Permutation< IndexType > &perm, const ITIVectorType &b, ITIVectorType *result)
In SWIG mode, we don't want anything besides these top-level includes.
trees with all degrees equal to
#define RETURN_IF_NULL(x)