14 #ifndef OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINT_STORAGE_H_
15 #define OR_TOOLS_MATH_OPT_STORAGE_ATOMIC_CONSTRAINT_STORAGE_H_
21 #include "absl/algorithm/container.h"
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/container/flat_hash_set.h"
24 #include "google/protobuf/map.h"
25 #include "absl/log/check.h"
69 template <
typename Constra
intData>
72 using IdType =
typename ConstraintData::IdType;
73 using ProtoType =
typename ConstraintData::ProtoType;
97 const google::protobuf::Map<int64_t, ProtoType>& constraints);
100 template <
typename DiffIter>
108 int64_t
size()
const {
return constraint_data_.size(); }
115 next_id_ =
std::max(minimum, next_id_);
122 const VariableId variable_id)
const {
136 google::protobuf::Map<int64_t, ProtoType>
Proto()
const {
137 google::protobuf::Map<int64_t, ProtoType> constraints;
138 for (
const auto& [
id,
data] : constraint_data_) {
139 constraints[
id.value()] =
data.Proto();
147 return constraint_data_.at(
id);
159 typename ConstraintData::UpdatesProtoType
Update(
const Diff& diff)
const;
166 absl::flat_hash_map<IdType, ConstraintData> constraint_data_;
168 absl::flat_hash_map<VariableId, absl::flat_hash_set<IdType>>
169 constraints_by_variable_;
177 template <
typename IdType>
186 template <
typename Constra
intData>
189 ConstraintData constraint) {
190 const std::vector<VariableId> vars = constraint.RelatedVariables();
191 const IdType id = next_id_++;
192 CHECK(constraint_data_.insert({id, std::move(constraint)}).second);
193 for (
const VariableId v : vars) {
194 constraints_by_variable_[v].insert(
id);
199 template <
typename Constra
intData>
201 const google::protobuf::Map<int64_t, ProtoType>& constraints) {
204 CHECK_GE(
id, next_id())
205 <<
"constraint ID in map: " << raw_id
206 <<
" is smaller than next_id(): " << next_id().value();
207 ensure_next_id_at_least(
id);
208 AddConstraint(ConstraintData::FromProto(constraints.at(raw_id)));
212 template <
typename Constra
intData>
213 template <
typename DiffIter>
216 for (
Diff& diff : diffs) {
218 if (
id >= diff.checkpoint) {
221 diff.deleted_constraints.insert(
id);
223 const auto data = constraint_data_.find(
id);
224 CHECK(data != constraint_data_.end());
225 for (
const VariableId v : data->second.RelatedVariables()) {
226 constraints_by_variable_[v].erase(
id);
228 constraint_data_.erase(
id);
231 template <
typename Constra
intData>
233 VariableId variable_id) {
234 const auto it = constraints_by_variable_.find(variable_id);
235 if (it == constraints_by_variable_.end()) {
238 for (
const IdType constraint_id : it->second) {
239 constraint_data_.at(constraint_id).DeleteVariable(variable_id);
241 constraints_by_variable_.erase(it);
244 template <
typename Constra
intData>
245 std::vector<typename AtomicConstraintStorage<ConstraintData>::IdType>
247 std::vector<IdType> result;
248 for (
const auto& [
id, _] : constraint_data_) {
249 result.push_back(
id);
254 template <
typename Constra
intData>
255 std::vector<typename AtomicConstraintStorage<ConstraintData>::IdType>
257 std::vector<IdType> result = Constraints();
258 absl::c_sort(result);
262 template <
typename Constra
intData>
264 const Diff& diff)
const {
268 template <
typename Constra
intData>
275 template <
typename Constra
intData>
276 typename ConstraintData::UpdatesProtoType
280 update.mutable_deleted_constraint_ids()->Add(deleted_id.value());
282 absl::c_sort(*update.mutable_deleted_constraint_ids());
286 (*update.mutable_new_constraints())[
id.
value()] = data(
id).Proto();
bool diff_is_empty(const Diff &diff) const
void DeleteVariable(VariableId variable_id)
typename ConstraintData::IdType IdType
void ensure_next_id_at_least(const IdType minimum)
google::protobuf::Map< int64_t, ProtoType > Proto() const
void AddConstraints(const google::protobuf::Map< int64_t, ProtoType > &constraints)
typename ConstraintData::UpdatesProtoType UpdatesProtoType
std::vector< IdType > SortedConstraints() const
const absl::flat_hash_set< IdType > & RelatedConstraints(const VariableId variable_id) const
bool contains(const IdType id) const
IdType AddConstraint(ConstraintData constraint)
void AdvanceCheckpointInDiff(Diff &diff) const
typename ConstraintData::ProtoType ProtoType
ConstraintData::UpdatesProtoType Update(const Diff &diff) const
void Delete(IdType id, const iterator_range< DiffIter > &diffs)
std::vector< IdType > Constraints() const
const ConstraintData & data(const IdType id) const
const Collection::value_type::second_type & FindWithDefault(const Collection &collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
std::vector< K > SortedMapKeys(const absl::flat_hash_map< K, V > &in_map)
StrongIntRange< IntType > MakeStrongIntRange(IntType end)
absl::flat_hash_set< IdType > deleted_constraints
Diff(const AtomicConstraintStorage< ConstraintData > &storage)