Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
diff.h
Go to the documentation of this file.
1// Copyright 2010-2025 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
14#ifndef OR_TOOLS_MATH_OPT_ELEMENTAL_DIFF_H_
15#define OR_TOOLS_MATH_OPT_ELEMENTAL_DIFF_H_
16
17#include <array>
18#include <cstdint>
19
20#include "absl/container/flat_hash_set.h"
21#include "absl/types/span.h"
28
30
31// Stores the modifications to the model since the previous checkpoint (or since
32// creation of the Diff if Advance() has never been called).
33//
34// Only the following modifications are tracked explicitly:
35// * elements before the checkpoint
36// * attributes with all elements in the key before the checkpoint
37// as all changes involving an element after the checkpoint are implied to be in
38// the difference.
39//
40// Note: users of ElementalImpl can only access a const Diff.
41//
42// When a element is deleted from the model, the creator of the Diff is
43// responsible both for:
44// 1. Calling Diff::DeleteElement() on the element,
45// 2. For each Attr with a key element on the element type, calling
46// Diff::EraseKeysForAttr()
47// We cannot do this all at once for the user, as we do not have access to the
48// relevant related keys in steps 3/4 above.
49class Diff {
50 public:
51 Diff() = default;
52
53 // Discards all tracked modifications, and in the future, track only
54 // modifications where all elements are at most checkpoint.
55 //
56 // Generally, checkpoints should be component-wise non-decreasing with each
57 // invocation of Advance(), but this is not checked here.
58 void Advance(const std::array<int64_t, kNumElements>& checkpoints);
59
61 // Elements
63
64 // The current checkpoint for the element type `e`.
65 //
66 // This equals the next element id for the element type `e` when Advance() was
67 // last called (or at creation time if advance was never called).
68 int64_t checkpoint(const ElementType e) const {
69 return element_diff(e).checkpoint();
70 }
71
72 // The elements of element type `e` that have been deleted since the last call
73 // to Advance() with id less than the checkpoint.
74 const absl::flat_hash_set<int64_t>& deleted_elements(
75 const ElementType e) const {
76 return element_diff(e).deleted();
77 }
78
79 // Tracks the element `id` of element type `e` as deleted if it is less than
80 // the checkpoint.
81 //
82 // WARNING: this does not update any related attributes.
83 void DeleteElement(const ElementType e, int64_t id) {
84 mutable_element_diff(e).Delete(id);
85 }
86
88 // Attributes
90
91 // Returns the keys with all elements below the checkpoint where the Attr2 `a`
92 // was modified since the last call to Advance().
93 template <typename AttrType>
95 const AttrType a) const {
96 return attr_diffs_[a].modified_keys();
97 }
99 // Marks that the attribute `a` has been modified for `attr_key`.
100 template <typename AttrType>
101 void SetModified(const AttrType a, const AttrKeyFor<AttrType> attr_key) {
102 if (IsBeforeCheckpoint(a, attr_key)) {
103 attr_diffs_[a].SetModified(attr_key);
104 }
106
107 // Discard any tracked modifications for attribute `a` on `keys`.
108 //
109 // Typically invoke when the element with id `keys[i]` is deleted from the
110 // model, and where `keys` are the keys `k` for all elements `e` where the `e`
111 // has a non-default value for `a`.
112 template <typename AttrType>
113 void EraseKeysForAttr(const AttrType a,
114 absl::Span<const AttrKeyFor<AttrType>> keys) {
115 if (!attr_diffs_[a].has_modified_keys()) {
116 return;
118 for (const auto& attr_key : keys) {
119 if (IsBeforeCheckpoint(a, attr_key)) {
120 attr_diffs_[a].Erase(attr_key);
121 }
122 }
123 }
124
125 private:
126 const ElementDiff& element_diff(const ElementType e) const {
127 // TODO(b/365997645): post C++ 23, prefer std::to_underlying(e).
128 return element_diffs_[static_cast<int>(e)];
129 }
130
131 ElementDiff& mutable_element_diff(const ElementType e) {
132 return element_diffs_[static_cast<int>(e)];
133 }
134
135 // Returns true if all elements if `key` are before their respective
136 // checkpoints.
137 template <typename AttrType>
138 bool IsBeforeCheckpoint(const AttrType a, const AttrKeyFor<AttrType> key) {
139 for (int i = 0; i < GetAttrKeySize<AttrType>(); ++i) {
140 if (key[i] >= element_diff(GetElementTypes(a)[i]).checkpoint()) {
141 return false;
142 }
143 }
144 return true;
145 }
146
147 std::array<ElementDiff, kNumElements> element_diffs_;
148
149 template <int i>
150 using DiffForAttr = AttrDiff<AllAttrs::TypeDescriptor<i>::kNumKeyElements,
151 typename AllAttrs::TypeDescriptor<i>::Symmetry>;
152 AttrMap<DiffForAttr> attr_diffs_;
153};
154
155} // namespace operations_research::math_opt
156
157#endif // OR_TOOLS_MATH_OPT_ELEMENTAL_DIFF_H_
void EraseKeysForAttr(const AttrType a, absl::Span< const AttrKeyFor< AttrType > > keys)
Definition diff.h:117
const absl::flat_hash_set< int64_t > & deleted_elements(const ElementType e) const
Definition diff.h:76
void Advance(const std::array< int64_t, kNumElements > &checkpoints)
Definition diff.cc:23
void DeleteElement(const ElementType e, int64_t id)
Definition diff.h:85
int64_t checkpoint(const ElementType e) const
Definition diff.h:70
void SetModified(const AttrType a, const AttrKeyFor< AttrType > attr_key)
Marks that the attribute a has been modified for attr_key.
Definition diff.h:105
const AttrKeyHashSet< AttrKeyFor< AttrType > > & modified_keys(const AttrType a) const
Definition diff.h:98
void Delete(int64_t id)
Tracks the element id as deleted if it is less than the checkpoint.
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
AttrKey< AttrTypeDescriptorT< AttrType >::kNumKeyElements, typename AttrTypeDescriptorT< AttrType >::Symmetry > AttrKeyFor
The type of the AttrKey for attribute type AttrType.
std::conditional_t<(AttrKeyT::size() > 0), absl::flat_hash_set< AttrKeyT >, detail::AttrKey0RawSet< typename AttrKeyT::SymmetryT, AttrKeyT > > AttrKeyHashSet
A hash set of AttrKeyT, where AttrKeyT is an AttrKey<n, Symmetry>.
Definition attr_key.h:347
constexpr std::array< ElementType, GetAttrKeySize< attr >()> GetElementTypes()