Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
duplicate_remover.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 <cstddef>
17#include <cstdint>
18
19#include "absl/log/check.h"
20#include "absl/types/span.h"
21
22namespace operations_research {
23
24size_t DenseIntDuplicateRemover::RemoveDuplicatesInternal(
25 absl::Span<int> span) {
26 // We use vector<uint8_t> because using vector<bool> would be potentially more
27 // expensive: writing in vector<bool> involves a read+write, and here we're
28 // directly writing.
29 int num_unique_kept = -1;
30 // Fast track for the leading portion without duplicates.
31 while (++num_unique_kept < span.size()) {
32 const int x = span[num_unique_kept];
33 DCHECK_GE(x, 0);
34 DCHECK_LT(x, tmp_mask_.size() * 8);
35 // Bit #i = Bit #(i modulo 8) of Byte #(i / 8).
36 const uint8_t mask = 1u << (x & 7); // Bit #(i modulo 8).
37 const uint8_t byte = tmp_mask_[x >> 3]; // .. of Byte #(i / 8).
38 if (mask & byte) break; // Already seen.
39 tmp_mask_[x >> 3] = byte | mask;
40 }
41 // The next portion is exactly the same, except that now we have to shift
42 // the elements that we're keeping, making it slightly slower.
43 for (int i = num_unique_kept + 1; i < span.size(); ++i) {
44 const int x = span[i];
45 DCHECK_GE(x, 0);
46 DCHECK_LT(x, tmp_mask_.size() * 8);
47 const uint8_t mask = 1 << (x & 7);
48 const uint8_t byte = tmp_mask_[x >> 3];
49 if (mask & byte) continue; // Already seen.
50 tmp_mask_[x >> 3] = mask | byte;
51 span[num_unique_kept++] = x; // Keep x=[i], at its new (shifted) position.
52 }
53 span.remove_suffix(span.size() - num_unique_kept);
54 // Clear the bit mask.
55 for (int x : span) tmp_mask_[x >> 3] = 0;
56 return num_unique_kept;
57}
58
59} // namespace operations_research
In SWIG mode, we don't want anything besides these top-level includes.
const Variable x
Definition qp_tests.cc:127