14#ifndef OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENTS_H_
15#define OR_TOOLS_MATH_OPT_ELEMENTAL_ELEMENTS_H_
26#include "absl/base/attributes.h"
27#include "absl/log/check.h"
28#include "absl/strings/str_format.h"
29#include "absl/strings/string_view.h"
47 "variable",
"linear_constraint",
"auxiliary_objective",
48 "quadratic_constraint",
"indicator_constraint"};
52 "var",
"lin_con",
"aux_obj",
"quad_con",
"indic_con"};
57template <
typename Sink>
63template <ElementType element_type>
70 constexpr explicit ElementId(int64_t
id) : id_(id) {
71 CHECK_GE(
id, 0) <<
"negative " << element_type <<
" id: " << id;
74 constexpr bool IsValid()
const {
return id_ >= 0; }
77 int64_t
value()
const {
return id_; }
82 return lhs.id_ == rhs.id_;
86 return lhs.id_ != rhs.id_;
90 return lhs.id_ < rhs.id_;
94 return lhs.id_ <= rhs.id_;
98 return lhs.id_ > rhs.id_;
102 return lhs.id_ >= rhs.id_;
120 template <
typename H>
122 return H::combine(std::move(h), a.id_);
131template <ElementType element_type>
134 return absl::StrFormat(
"%s{%i}",
ToString(
id.type()),
id.value());
136 return absl::StrFormat(
"%s{invalid}",
ToString(
id.type()));
140template <ElementType element_type>
147template <
typename Sink, ElementType element_type>
155template <ElementType element_type,
typename Container>
161 const Container* container ABSL_ATTRIBUTE_LIFETIME_BOUND)
162 : container_(*container) {}
164 size_t size()
const {
return container_.size(); }
165 bool empty()
const {
return container_.empty(); }
171 auto begin()
const {
return Iterator(container_.begin()); }
172 auto end()
const {
return Iterator(container_.end()); }
175 class Iterator final {
177 using difference_type = std::ptrdiff_t;
180 Iterator() =
default;
181 explicit Iterator(
typename Container::const_iterator it) : it_(it) {}
185 Iterator& operator++() {
190 Iterator operator++(
int) {
196 friend bool operator==(
const Iterator& lhs,
const Iterator& rhs) {
197 return lhs.it_ == rhs.it_;
199 friend bool operator!=(
const Iterator& lhs,
const Iterator& rhs) {
200 return lhs.it_ != rhs.it_;
204 typename Container::const_iterator it_ = {};
206#if __cplusplus >= 202002L
207 static_assert(std::forward_iterator<Iterator>);
210 const Container& container_;
215template <ElementType element_type>
230 size_t size()
const {
return container_.size(); }
231 bool empty()
const {
return container_.empty(); }
248 Container container_;
251template <ElementType element_type>
258template <ElementType element_type>
A strongly typed element id. Value type.
friend bool operator>(const ElementId &lhs, const ElementId &rhs)
friend H AbslHashValue(H h, const ElementId &a)
constexpr bool IsValid() const
static constexpr ElementType type()
friend bool operator<(const ElementId &lhs, const ElementId &rhs)
friend bool operator<=(const ElementId &lhs, const ElementId &rhs)
constexpr ElementId()
Constructs an invalid element id.
int64_t value() const
Returns the raw id value.
friend bool operator==(const ElementId &lhs, const ElementId &rhs)
constexpr ElementId(int64_t id)
Constructs a valid element id. DCHECKs that id is non-negative.
friend bool operator>=(const ElementId &lhs, const ElementId &rhs)
friend bool operator!=(const ElementId &lhs, const ElementId &rhs)
ElementId< element_type > operator[](size_t i) const
ElementIdsConstView(const Container *container ABSL_ATTRIBUTE_LIFETIME_BOUND)
ElementId< element_type > value_type
ElementIdsVector(Container &&container)
Container & container()
Provides access to the untyped container.
ElementIdsVector & operator=(const ElementIdsVector &container)=delete
ElementIdsVector(const ElementIdsVector &container)=delete
ElementId< element_type > operator[](size_t i) const
typename View::value_type value_type
std::vector< int64_t > Container
ElementIdsConstView< element_type, Container > View
ElementIdsVector & operator=(ElementIdsVector &&container)=delete
ElementIdsVector(ElementIdsVector &&) noexcept=default
This is move-constructible only.
constexpr std::array< std::remove_cv_t< T >, N > to_array(T(&ts)[N])
An object oriented wrapper for quadratic constraints in ModelStorage.
When the underlying default is used When the feature cannot be turned kOff will return an error If the feature is enabled by default
ElementId< ElementType::kAuxiliaryObjective > AuxiliaryObjectiveId
static constexpr bool is_element_id_v
constexpr absl::string_view kShortElementNames[kNumElements]
ElementId< ElementType::kVariable > VariableId
constexpr int kNumElements
typename ElementIdsVector< element_type >::View ElementIdsSpan
ElementId< ElementType::kQuadraticConstraint > QuadraticConstraintId
void AbslStringify(Sink &sink, const AttrT attr_type)
std::ostream & operator<<(std::ostream &ostr, const SecondOrderConeConstraint &constraint)
ElementId< ElementType::kLinearConstraint > LinearConstraintId
absl::string_view ToString(const AttrT attr)
constexpr absl::string_view kElementNames[kNumElements]
ElementId< ElementType::kIndicatorConstraint > IndicatorConstraintId
LinearExpr operator*(LinearExpr lhs, double rhs)
Traits to detect whether T is an ElementId.