Google OR-Tools v9.9
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sparse_permutation.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 "absl/types/span.h"
23
24namespace operations_research {
25
26void SparsePermutation::RemoveCycles(absl::Span<const int> cycle_indices) {
27 // TODO(user): make this a class member to avoid allocation if the complexity
28 // becomes an issue. In this case, also optimize the loop below by not copying
29 // the first cycles.
30 std::vector<bool> should_be_deleted(NumCycles(), false);
31 for (int i : cycle_indices) {
32 DCHECK_GE(i, 0);
33 DCHECK_LT(i, NumCycles());
34 DCHECK(!should_be_deleted[i])
35 << "Duplicate index given to RemoveCycles(): " << i;
36 should_be_deleted[i] = true;
37 }
38 int new_cycles_size = 0; // new index in cycles_
39 int new_cycle_ends_size = 0; // new index in cycle_ends_
40 int start = 0;
41 for (int i = 0; i < NumCycles(); ++i) {
42 const int end = cycle_ends_[i];
43 if (!should_be_deleted[i]) {
44 for (int j = start; j < end; ++j) {
45 cycles_[new_cycles_size++] = cycles_[j];
46 }
47 cycle_ends_[new_cycle_ends_size++] = new_cycles_size;
48 }
49 start = end;
50 }
51 cycles_.resize(new_cycles_size);
52 cycle_ends_.resize(new_cycle_ends_size);
53}
54
55std::string SparsePermutation::DebugString() const {
56 DCHECK_EQ(cycles_.empty(), cycle_ends_.empty());
57 if (!cycles_.empty()) DCHECK_EQ(cycles_.size(), cycle_ends_.back());
58 std::vector<std::vector<int>> cycles;
59 int start = 0;
60 for (const int end : cycle_ends_) {
61 // Find the minimum.
62 int min_pos = start;
63 for (int i = start + 1; i < end; ++i) {
64 if (cycles_[i] < cycles_[min_pos]) min_pos = i;
65 }
66 std::vector<int> cycle;
67 for (int i = min_pos; i < end; ++i) cycle.push_back(cycles_[i]);
68 for (int i = start; i < min_pos; ++i) cycle.push_back(cycles_[i]);
69 cycles.push_back(cycle);
70 start = end;
71 }
72 std::sort(cycles.begin(), cycles.end());
73 std::string out;
74 for (const std::vector<int>& cycle : cycles) {
75 if (!out.empty()) out += " ";
76 out += "(";
77 out += absl::StrJoin(cycle, " ");
78 out += ")";
79 }
80 return out;
81}
82
83} // namespace operations_research
void RemoveCycles(absl::Span< const int > cycle_indices)
In SWIG mode, we don't want anything besides these top-level includes.
std::optional< int64_t > end
int64_t start