Google OR-Tools v9.11
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 <utility>
19#include <vector>
20
21#include "absl/strings/str_join.h"
22#include "absl/types/span.h"
24
25namespace operations_research {
26
27void SparsePermutation::RemoveCycles(absl::Span<const int> cycle_indices) {
28 // TODO(user): make this a class member to avoid allocation if the complexity
29 // becomes an issue. In this case, also optimize the loop below by not copying
30 // the first cycles.
31 std::vector<bool> should_be_deleted(NumCycles(), false);
32 for (int i : cycle_indices) {
33 DCHECK_GE(i, 0);
34 DCHECK_LT(i, NumCycles());
35 DCHECK(!should_be_deleted[i])
36 << "Duplicate index given to RemoveCycles(): " << i;
37 should_be_deleted[i] = true;
38 }
39 int new_cycles_size = 0; // new index in cycles_
40 int new_cycle_ends_size = 0; // new index in cycle_ends_
41 int start = 0;
42 for (int i = 0; i < NumCycles(); ++i) {
43 const int end = cycle_ends_[i];
44 if (!should_be_deleted[i]) {
45 for (int j = start; j < end; ++j) {
46 cycles_[new_cycles_size++] = cycles_[j];
47 }
48 cycle_ends_[new_cycle_ends_size++] = new_cycles_size;
49 }
50 start = end;
51 }
52 cycles_.resize(new_cycles_size);
53 cycle_ends_.resize(new_cycle_ends_size);
54}
55
56std::string SparsePermutation::DebugString() const {
57 DCHECK_EQ(cycles_.empty(), cycle_ends_.empty());
58 if (!cycles_.empty()) DCHECK_EQ(cycles_.size(), cycle_ends_.back());
59 std::vector<std::vector<int>> cycles;
60 int start = 0;
61 for (const int end : cycle_ends_) {
62 // Find the minimum.
63 int min_pos = start;
64 for (int i = start + 1; i < end; ++i) {
65 if (cycles_[i] < cycles_[min_pos]) min_pos = i;
66 }
67 std::vector<int> cycle;
68 for (int i = min_pos; i < end; ++i) cycle.push_back(cycles_[i]);
69 for (int i = start; i < min_pos; ++i) cycle.push_back(cycles_[i]);
70 cycles.push_back(std::move(cycle));
71 start = end;
72 }
73 std::sort(cycles.begin(), cycles.end());
74 std::string out;
75 for (const std::vector<int>& cycle : cycles) {
76 if (!out.empty()) out += " ";
77 out += "(";
78 out += absl::StrJoin(cycle, " ");
79 out += ")";
80 }
81 return out;
82}
83
84} // 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