14#ifndef OR_TOOLS_BASE_MAP_UTIL_H_
15#define OR_TOOLS_BASE_MAP_UTIL_H_
35template <
typename Collection,
typename KeyType = MapUtilKeyT<Collection>>
37 const Collection& collection,
const KeyType& key,
39 typename Collection::const_iterator it = collection.find(key);
40 if (it == collection.end()) {
49template <
class Collection,
typename KeyType = MapUtilKeyT<Collection>>
54 typename Collection::const_iterator it = collection.find(key);
55 if (it == collection.end()) {
56 return *default_value;
64template <
class Collection>
65const typename Collection::value_type::second_type*
FindOrNull(
66 const Collection& collection,
67 const typename Collection::value_type::first_type& key) {
68 typename Collection::const_iterator it = collection.find(key);
69 if (it == collection.end()) {
78template <
class Collection>
80 Collection& collection,
81 const typename Collection::value_type::first_type& key) {
82 typename Collection::iterator it = collection.find(key);
83 if (it == collection.end()) {
93template <
class Collection>
95 const Collection& collection,
96 const typename Collection::value_type::first_type& key) {
97 typename Collection::const_iterator it = collection.find(key);
98 if (it == collection.end()) {
109template <
class Collection,
class Key,
class Value>
111 const Value&
value) {
112 std::pair<typename Collection::iterator, bool> ret =
113 collection->insert(
typename Collection::value_type(key,
value));
116 ret.first->second =
value;
126template <
class Collection>
128 const typename Collection::value_type&
value) {
129 std::pair<typename Collection::iterator, bool> ret =
130 collection->insert(
value);
138template <
class Collection,
class Key,
class Value>
140 const Value&
value) {
141 std::pair<typename Collection::iterator, bool> ret =
142 collection->insert(
typename Collection::value_type(key,
value));
149template <
class Collection>
151 const typename Collection::value_type&
value) {
152 CHECK(collection->insert(
value).second);
158template <
class Collection>
160 const typename Collection::value_type&
value) {
161 CHECK(collection->insert(
value).second) <<
"duplicate value: " <<
value;
166template <
class Collection>
168 const typename Collection::value_type::first_type& key,
169 const typename Collection::value_type::second_type& data) {
170 typedef typename Collection::value_type value_type;
171 CHECK(collection->insert(value_type(key, data)).second)
172 <<
"duplicate key: " << key;
177template <
typename Collection>
179 const typename Collection::value_type::first_type& key) {
180 auto [it, did_insert] = collection->insert(
typename Collection::value_type(
181 key,
typename Collection::value_type::second_type()));
182 CHECK(did_insert) <<
"duplicate key " << key;
189template <
class Collection,
class Key,
class Value>
190bool FindCopy(
const Collection& collection,
const Key& key,
191 Value*
const value) {
192 typename Collection::const_iterator it = collection.find(key);
193 if (it == collection.end()) {
204template <
class Collection,
class Key>
206 typename Collection::const_iterator it = collection.find(key);
207 return it != collection.end();
210template <
class Collection>
211const typename Collection::value_type::second_type&
FindOrDie(
212 const Collection& collection,
213 const typename Collection::value_type::first_type& key) {
214 typename Collection::const_iterator it = collection.find(key);
215 CHECK(it != collection.end()) <<
"Map key not found: " << key;
220template <
class Collection>
222 const Collection& collection,
223 const typename Collection::value_type::first_type& key) {
224 typename Collection::const_iterator it = collection.find(key);
225 CHECK(it != collection.end()) <<
"Map key not found";
230template <
class Collection>
232 Collection& collection,
233 const typename Collection::value_type::first_type& key) {
234 typename Collection::iterator it = collection.find(key);
235 CHECK(it != collection.end()) <<
"Map key not found";
241template <
class Collection>
243 Collection*
const collection,
244 const typename Collection::value_type::first_type& key,
245 const typename Collection::value_type::second_type&
value) {
246 std::pair<typename Collection::iterator, bool> ret =
247 collection->insert(
typename Collection::value_type(key,
value));
248 return ret.first->second;
typename MapUtilValueT< M >::second_type MapUtilMappedT
const Collection::value_type::second_type FindPtrOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
bool InsertOrUpdate(Collection *const collection, const Key &key, const Value &value)
void InsertOrDieNoPrint(Collection *const collection, const typename Collection::value_type &value)
typename MapUtilValueT< M >::first_type MapUtilKeyT
bool InsertIfNotPresent(Collection *const collection, const typename Collection::value_type &value)
Collection::value_type::second_type & LookupOrInsert(Collection *const collection, const typename Collection::value_type::first_type &key, const typename Collection::value_type::second_type &value)
typename M::value_type MapUtilValueT
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
bool FindCopy(const Collection &collection, const Key &key, Value *const value)
const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)
const Collection::value_type::second_type & FindOrDieNoPrint(const Collection &collection, const typename Collection::value_type::first_type &key)
Same as FindOrDie above, but doesn't log the key on failure.
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
bool ContainsKey(const Collection &collection, const Key &key)
auto & InsertKeyOrDie(Collection *const collection, const typename Collection::value_type::first_type &key)
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)