Files
ortools-clone/ortools/algorithms/sparse_permutation.cc

82 lines
2.7 KiB
C++
Raw Normal View History

// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/algorithms/sparse_permutation.h"
#include <algorithm>
2022-05-04 08:23:41 +02:00
#include <string>
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
#include "absl/strings/str_join.h"
2018-06-08 16:40:43 +02:00
#include "ortools/base/logging.h"
namespace operations_research {
void SparsePermutation::RemoveCycles(const std::vector<int>& cycle_indices) {
// TODO(user): make this a class member to avoid allocation if the complexity
// becomes an issue. In this case, also optimize the loop below by not copying
// the first cycles.
std::vector<bool> should_be_deleted(NumCycles(), false);
for (int i : cycle_indices) {
DCHECK_GE(i, 0);
DCHECK_LT(i, NumCycles());
DCHECK(!should_be_deleted[i])
<< "Duplicate index given to RemoveCycles(): " << i;
should_be_deleted[i] = true;
}
2020-10-22 23:36:58 +02:00
int new_cycles_size = 0; // new index in cycles_
int new_cycle_ends_size = 0; // new index in cycle_ends_
int start = 0;
for (int i = 0; i < NumCycles(); ++i) {
const int end = cycle_ends_[i];
if (!should_be_deleted[i]) {
for (int j = start; j < end; ++j) {
cycles_[new_cycles_size++] = cycles_[j];
}
cycle_ends_[new_cycle_ends_size++] = new_cycles_size;
}
start = end;
}
cycles_.resize(new_cycles_size);
cycle_ends_.resize(new_cycle_ends_size);
}
std::string SparsePermutation::DebugString() const {
DCHECK_EQ(cycles_.empty(), cycle_ends_.empty());
2020-10-22 23:36:58 +02:00
if (!cycles_.empty()) DCHECK_EQ(cycles_.size(), cycle_ends_.back());
std::vector<std::vector<int>> cycles;
int start = 0;
for (const int end : cycle_ends_) {
// Find the minimum.
int min_pos = start;
for (int i = start + 1; i < end; ++i) {
2020-10-22 23:36:58 +02:00
if (cycles_[i] < cycles_[min_pos]) min_pos = i;
}
std::vector<int> cycle;
2020-10-22 23:36:58 +02:00
for (int i = min_pos; i < end; ++i) cycle.push_back(cycles_[i]);
for (int i = start; i < min_pos; ++i) cycle.push_back(cycles_[i]);
cycles.push_back(cycle);
start = end;
}
std::sort(cycles.begin(), cycles.end());
std::string out;
for (const std::vector<int>& cycle : cycles) {
2020-10-22 23:36:58 +02:00
if (!out.empty()) out += " ";
out += "(";
out += absl::StrJoin(cycle, " ");
out += ")";
}
return out;
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research