16
16
#ifndef OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
17
17
#define OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
18
18
19
- #include < cstdint>
20
19
#include < optional>
21
- #include < ostream>
22
20
#include < string>
23
21
#include < vector>
24
22
25
23
#include " absl/strings/string_view.h"
26
- #include " ortools/base/strong_int.h"
27
24
#include " ortools/math_opt/constraints/util/model_util.h"
28
25
#include " ortools/math_opt/cpp/variable_and_expressions.h"
26
+ #include " ortools/math_opt/elemental/elements.h"
29
27
#include " ortools/math_opt/storage/model_storage.h"
28
+ #include " ortools/math_opt/storage/model_storage_item.h"
30
29
31
30
namespace operations_research ::math_opt {
32
31
33
32
// A value type that references an indicator constraint from ModelStorage.
34
33
// 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> {
38
37
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;
46
39
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 ;
51
41
52
42
// Returns nullopt if the indicator variable is unset (this is a valid state,
53
43
// in which the constraint is functionally ignored).
@@ -65,91 +55,36 @@ class IndicatorConstraint {
65
55
// Returns a detailed string description of the contents of the constraint
66
56
// (not its name, use `<<` for that instead).
67
57
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_;
81
58
};
82
59
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
-
88
60
// //////////////////////////////////////////////////////////////////////////////
89
61
// Inline function implementations
90
62
// //////////////////////////////////////////////////////////////////////////////
91
63
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 ;
101
67
}
102
68
return kDeletedConstraintDefaultDescription ;
103
69
}
104
70
105
71
std::optional<Variable> IndicatorConstraint::indicator_variable () const {
106
72
const std::optional<VariableId> maybe_indicator =
107
- storage_ ->constraint_data (id_ ).indicator ;
73
+ storage () ->constraint_data (typed_id () ).indicator ;
108
74
if (!maybe_indicator.has_value ()) {
109
75
return std::nullopt;
110
76
}
111
- return Variable (storage_ , *maybe_indicator);
77
+ return Variable (storage () , *maybe_indicator);
112
78
}
113
79
114
80
bool IndicatorConstraint::activate_on_zero () const {
115
- return storage_ ->constraint_data (id_ ).activate_on_zero ;
81
+ return storage () ->constraint_data (typed_id () ).activate_on_zero ;
116
82
}
117
83
118
84
std::vector<Variable> IndicatorConstraint::NonzeroVariables () const {
119
- return AtomicConstraintNonzeroVariables (*storage_, id_ );
85
+ return AtomicConstraintNonzeroVariables (*storage (), typed_id () );
120
86
}
121
87
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
-
153
88
} // namespace operations_research::math_opt
154
89
155
90
#endif // OR_TOOLS_MATH_OPT_CONSTRAINTS_INDICATOR_INDICATOR_CONSTRAINT_H_
0 commit comments