18 #ifndef OR_TOOLS_MATH_OPT_CPP_ID_MAP_H_
19 #define OR_TOOLS_MATH_OPT_CPP_ID_MAP_H_
22 #include <initializer_list>
27 #include "absl/container/flat_hash_map.h"
28 #include "absl/container/flat_hash_set.h"
29 #include "absl/types/span.h"
30 #include "absl/log/check.h"
56 template <
typename K,
typename V>
87 return lhs.storage_iterator_ == rhs.storage_iterator_;
90 return lhs.storage_iterator_ != rhs.storage_iterator_;
97 typename StorageType::iterator storage_iterator);
99 const IdMap* map_ =
nullptr;
100 typename StorageType::iterator storage_iterator_;
121 return lhs.storage_iterator_ == rhs.storage_iterator_;
125 return lhs.storage_iterator_ != rhs.storage_iterator_;
133 typename StorageType::const_iterator storage_iterator);
135 const IdMap* map_ =
nullptr;
136 typename StorageType::const_iterator storage_iterator_;
140 template <
typename InputIt>
141 inline IdMap(InputIt first, InputIt last);
142 inline IdMap(std::initializer_list<value_type> ilist);
148 inline const_iterator
begin()
const;
151 inline const_iterator
cend()
const;
152 inline const_iterator
end()
const;
155 bool empty()
const {
return map_.empty(); }
160 inline std::pair<iterator, bool>
insert(std::pair<K, V> k_v);
161 template <
typename InputIt>
162 inline void insert(InputIt first, InputIt last);
163 inline void insert(std::initializer_list<value_type> ilist);
165 template <
typename M>
168 inline std::pair<iterator, bool>
emplace(
const K& k, V v);
169 template <
typename... Args>
170 inline std::pair<iterator, bool>
try_emplace(
const K& k, Args&&... args);
182 inline void erase(const_iterator pos);
183 inline iterator
erase(const_iterator first, const_iterator last);
187 inline const V&
at(
const K& k)
const;
188 inline V&
at(
const K& k);
192 inline iterator
find(
const K& k);
193 inline const_iterator
find(
const K& k)
const;
225 inline std::vector<V>
Values(absl::Span<const K> keys)
const;
227 const absl::flat_hash_set<K>& keys)
const;
238 return lhs.storage_ == rhs.storage_ && lhs.map_ == rhs.map_;
241 return !(lhs == rhs);
245 inline std::vector<IdType> SortedIds()
const;
249 inline void CheckModel(
const K& k)
const;
253 inline void CheckOrSetModel(
const K& k);
257 inline void CheckOrSetModel(
const IdMap& other);
268 template <
typename K,
typename V>
281 template <
typename K,
typename V>
283 return reference(K(map_->storage_, storage_iterator_->first),
284 storage_iterator_->second);
287 template <
typename K,
typename V>
293 template <
typename K,
typename V>
299 template <
typename K,
typename V>
306 template <
typename K,
typename V>
308 typename StorageType::iterator storage_iterator)
309 : map_(map), storage_iterator_(std::move(storage_iterator)) {}
315 template <
typename K,
typename V>
317 : map_(non_const_iterator.map_),
318 storage_iterator_(non_const_iterator.storage_iterator_) {}
320 template <
typename K,
typename V>
323 return reference(K(map_->storage_, storage_iterator_->first),
324 storage_iterator_->second);
327 template <
typename K,
typename V>
333 template <
typename K,
typename V>
340 template <
typename K,
typename V>
348 template <
typename K,
typename V>
350 const IdMap* map,
typename StorageType::const_iterator storage_iterator)
351 : map_(map), storage_iterator_(std::move(storage_iterator)) {}
357 template <
typename K,
typename V>
359 : storage_(values.
empty() ? nullptr :
storage), map_(std::move(values)) {
361 CHECK(storage_ !=
nullptr);
365 template <
typename K,
typename V>
366 template <
typename InputIt>
371 template <
typename K,
typename V>
376 template <
typename K,
typename V>
381 template <
typename K,
typename V>
386 template <
typename K,
typename V>
388 return iterator(
this, map_.begin());
391 template <
typename K,
typename V>
396 template <
typename K,
typename V>
401 template <
typename K,
typename V>
406 template <
typename K,
typename V>
412 template <
typename K,
typename V>
414 std::pair<K, V> k_v) {
415 return emplace(k_v.first, std::move(k_v.second));
418 template <
typename K,
typename V>
419 template <
typename InputIt>
421 for (InputIt it = first; it != last; ++it) {
426 template <
typename K,
typename V>
428 insert(ilist.begin(), ilist.end());
431 template <
typename K,
typename V>
432 template <
typename M>
436 auto initial_ret = map_.insert_or_assign(k.typed_id(), std::forward<M>(v));
437 return std::make_pair(
iterator(
this, std::move(initial_ret.first)),
441 template <
typename K,
typename V>
445 auto initial_ret = map_.emplace(k.typed_id(), std::move(v));
446 return std::make_pair(
iterator(
this, std::move(initial_ret.first)),
450 template <
typename K,
typename V>
451 template <
typename... Args>
453 const K& k, Args&&... args) {
456 map_.try_emplace(k.typed_id(), std::forward<Args>(args)...);
457 return std::make_pair(
iterator(
this, std::move(initial_ret.first)),
461 template <
typename K,
typename V>
464 const size_type ret = map_.erase(k.typed_id());
471 template <
typename K,
typename V>
473 map_.erase(pos.storage_iterator_);
479 template <
typename K,
typename V>
482 auto ret = map_.erase(first.storage_iterator_, last.storage_iterator_);
486 return iterator(
this, std::move(ret));
489 template <
typename K,
typename V>
492 swap(storage_, other.storage_);
493 swap(map_, other.map_);
496 template <
typename K,
typename V>
499 return map_.at(k.typed_id());
502 template <
typename K,
typename V>
505 return map_.at(k.typed_id());
508 template <
typename K,
typename V>
511 return map_[k.typed_id()];
514 template <
typename K,
typename V>
517 return map_.count(k.typed_id());
520 template <
typename K,
typename V>
523 return map_.contains(k.typed_id());
526 template <
typename K,
typename V>
529 return iterator(
this, map_.find(k.typed_id()));
532 template <
typename K,
typename V>
538 template <
typename K,
typename V>
541 const auto it = find(k);
548 template <
typename K,
typename V>
549 std::pair<typename IdMap<K, V>::const_iterator,
552 const auto it = find(k);
559 template <
typename K,
typename V>
561 CheckOrSetModel(other);
562 for (
const auto& pair : other.map_) {
563 map_[pair.first] += pair.second;
567 template <
typename K,
typename V>
569 CheckOrSetModel(other);
570 for (
const auto& pair : other.map_) {
571 map_[pair.first] -= pair.second;
575 template <
typename K,
typename V>
577 std::vector<V> result;
578 result.reserve(keys.size());
579 for (
const K key : keys) {
580 result.push_back(at(key));
585 template <
typename K,
typename V>
587 const absl::flat_hash_set<K>& keys)
const {
588 absl::flat_hash_map<K, V> result;
589 for (
const K key : keys) {
590 result[key] = at(key);
595 template <
typename K,
typename V>
597 std::vector<K> result;
598 result.reserve(map_.size());
599 for (
const IdType id : SortedIds()) {
600 result.push_back(K(storage_,
id));
605 template <
typename K,
typename V>
607 std::vector<V> result;
608 result.reserve(map_.size());
609 for (
const IdType id : SortedIds()) {
610 result.push_back(map_.at(
id));
615 template <
typename K,
typename V>
617 std::vector<IdType> result;
619 for (
const auto& [
id, _] : map_) {
620 result.push_back(
id);
622 std::sort(result.begin(), result.end());
626 template <
typename K,
typename V>
627 void IdMap<K, V>::CheckModel(
const K& k)
const {
629 CHECK(storage_ ==
nullptr || storage_ == k.storage())
633 template <
typename K,
typename V>
634 void IdMap<K, V>::CheckOrSetModel(
const K& k) {
636 if (storage_ ==
nullptr) {
637 storage_ = k.storage();
643 template <
typename K,
typename V>
644 void IdMap<K, V>::CheckOrSetModel(
const IdMap& other) {
645 if (storage_ ==
nullptr) {
646 storage_ = other.storage_;
647 }
else if (other.storage_ !=
nullptr) {
648 CHECK_EQ(storage_, other.storage_)
652 DCHECK(other.empty());
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)
IdMap(const ModelStorage *storage, StorageType values)
size_type erase(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
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)
std::pair< iterator, bool > insert_or_assign(const K &k, M &&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)
const ModelStorage * storage() const
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 kKeyHasNullModelStorage
constexpr absl::string_view kObjectsFromOtherModelStorage
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Collection of objects used to extend the Constraint Solver library.
std::optional< int64_t > end