14#ifndef OR_TOOLS_MATH_OPT_STORAGE_SPARSE_COEFFICIENT_MAP_H_
15#define OR_TOOLS_MATH_OPT_STORAGE_SPARSE_COEFFICIENT_MAP_H_
19#include "absl/container/flat_hash_map.h"
20#include "absl/meta/type_traits.h"
22#include "ortools/math_opt/sparse_containers.pb.h"
38 absl::flat_hash_map<VariableId, double>
terms);
41 inline double get(VariableId
id)
const;
44 inline bool set(VariableId
id,
double coeff);
46 const absl::flat_hash_map<VariableId, double>&
terms()
const {
53 inline void erase(VariableId
id);
55 SparseDoubleVectorProto
Proto()
const;
58 absl::flat_hash_map<VariableId, double> terms_;
66 absl::flat_hash_map<VariableId, double> terms)
67 : terms_(
std::move(terms)) {
68 absl::erase_if(terms_, [](
const auto& term) {
return term.second == 0.0; });
72 const auto it = terms_.find(
id);
73 if (it == terms_.end()) {
81 return terms_.erase(
id) > 0;
83 const auto [iterator, inserted] = terms_.try_emplace(
id, coeff);
87 if (iterator->second != coeff) {
88 iterator->second = coeff;
SparseCoefficientMap()=default
bool set(VariableId id, double coeff)
Returns true if the stored value changes.
SparseDoubleVectorProto Proto() const
const absl::flat_hash_map< VariableId, double > & terms() const
double get(VariableId id) const
Returns 0.0 by default if no value is set.
void erase(VariableId id)
Has no effect if id is not set.
An object oriented wrapper for quadratic constraints in ModelStorage.