OR-Tools  9.6
sparse_permutation.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <algorithm>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/strings/str_join.h"
21 #include "ortools/base/logging.h"
22 
23 namespace operations_research {
24 
25 void SparsePermutation::RemoveCycles(const std::vector<int>& cycle_indices) {
26  // TODO(user): make this a class member to avoid allocation if the complexity
27  // becomes an issue. In this case, also optimize the loop below by not copying
28  // the first cycles.
29  std::vector<bool> should_be_deleted(NumCycles(), false);
30  for (int i : cycle_indices) {
31  DCHECK_GE(i, 0);
32  DCHECK_LT(i, NumCycles());
33  DCHECK(!should_be_deleted[i])
34  << "Duplicate index given to RemoveCycles(): " << i;
35  should_be_deleted[i] = true;
36  }
37  int new_cycles_size = 0; // new index in cycles_
38  int new_cycle_ends_size = 0; // new index in cycle_ends_
39  int start = 0;
40  for (int i = 0; i < NumCycles(); ++i) {
41  const int end = cycle_ends_[i];
42  if (!should_be_deleted[i]) {
43  for (int j = start; j < end; ++j) {
44  cycles_[new_cycles_size++] = cycles_[j];
45  }
46  cycle_ends_[new_cycle_ends_size++] = new_cycles_size;
47  }
48  start = end;
49  }
50  cycles_.resize(new_cycles_size);
51  cycle_ends_.resize(new_cycle_ends_size);
52 }
53 
54 std::string SparsePermutation::DebugString() const {
55  DCHECK_EQ(cycles_.empty(), cycle_ends_.empty());
56  if (!cycles_.empty()) DCHECK_EQ(cycles_.size(), cycle_ends_.back());
57  std::vector<std::vector<int>> cycles;
58  int start = 0;
59  for (const int end : cycle_ends_) {
60  // Find the minimum.
61  int min_pos = start;
62  for (int i = start + 1; i < end; ++i) {
63  if (cycles_[i] < cycles_[min_pos]) min_pos = i;
64  }
65  std::vector<int> cycle;
66  for (int i = min_pos; i < end; ++i) cycle.push_back(cycles_[i]);
67  for (int i = start; i < min_pos; ++i) cycle.push_back(cycles_[i]);
68  cycles.push_back(cycle);
69  start = end;
70  }
71  std::sort(cycles.begin(), cycles.end());
72  std::string out;
73  for (const std::vector<int>& cycle : cycles) {
74  if (!out.empty()) out += " ";
75  out += "(";
76  out += absl::StrJoin(cycle, " ");
77  out += ")";
78  }
79  return out;
80 }
81 
82 } // namespace operations_research
void RemoveCycles(const std::vector< int > &cycle_indices)
Collection of objects used to extend the Constraint Solver library.
std::optional< int64_t > end
int64_t start