15 #ifndef OR_TOOLS_MATH_OPT_CPP_ID_MAP_H_
16 #define OR_TOOLS_MATH_OPT_CPP_ID_MAP_H_
19 #include <initializer_list>
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/container/flat_hash_set.h"
27 #include "absl/types/span.h"
53 template <
typename K,
typename V>
84 return lhs.storage_iterator_ == rhs.storage_iterator_;
87 return lhs.storage_iterator_ != rhs.storage_iterator_;
94 typename StorageType::iterator storage_iterator);
96 const IdMap* map_ =
nullptr;
97 typename StorageType::iterator storage_iterator_;
118 return lhs.storage_iterator_ == rhs.storage_iterator_;
122 return lhs.storage_iterator_ != rhs.storage_iterator_;
130 typename StorageType::const_iterator storage_iterator);
132 const IdMap* map_ =
nullptr;
133 typename StorageType::const_iterator storage_iterator_;
137 template <
typename InputIt>
138 inline IdMap(InputIt first, InputIt last);
139 inline IdMap(std::initializer_list<value_type> ilist);
145 inline const_iterator
begin()
const;
148 inline const_iterator
cend()
const;
149 inline const_iterator
end()
const;
152 bool empty()
const {
return map_.empty(); }
157 inline std::pair<iterator, bool>
insert(std::pair<K, V> k_v);
158 template <
typename InputIt>
159 inline void insert(InputIt first, InputIt last);
160 inline void insert(std::initializer_list<value_type> ilist);
162 inline std::pair<iterator, bool>
emplace(
const K& k, V v);
163 template <
typename... Args>
164 inline std::pair<iterator, bool>
try_emplace(
const K& k, Args&&... args);
176 inline void erase(const_iterator pos);
177 inline iterator
erase(const_iterator first, const_iterator last);
181 inline const V&
at(
const K& k)
const;
182 inline V&
at(
const K& k);
186 inline iterator
find(
const K& k);
187 inline const_iterator
find(
const K& k)
const;
219 inline std::vector<V>
Values(absl::Span<const K> keys)
const;
221 const absl::flat_hash_set<K>& keys)
const;
232 return lhs.model_ == rhs.model_ && lhs.map_ == rhs.map_;
235 return !(lhs == rhs);
239 inline std::vector<IdType> SortedIds()
const;
243 inline void CheckModel(
const K& k)
const;
247 inline void CheckOrSetModel(
const K& k);
251 inline void CheckOrSetModel(
const IdMap& other);
262 template <
typename K,
typename V>
275 template <
typename K,
typename V>
277 return reference(K(map_->model_, storage_iterator_->first),
278 storage_iterator_->second);
281 template <
typename K,
typename V>
287 template <
typename K,
typename V>
293 template <
typename K,
typename V>
300 template <
typename K,
typename V>
302 typename StorageType::iterator storage_iterator)
303 : map_(map), storage_iterator_(std::move(storage_iterator)) {}
309 template <
typename K,
typename V>
311 : map_(non_const_iterator.map_),
312 storage_iterator_(non_const_iterator.storage_iterator_) {}
314 template <
typename K,
typename V>
317 return reference(K(map_->model_, storage_iterator_->first),
318 storage_iterator_->second);
321 template <
typename K,
typename V>
327 template <
typename K,
typename V>
334 template <
typename K,
typename V>
342 template <
typename K,
typename V>
344 const IdMap* map,
typename StorageType::const_iterator storage_iterator)
345 : map_(map), storage_iterator_(std::move(storage_iterator)) {}
351 template <
typename K,
typename V>
353 : model_(
model), map_(std::move(values)) {
355 CHECK(model_ !=
nullptr);
359 template <
typename K,
typename V>
360 template <
typename InputIt>
365 template <
typename K,
typename V>
370 template <
typename K,
typename V>
375 template <
typename K,
typename V>
380 template <
typename K,
typename V>
382 return iterator(
this, map_.begin());
385 template <
typename K,
typename V>
390 template <
typename K,
typename V>
395 template <
typename K,
typename V>
400 template <
typename K,
typename V>
406 template <
typename K,
typename V>
408 std::pair<K, V> k_v) {
409 return emplace(k_v.first, std::move(k_v.second));
412 template <
typename K,
typename V>
413 template <
typename InputIt>
415 for (InputIt it = first; it != last; ++it) {
420 template <
typename K,
typename V>
422 insert(ilist.begin(), ilist.end());
425 template <
typename K,
typename V>
429 auto initial_ret = map_.emplace(k.typed_id(), std::move(v));
430 return std::make_pair(
iterator(
this, std::move(initial_ret.first)),
434 template <
typename K,
typename V>
435 template <
typename... Args>
437 const K& k, Args&&... args) {
440 map_.try_emplace(k.typed_id(), std::forward<Args>(args)...);
441 return std::make_pair(
iterator(
this, std::move(initial_ret.first)),
445 template <
typename K,
typename V>
448 const int ret = map_.erase(k.typed_id());
455 template <
typename K,
typename V>
457 map_.erase(pos.storage_iterator_);
463 template <
typename K,
typename V>
466 auto ret = map_.erase(first.storage_iterator_, last.storage_iterator_);
470 return iterator(
this, std::move(ret));
473 template <
typename K,
typename V>
476 swap(model_, other.model_);
477 swap(map_, other.map_);
480 template <
typename K,
typename V>
483 return map_.at(k.typed_id());
486 template <
typename K,
typename V>
489 return map_.at(k.typed_id());
492 template <
typename K,
typename V>
495 return map_[k.typed_id()];
498 template <
typename K,
typename V>
501 return map_.count(k.typed_id());
504 template <
typename K,
typename V>
507 return map_.contains(k.typed_id());
510 template <
typename K,
typename V>
513 return iterator(
this, map_.find(k.typed_id()));
516 template <
typename K,
typename V>
522 template <
typename K,
typename V>
525 const auto it = find(k);
532 template <
typename K,
typename V>
533 std::pair<typename IdMap<K, V>::const_iterator,
536 const auto it = find(k);
543 template <
typename K,
typename V>
545 CheckOrSetModel(other);
546 for (
const auto& pair : other.map_) {
547 map_[pair.first] += pair.second;
551 template <
typename K,
typename V>
553 CheckOrSetModel(other);
554 for (
const auto& pair : other.map_) {
555 map_[pair.first] -= pair.second;
559 template <
typename K,
typename V>
561 std::vector<V> result;
562 result.reserve(keys.size());
563 for (
const K key : keys) {
564 result.push_back(at(key));
569 template <
typename K,
typename V>
571 const absl::flat_hash_set<K>& keys)
const {
572 absl::flat_hash_map<K, V> result;
573 for (
const K key : keys) {
574 result[key] = at(key);
579 template <
typename K,
typename V>
581 std::vector<K> result;
582 result.reserve(map_.size());
583 for (
const IdType id : SortedIds()) {
584 result.push_back(K(model_,
id));
589 template <
typename K,
typename V>
591 std::vector<V> result;
592 result.reserve(map_.size());
593 for (
const IdType id : SortedIds()) {
594 result.push_back(map_.at(
id));
599 template <
typename K,
typename V>
601 std::vector<IdType> result;
603 for (
const auto& [
id, _] : map_) {
604 result.push_back(
id);
606 std::sort(result.begin(), result.end());
610 template <
typename K,
typename V>
611 void IdMap<K, V>::CheckModel(
const K& k)
const {
613 CHECK(model_ ==
nullptr || model_ == k.model())
617 template <
typename K,
typename V>
618 void IdMap<K, V>::CheckOrSetModel(
const K& k) {
620 if (model_ ==
nullptr) {
627 template <
typename K,
typename V>
628 void IdMap<K, V>::CheckOrSetModel(
const IdMap& other) {
629 if (model_ ==
nullptr) {
630 model_ = other.model_;
631 }
else if (other.model_ !=
nullptr) {
#define CHECK_EQ(val1, val2)
#define DCHECK(condition)
friend bool operator!=(const const_iterator &lhs, const const_iterator &rhs)
IdMap::const_pointer pointer
IdMap::difference_type difference_type
internal::ArrowOperatorProxy< reference > operator->() const
std::forward_iterator_tag iterator_category
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs)
const_iterator & operator++()
IdMap::const_reference reference
IdMap::value_type value_type
reference operator*() const
friend bool operator==(const iterator &lhs, const iterator &rhs)
IdMap::difference_type difference_type
std::forward_iterator_tag iterator_category
IdMap::value_type value_type
friend bool operator!=(const iterator &lhs, const iterator &rhs)
internal::ArrowOperatorProxy< reference > operator->() const
reference operator*() const
IdMap::reference reference
void Subtract(const IdMap &other)
const StorageType & raw_map() const
IdMap(InputIt first, InputIt last)
void reserve(size_type count)
const_iterator begin() const
std::pair< const K, V > value_type
V & operator[](const K &k)
void insert(InputIt first, InputIt last)
std::vector< K > SortedKeys() const
std::vector< V > SortedValues() const
absl::flat_hash_map< IdType, V > StorageType
std::pair< iterator, bool > emplace(const K &k, V v)
std::pair< const K, const V & > const_reference
IndexedModel * model() const
std::pair< iterator, bool > try_emplace(const K &k, Args &&... args)
std::vector< V > Values(absl::Span< const K > keys) const
friend bool operator==(const IdMap &lhs, const IdMap &rhs)
bool contains(const K &k) const
iterator erase(const_iterator first, const_iterator last)
typename StorageType::size_type size_type
const_iterator end() const
std::pair< iterator, bool > insert(std::pair< K, V > k_v)
IdMap(std::initializer_list< value_type > ilist)
std::pair< const_iterator, const_iterator > equal_range(const K &k) const
typename K::IdType IdType
absl::flat_hash_map< K, V > Values(const absl::flat_hash_set< K > &keys) const
const_iterator cend() const
size_type count(const K &k) const
const V & at(const K &k) const
const_iterator cbegin() const
friend bool operator!=(const IdMap &lhs, const IdMap &rhs)
IdMap(IndexedModel *model, StorageType values)
void insert(std::initializer_list< value_type > ilist)
const_iterator find(const K &k) const
typename StorageType::difference_type difference_type
void erase(const_iterator pos)
std::pair< iterator, iterator > equal_range(const K &k)
void Add(const IdMap &other)
iterator find(const K &k)
std::pair< const K, V & > reference
constexpr absl::string_view kObjectsFromOtherIndexedModel
constexpr absl::string_view kKeyHasNullIndexedModel
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Collection of objects used to extend the Constraint Solver library.