36#ifndef OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
37#define OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
42#include "absl/algorithm/container.h"
43#include "absl/container/flat_hash_map.h"
44#include "absl/status/status.h"
45#include "absl/strings/string_view.h"
46#include "absl/types/span.h"
54class LinearConstraint;
55class QuadraticConstraint;
56class SecondOrderConeConstraint;
59class IndicatorConstraint;
60class QuadraticTermKey;
73 (std::is_same_v<T, Variable> || std::is_same_v<T, LinearConstraint> ||
74 std::is_same_v<T, QuadraticConstraint> ||
75 std::is_same_v<T, SecondOrderConeConstraint> ||
76 std::is_same_v<T, Sos1Constraint> || std::is_same_v<T, Sos2Constraint> ||
77 std::is_same_v<T, IndicatorConstraint> ||
78 std::is_same_v<T, QuadraticTermKey> || std::is_same_v<T, Objective>);
85template <
typename Map,
86 typename = std::enable_if_t<is_key_type_v<typename Map::key_type>>>
87std::vector<typename Map::key_type>
SortedKeys(
const Map& map) {
88 using K =
typename Map::key_type;
89 std::vector<K> result;
90 result.reserve(map.size());
91 for (
const typename Map::const_reference item : map) {
92 result.push_back(item.first);
94 absl::c_sort(result, [](
const K& lhs,
const K& rhs) {
95 if (lhs.storage() != rhs.storage()) {
96 return lhs.storage() < rhs.storage();
98 return lhs.typed_id() < rhs.typed_id();
108template <
typename Set,
109 typename = std::enable_if_t<is_key_type_v<typename Set::key_type>>>
111 using K =
typename Set::key_type;
112 std::vector<K> result;
113 result.reserve(set.size());
114 for (
const typename Set::const_reference item : set) {
115 result.push_back(item);
117 absl::c_sort(result, [](
const K& lhs,
const K& rhs) {
118 if (lhs.storage() != rhs.storage()) {
119 return lhs.storage() < rhs.storage();
121 return lhs.typed_id() < rhs.typed_id();
134template <
typename Map,
typename Keys,
135 typename = std::enable_if_t<is_key_type_v<typename Map::key_type>>>
136std::vector<typename Map::mapped_type>
Values(
const Map& map,
138 using K =
typename Map::key_type;
139 const absl::Span<const K> keys_span = keys;
140 std::vector<typename Map::mapped_type> result;
141 result.reserve(keys_span.size());
142 for (
const K& key : keys_span) {
143 result.push_back(map.at(key));
152 "The input key has null .storage().";
157 "The input objects belongs to another model.";
162 "the input does not belong to the same model";
169 const ModelStorage*
const storage,
170 const ModelStorage*
const expected_storage) {
171 if (expected_storage ==
nullptr) {
172 return absl::InternalError(
"expected_storage is nullptr");
174 if (storage !=
nullptr && storage != expected_storage) {
177 return absl::OkStatus();
constexpr absl::string_view kKeyHasNullModelStorage
The CHECK message to use when a KeyType::storage() is nullptr.
constexpr absl::string_view kInputFromInvalidModelStorage
constexpr absl::string_view kObjectsFromOtherModelStorage
absl::Status CheckModelStorage(const ModelStorage *const storage, const ModelStorage *const expected_storage)
An object oriented wrapper for quadratic constraints in ModelStorage.
std::vector< typename Set::key_type > SortedElements(const Set &set)
std::vector< typename Map::mapped_type > Values(const Map &map, const Keys &keys)
constexpr bool is_key_type_v
std::vector< typename Map::key_type > SortedKeys(const Map &map)