Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
map_util.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_MAP_UTIL_H_
15#define OR_TOOLS_BASE_MAP_UTIL_H_
16
17#include <utility>
18
20
21namespace gtl {
22template <typename M>
23using MapUtilValueT = typename M::value_type;
24template <typename M>
26template <typename M>
28
29// Perform a lookup in a std::map or std::unordered_map.
30// If the key is present in the map then the value associated with that
31// key is returned, otherwise the value passed as a default is returned.
32//
33// Prefer the two-argument form unless you need to specify a custom default
34// value (i.e., one that is not equal to a value-initialized instance).
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()) {
41 return value;
42 }
43 return it->second;
44}
45
46// Returns a const reference to the value associated with the given key if it
47// exists, otherwise returns a const reference to a value-initialized object
48// that is never destroyed.
49template <class Collection, typename KeyType = MapUtilKeyT<Collection>>
50const MapUtilMappedT<Collection>& FindWithDefault(const Collection& collection,
51 const KeyType& key) {
52 static const MapUtilMappedT<Collection>* const default_value =
54 typename Collection::const_iterator it = collection.find(key);
55 if (it == collection.end()) {
56 return *default_value;
57 }
58 return it->second;
59}
60
61// Perform a lookup in a std::map or std::unordered_map.
62// If the key is present a const pointer to the associated value is returned,
63// otherwise a NULL pointer is returned.
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()) {
70 return 0;
71 }
72 return &it->second;
73}
74
75// Perform a lookup in a std::map or std::unordered_map.
76// Same as above but the returned pointer is not const and can be used to change
77// the stored value.
78template <class Collection>
79typename Collection::value_type::second_type* FindOrNull(
80 Collection& collection, // NOLINT
81 const typename Collection::value_type::first_type& key) {
82 typename Collection::iterator it = collection.find(key);
83 if (it == collection.end()) {
84 return 0;
85 }
86 return &it->second;
87}
88
89// Perform a lookup in a std::map or std::unordered_map whose values are
90// pointers. If the key is present a const pointer to the associated value is
91// returned, otherwise a NULL pointer is returned. This function does not
92// distinguish between a missing key and a key mapped to a NULL value.
93template <class Collection>
94const typename Collection::value_type::second_type FindPtrOrNull(
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()) {
99 return 0;
100 }
101 return it->second;
102}
103
104// Change the value associated with a particular key in a std::map or
105// std::unordered_map. If the key is not present in the map the key and value
106// are inserted, otherwise the value is updated to be a copy of the value
107// provided. True indicates that an insert took place, false indicates an
108// update.
109template <class Collection, class Key, class Value>
110bool InsertOrUpdate(Collection* const collection, const Key& key,
111 const Value& value) {
112 std::pair<typename Collection::iterator, bool> ret =
113 collection->insert(typename Collection::value_type(key, value));
114 if (!ret.second) {
115 // update
116 ret.first->second = value;
117 return false;
118 }
119 return true;
120}
121
122// Insert a new key into a set.
123// If the key is not present in the set the key is
124// inserted, otherwise nothing happens. True indicates that an insert
125// took place, false indicates the key was already present.
126template <class Collection>
127bool InsertIfNotPresent(Collection* const collection,
128 const typename Collection::value_type& value) {
129 std::pair<typename Collection::iterator, bool> ret =
130 collection->insert(value);
131 return ret.second;
132}
133
134// Insert a new key and value into a std::map or std::unordered_map.
135// If the key is not present in the map the key and value are
136// inserted, otherwise nothing happens. True indicates that an insert
137// took place, false indicates the key was already present.
138template <class Collection, class Key, class Value>
139bool InsertIfNotPresent(Collection* const collection, const Key& key,
140 const Value& value) {
141 std::pair<typename Collection::iterator, bool> ret =
142 collection->insert(typename Collection::value_type(key, value));
143 return ret.second;
144}
145
146// Inserts a new std::pair<key,value> into a std::map or std::unordered_map.
147// Insert a new key into a std::set or std::unordered_set.
148// Dies if the key is already present.
149template <class Collection>
150void InsertOrDieNoPrint(Collection* const collection,
151 const typename Collection::value_type& value) {
152 CHECK(collection->insert(value).second);
153}
154
155// Inserts a new std::pair<key,value> into a std::map or std::unordered_map.
156// Insert a new key into a std::set or std::unordered_set.
157// Dies if the key is already present.
158template <class Collection>
159void InsertOrDie(Collection* const collection,
160 const typename Collection::value_type& value) {
161 CHECK(collection->insert(value).second) << "duplicate value: " << value;
162}
163
164// Inserts a new key/value into a std::map or std::unordered_map.
165// Dies if the key is already present.
166template <class Collection>
167void InsertOrDie(Collection* const 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;
173}
174
175// Inserts a key into a map with the default value or dies. Returns a reference
176// to the inserted element.
177template <typename Collection>
178auto& InsertKeyOrDie(Collection* const 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;
183 return it->second;
184}
185
186// Perform a lookup in std::map or std::unordered_map.
187// If the key is present and value is non-NULL then a copy of the value
188// associated with the key is made into *value. Returns whether key was present.
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()) {
194 return false;
195 }
196 if (value) {
197 *value = it->second;
198 }
199 return true;
200}
201
202// Test to see if a std::set, std::map, std::unordered_set or std::unordered_map
203// contains a particular key. Returns true if the key is in the collection.
204template <class Collection, class Key>
205bool ContainsKey(const Collection& collection, const Key& key) {
206 typename Collection::const_iterator it = collection.find(key);
207 return it != collection.end();
208}
209
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;
216 return it->second;
217}
218
219// Same as FindOrDie above, but doesn't log the key on failure.
220template <class Collection>
221const typename Collection::value_type::second_type& FindOrDieNoPrint(
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";
226 return it->second;
227}
228
229// Same as above, but returns a non-const reference.
230template <class Collection>
231typename Collection::value_type::second_type& FindOrDieNoPrint(
232 Collection& collection, // NOLINT
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";
236 return it->second;
237}
238
239// Lookup a key in a std::map or std::unordered_map, insert it if it is not
240// present. Returns a reference to the value associated with the key.
241template <class Collection>
242typename Collection::value_type::second_type& LookupOrInsert(
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;
249}
250} // namespace gtl
251#endif // OR_TOOLS_BASE_MAP_UTIL_H_
int64_t value
typename MapUtilValueT< M >::second_type MapUtilMappedT
Definition map_util.h:27
const Collection::value_type::second_type FindPtrOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:94
bool InsertOrUpdate(Collection *const collection, const Key &key, const Value &value)
Definition map_util.h:110
void InsertOrDieNoPrint(Collection *const collection, const typename Collection::value_type &value)
Definition map_util.h:150
typename MapUtilValueT< M >::first_type MapUtilKeyT
Definition map_util.h:25
bool InsertIfNotPresent(Collection *const collection, const typename Collection::value_type &value)
Definition map_util.h:127
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)
Definition map_util.h:242
typename M::value_type MapUtilValueT
Definition map_util.h:23
void InsertOrDie(Collection *const collection, const typename Collection::value_type &value)
Definition map_util.h:159
bool FindCopy(const Collection &collection, const Key &key, Value *const value)
Definition map_util.h:190
const Collection::value_type::second_type & FindOrDie(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:211
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.
Definition map_util.h:221
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:65
bool ContainsKey(const Collection &collection, const Key &key)
Definition map_util.h:205
auto & InsertKeyOrDie(Collection *const collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:178
const MapUtilMappedT< Collection > & FindWithDefault(const Collection &collection, const KeyType &key, const MapUtilMappedT< Collection > &value)
Definition map_util.h:36