Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
element_ref_tracker.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_ELEMENT_REF_TRACKER_H_
15#define OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENT_REF_TRACKER_H_
16
17#include "absl/container/flat_hash_map.h"
18#include "absl/container/flat_hash_set.h"
22
24
25template <typename ValueType, int n, typename Symmetry>
27
28// The `ElementRefTracker` for a given attribute type descriptor.
29template <typename Descriptor>
31 ElementRefTracker<typename Descriptor::ValueType,
32 Descriptor::kNumKeyElements,
33 typename Descriptor::Symmetry>;
34
35// A tracker for values that reference elements.
36//
37// This is used to delete attributes when the elements they reference are
38// deleted.
39template <ElementType element_type, int n, typename Symmetry>
40class ElementRefTracker<ElementId<element_type>, n, Symmetry> {
41 public:
44
45 // Returns the set of keys that reference element `id`.
46 const absl::flat_hash_set<Key>& GetKeysReferencing(const ElemId id) const {
47 return gtl::FindWithDefault(element_id_to_attr_keys_, id);
48 }
49
50 // Tracks the fact that attribute with key `key` has a value that references
51 // elements.
52 void Track(const Key key, const ElemId id) {
53 element_id_to_attr_keys_[id].insert(key);
54 }
55
56 void Untrack(const Key key, const ElemId id) {
57 const auto it = element_id_to_attr_keys_.find(id);
58 if (it != element_id_to_attr_keys_.end()) {
59 it->second.erase(key);
60 if (it->second.empty()) {
61 element_id_to_attr_keys_.erase(it);
62 }
63 }
64 }
65
66 void Clear() { element_id_to_attr_keys_.clear(); }
67
68 private:
69 // A map of element id to the list of attribute keys that have a non-default
70 // value for this element.
71 absl::flat_hash_map<ElemId, absl::flat_hash_set<Key>>
72 element_id_to_attr_keys_;
73};
74
75// Other value types do not need tracking.
76template <typename ValueType, int n, typename Symmetry>
78
79} // namespace operations_research::math_opt
80
81#endif // OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENT_REF_TRACKER_H_
A strongly typed element id. Value type.
Definition elements.h:64
const absl::flat_hash_set< Key > & GetKeysReferencing(const ElemId id) const
Returns the set of keys that reference element id.
Other value types do not need tracking.
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
Definition map_util.h:36
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
ElementRefTracker< typename Descriptor::ValueType, Descriptor::kNumKeyElements, typename Descriptor::Symmetry > ElementRefTrackerForAttrTypeDescriptor
The ElementRefTracker for a given attribute type descriptor.