14#ifndef OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENT_STORAGE_H_
15#define OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENT_STORAGE_H_
24#include "absl/container/flat_hash_map.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/string_view.h"
39 int64_t
Add(
const absl::string_view name) {
40 const int64_t
id = elements_.size();
41 elements_.emplace_back(name);
45 bool exists(
const int64_t
id)
const {
46 return 0 <=
id &&
id < elements_.size();
49 absl::StatusOr<absl::string_view>
GetName(
const int64_t
id)
const {
58 std::vector<int64_t>
AllIds()
const;
60 int64_t
size()
const {
return elements_.size(); }
64 std::vector<std::string> elements_;
72 int64_t
Add(
const absl::string_view name) {
73 const int64_t
id = next_id_;
74 elements_.try_emplace(
id, name);
79 bool Erase(int64_t
id) {
return elements_.erase(
id) > 0; }
81 bool exists(int64_t
id)
const {
return elements_.contains(
id); }
83 absl::StatusOr<absl::string_view>
GetName(int64_t
id)
const {
84 if (
const auto it = elements_.find(
id); it != elements_.end()) {
90 int64_t
next_id()
const {
return next_id_; }
92 std::vector<int64_t>
AllIds()
const;
94 int64_t
size()
const {
return elements_.size(); }
97 next_id_ = std::max(next_id_,
id);
101 absl::flat_hash_map<int64_t, std::string> elements_;
102 int64_t next_id_ = 0;
116 template <
typename Self,
typename Fn>
117 static auto Visit(Self& self, Fn fn) {
118 if (std::holds_alternative<detail::DenseElementStorage>(self.impl_)) {
119 return fn(std::get<detail::DenseElementStorage>(self.impl_));
121 return fn(std::get<detail::SparseElementStorage>(self.impl_));
131 int64_t
Add(
const absl::string_view name) {
132 return Visit(*
this, [name](
auto& impl) {
return impl.Add(name); });
138 bool Erase(
const int64_t
id) {
return AsSparse().Erase(
id); }
142 return Visit(*
this, [
id](
auto& impl) {
return impl.exists(
id); });
147 absl::StatusOr<absl::string_view>
GetName(
const int64_t
id)
const {
148 return Visit(*
this, [
id](
auto& impl) {
return impl.GetName(
id); });
155 return Visit(*
this, [](
auto& impl) {
return impl.next_id(); });
160 std::vector<int64_t>
AllIds()
const;
164 return Visit(*
this, [](
auto& impl) {
return impl.size(); });
173 AsSparse().ensure_next_id_at_least(
id);
179 if (
auto* sparse = std::get_if<detail::SparseElementStorage>(&impl_)) {
182 impl_ = detail::SparseElementStorage(
183 std::move(std::get<detail::DenseElementStorage>(impl_)));
184 return std::get<detail::SparseElementStorage>(impl_);
187 std::variant<detail::DenseElementStorage, detail::SparseElementStorage> impl_;
int64_t size() const
Returns the number of elements added and not erased.
absl::StatusOr< absl::string_view > GetName(const int64_t id) const
void EnsureNextIdAtLeast(const int64_t id)
std::vector< int64_t > AllIds() const
int64_t Add(const absl::string_view name)
Creates a new element and returns its id.
bool Exists(const int64_t id) const
Returns true an element with this id was created and not yet erased.
bool Erase(const int64_t id)
std::vector< int64_t > AllIds() const
friend class SparseElementStorage
bool exists(const int64_t id) const
int64_t Add(const absl::string_view name)
absl::StatusOr< absl::string_view > GetName(const int64_t id) const
A sparse element storage, which supports deletion.
int64_t Add(const absl::string_view name)
void ensure_next_id_at_least(int64_t id)
bool exists(int64_t id) const
SparseElementStorage(DenseElementStorage &&dense)
std::vector< int64_t > AllIds() const
absl::StatusOr< absl::string_view > GetName(int64_t id) const
An object oriented wrapper for quadratic constraints in ModelStorage.
StatusBuilder InvalidArgumentErrorBuilder()