14#ifndef OR_TOOLS_UTIL_DENSE_SET_H_
15#define OR_TOOLS_UTIL_DENSE_SET_H_
21#include "absl/log/check.h"
22#include "absl/types/span.h"
38template <
typename T,
bool auto_resize = true>
41 using iterator =
typename std::vector<T>::const_iterator;
48 size_t size()
const {
return values_.size(); }
49 bool empty()
const {
return values_.empty(); }
51 values_.reserve(
size);
52 if (
size >= positions_.size()) positions_.resize(
size, -1);
54 size_t capacity()
const {
return positions_.size(); }
57 const int pos = Position(
value);
59 DCHECK_GT(positions_.size(), ToInt(
value));
60 positions_[ToInt(
value)] = values_.size();
61 values_.push_back(
value);
62 return {values_.begin() + positions_[ToInt(
value)],
true};
64 return {values_.begin() + pos,
false};
68 const int pos = Position(
value);
69 DCHECK_GT(positions_.size(), ToInt(
value));
70 if (pos < 0)
return values_.end();
71 return values_.begin() + pos;
76 return positions_[ToInt(
value)] >= 0;
81 DCHECK_GT(positions_.size(), ToInt(
value));
82 positions_[ToInt(values_.back())] = it - values_.begin();
83 positions_[ToInt(
value)] = -1;
85 values_[it -
begin()] = values_.back();
90 const int pos = Position(
value);
91 if (pos < 0)
return 0;
92 DCHECK_GT(positions_.size(), ToInt(
value));
93 positions_[ToInt(values_.back())] = pos;
94 values_[pos] = values_.back();
96 positions_[ToInt(
value)] = -1;
102 absl::Span<const T>
values()
const {
return values_; }
107 for (
const T
value : values_) {
108 DCHECK_GT(positions_.size(), ToInt(
value));
109 positions_[ToInt(
value)] = -1;
116 inline int Position(T
value) {
117 int int_value = ToInt(
value);
118 DCHECK_GE(int_value, 0);
122 if (
kAutoResize && int_value >= positions_.size()) {
123 positions_.resize(ToInt(
value) + 1, -1);
125 DCHECK_GT(positions_.size(), int_value);
126 return positions_[int_value];
128 std::vector<int> positions_;
129 std::vector<T> values_;
137template <
typename T,
bool resize>
139 return value.value();
142inline int DenseSet<int, true>::ToInt(
int value) {
146inline int DenseSet<int, false>::ToInt(
int value) {
typename std::vector< T >::const_iterator const_iterator
const_iterator end() const
const_iterator begin() const
std::pair< iterator, bool > insert(T value)
typename std::vector< T >::const_iterator iterator
static constexpr bool kAutoResize
absl::Span< const T > values() const
bool contains(T value) const
void reserve(size_t size)
In SWIG mode, we don't want anything besides these top-level includes.