78 #ifndef OR_TOOLS_UTIL_PERMUTATION_H_
79 #define OR_TOOLS_UTIL_PERMUTATION_H_
88 template <
typename IndexType>
97 IndexType destination)
const = 0;
111 virtual void SetSeen(IndexType* unused_permutation_element)
const {
112 LOG(FATAL) <<
"Base implementation of SetSeen() must not be called.";
122 virtual bool Unseen(IndexType unused_permutation_element)
const {
123 LOG(FATAL) <<
"Base implementation of Unseen() must not be called.";
141 template <
typename DataType,
typename IndexType>
148 IndexType destination)
const override {
149 data_[destination] = data_[source];
152 data_[destination] = temp_;
154 void SetSeen(IndexType* permutation_element)
const override {
155 *permutation_element = -*permutation_element - 1;
157 bool Unseen(IndexType permutation_element)
const override {
158 return permutation_element >= 0;
174 template <
typename IndexType>
178 : cycle_handler_(cycle_handler) {}
180 void Apply(IndexType permutation[],
int permutation_start,
181 int permutation_end) {
182 for (IndexType current = permutation_start; current < permutation_end;
184 IndexType
next = permutation[current];
186 const IndexType cycle_start = current;
187 if (cycle_handler_->Unseen(
next)) {
188 cycle_handler_->SetSeen(&permutation[current]);
189 DCHECK(!cycle_handler_->Unseen(permutation[current]));
190 cycle_handler_->SetTempFromIndex(current);
191 while (cycle_handler_->Unseen(permutation[
next])) {
192 cycle_handler_->SetIndexFromIndex(
next, current);
195 cycle_handler_->SetSeen(&permutation[current]);
196 DCHECK(!cycle_handler_->Unseen(permutation[current]));
198 cycle_handler_->SetIndexFromTemp(current);
202 DCHECK_EQ(cycle_start, current);
void SetIndexFromIndex(IndexType source, IndexType destination) const override
void SetIndexFromTemp(IndexType destination) const override
ArrayIndexCycleHandler(DataType *data)
bool Unseen(IndexType permutation_element) const override
void SetTempFromIndex(IndexType source) override
void SetSeen(IndexType *permutation_element) const override
PermutationApplier(PermutationCycleHandler< IndexType > *cycle_handler)
void Apply(IndexType permutation[], int permutation_start, int permutation_end)
virtual void SetIndexFromTemp(IndexType destination) const =0
virtual ~PermutationCycleHandler()
virtual bool Unseen(IndexType unused_permutation_element) const
virtual void SetTempFromIndex(IndexType source)=0
virtual void SetSeen(IndexType *unused_permutation_element) const
virtual void SetIndexFromIndex(IndexType source, IndexType destination) const =0
PermutationCycleHandler()
Collection of objects used to extend the Constraint Solver library.