38#ifndef OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
39#define OR_TOOLS_MATH_OPT_CPP_KEY_TYPES_H_
44#include "absl/algorithm/container.h"
45#include "absl/container/flat_hash_map.h"
46#include "absl/status/status.h"
47#include "absl/strings/string_view.h"
48#include "absl/types/span.h"
77 std::is_same_v<T, SecondOrderConeConstraint> ||
78 std::is_same_v<T, Sos1Constraint> || std::is_same_v<T, Sos2Constraint> ||
79 std::is_same_v<T, QuadraticTermKey> || std::is_same_v<T, Objective>);
86template <
typename Map,
87 typename = std::enable_if_t<is_key_type_v<typename Map::key_type>>>
88std::vector<typename Map::key_type>
SortedKeys(
const Map& map) {
89 using K =
typename Map::key_type;
90 std::vector<K> result;
91 result.reserve(map.size());
92 for (
const typename Map::const_reference item : map) {
93 result.push_back(item.first);
95 absl::c_sort(result, [](
const K& lhs,
const K& rhs) {
96 if (lhs.storage() != rhs.storage()) {
97 return lhs.storage() < rhs.storage();
99 return lhs.typed_id() < rhs.typed_id();
109template <
typename Set,
110 typename = std::enable_if_t<is_key_type_v<typename Set::key_type>>>
112 using K =
typename Set::key_type;
113 std::vector<K> result;
114 result.reserve(set.size());
115 for (
const typename Set::const_reference item : set) {
116 result.push_back(item);
118 absl::c_sort(result, [](
const K& lhs,
const K& rhs) {
119 if (lhs.storage() != rhs.storage()) {
120 return lhs.storage() < rhs.storage();
122 return lhs.typed_id() < rhs.typed_id();
135template <
typename Map,
typename Keys,
136 typename = std::enable_if_t<is_key_type_v<typename Map::key_type>>>
137std::vector<typename Map::mapped_type>
Values(
const Map& map,
139 using K =
typename Map::key_type;
140 const absl::Span<const K> keys_span = keys;
141 std::vector<typename Map::mapped_type> result;
142 result.reserve(keys_span.size());
143 for (
const K& key : keys_span) {
144 result.push_back(map.at(key));
153 "The input key has null .storage().";
158 "The input objects belongs to another model.";
163 "the input does not belong to the same model";
172 if (expected_storage ==
nullptr) {
173 return absl::InternalError(
"expected_storage is nullptr");
175 if (storage !=
nullptr && storage != expected_storage) {
178 return absl::OkStatus();
absl::Status CheckModelStorage(const NullableModelStorageCPtr storage, const ModelStorageCPtr expected_storage)
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
An object oriented wrapper for quadratic constraints in ModelStorage.
absl::Nonnull< const ModelStorage * > ModelStorageCPtr
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)
absl::Nullable< const ModelStorage * > NullableModelStorageCPtr
static constexpr bool value