1616#ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
1717#define OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
1818
19- #include < cstdint>
2019#include < optional>
21- #include < ostream>
2220#include < string>
2321#include < vector>
2422
2523#include " absl/strings/string_view.h"
26- #include " ortools/base/strong_int.h"
2724#include " ortools/math_opt/constraints/util/model_util.h"
2825#include " ortools/math_opt/cpp/variable_and_expressions.h"
26+ #include " ortools/math_opt/elemental/elements.h"
2927#include " ortools/math_opt/storage/model_storage.h"
28+ #include " ortools/math_opt/storage/model_storage_item.h"
3029
3130namespace operations_research ::math_opt {
3231
3332// A value type that references an indicator constraint from ModelStorage.
3433// Usually this type is passed by copy.
35- //
36- // This type implements https://abseil.io/docs/cpp/guides/hash.
37- class IndicatorConstraint {
34+ class IndicatorConstraint final
35+ : public ModelStorageElement<ElementType:: kIndicatorConstraint ,
36+ IndicatorConstraint> {
3837 public:
39- // The typed integer used for ids.
40- using IdType = IndicatorConstraintId;
41-
42- inline IndicatorConstraint (const ModelStorage* storage,
43- IndicatorConstraintId id);
44-
45- inline int64_t id () const ;
38+ using ModelStorageElement::ModelStorageElement;
4639
47- inline IndicatorConstraintId typed_id () const ;
48- inline const ModelStorage* storage () const ;
49-
50- inline absl::string_view name () const ;
40+ absl::string_view name () const ;
5141
5242 // Returns nullopt if the indicator variable is unset (this is a valid state,
5343 // in which the constraint is functionally ignored).
@@ -65,91 +55,36 @@ class IndicatorConstraint {
6555 // Returns a detailed string description of the contents of the constraint
6656 // (not its name, use `<<` for that instead).
6757 std::string ToString () const ;
68-
69- friend inline bool operator ==(const IndicatorConstraint& lhs,
70- const IndicatorConstraint& rhs);
71- friend inline bool operator !=(const IndicatorConstraint& lhs,
72- const IndicatorConstraint& rhs);
73- template <typename H>
74- friend H AbslHashValue (H h, const IndicatorConstraint& constraint);
75- friend std::ostream& operator <<(std::ostream& ostr,
76- const IndicatorConstraint& constraint);
77-
78- private:
79- const ModelStorage* storage_;
80- IndicatorConstraintId id_;
8158};
8259
83- // Streams the name of the constraint, as registered upon constraint creation,
84- // or a short default if none was provided.
85- inline std::ostream& operator <<(std::ostream& ostr,
86- const IndicatorConstraint& constraint);
87-
8860// //////////////////////////////////////////////////////////////////////////////
8961// Inline function implementations
9062// //////////////////////////////////////////////////////////////////////////////
9163
92- int64_t IndicatorConstraint::id () const { return id_.value (); }
93-
94- IndicatorConstraintId IndicatorConstraint::typed_id () const { return id_; }
95-
96- const ModelStorage* IndicatorConstraint::storage () const { return storage_; }
97-
98- absl::string_view IndicatorConstraint::name () const {
99- if (storage_->has_constraint (id_)) {
100- return storage_->constraint_data (id_).name ;
64+ inline absl::string_view IndicatorConstraint::name () const {
65+ if (storage ()->has_constraint (typed_id ())) {
66+ return storage ()->constraint_data (typed_id ()).name ;
10167 }
10268 return kDeletedConstraintDefaultDescription ;
10369}
10470
10571std::optional<Variable> IndicatorConstraint::indicator_variable () const {
10672 const std::optional<VariableId> maybe_indicator =
107- storage_ ->constraint_data (id_ ).indicator ;
73+ storage () ->constraint_data (typed_id () ).indicator ;
10874 if (!maybe_indicator.has_value ()) {
10975 return std::nullopt ;
11076 }
111- return Variable (storage_ , *maybe_indicator);
77+ return Variable (storage () , *maybe_indicator);
11278}
11379
11480bool IndicatorConstraint::activate_on_zero () const {
115- return storage_ ->constraint_data (id_ ).activate_on_zero ;
81+ return storage () ->constraint_data (typed_id () ).activate_on_zero ;
11682}
11783
11884std::vector<Variable> IndicatorConstraint::NonzeroVariables () const {
119- return AtomicConstraintNonzeroVariables (*storage_, id_ );
85+ return AtomicConstraintNonzeroVariables (*storage (), typed_id () );
12086}
12187
122- bool operator ==(const IndicatorConstraint& lhs,
123- const IndicatorConstraint& rhs) {
124- return lhs.id_ == rhs.id_ && lhs.storage_ == rhs.storage_ ;
125- }
126-
127- bool operator !=(const IndicatorConstraint& lhs,
128- const IndicatorConstraint& rhs) {
129- return !(lhs == rhs);
130- }
131-
132- template <typename H>
133- H AbslHashValue (H h, const IndicatorConstraint& constraint) {
134- return H::combine (std::move (h), constraint.id_ .value (), constraint.storage_ );
135- }
136-
137- std::ostream& operator <<(std::ostream& ostr,
138- const IndicatorConstraint& constraint) {
139- // TODO(b/170992529): handle quoting of invalid characters in the name.
140- const absl::string_view name = constraint.name ();
141- if (name.empty ()) {
142- ostr << " __indic_con#" << constraint.id () << " __" ;
143- } else {
144- ostr << name;
145- }
146- return ostr;
147- }
148-
149- IndicatorConstraint::IndicatorConstraint (const ModelStorage* const storage,
150- const IndicatorConstraintId id)
151- : storage_(storage), id_(id) {}
152-
15388} // namespace operations_research::math_opt
15489
15590#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
0 commit comments