2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_MAPUTIL_H_
18 #define FOLLY_MAPUTIL_H_
23 * Given a map and a key, return the value corresponding to the key in the map,
24 * or a given default value if the key doesn't exist in the map.
27 typename Map::mapped_type get_default(
28 const Map& map, const typename Map::key_type& key,
29 const typename Map::mapped_type& dflt =
30 typename Map::mapped_type()) {
31 auto pos = map.find(key);
32 return (pos != map.end() ? pos->second : dflt);
36 * Given a map and a key, return a reference to the value corresponding to the
37 * key in the map, or the given default reference if the key doesn't exist in
41 const typename Map::mapped_type& get_ref_default(
42 const Map& map, const typename Map::key_type& key,
43 const typename Map::mapped_type& dflt) {
44 auto pos = map.find(key);
45 return (pos != map.end() ? pos->second : dflt);
49 * Given a map and a key, return a pointer to the value corresponding to the
50 * key in the map, or nullptr if the key doesn't exist in the map.
53 const typename Map::mapped_type* get_ptr(
54 const Map& map, const typename Map::key_type& key) {
55 auto pos = map.find(key);
56 return (pos != map.end() ? &pos->second : nullptr);
60 * Non-const overload of the above.
63 typename Map::mapped_type* get_ptr(
64 Map& map, const typename Map::key_type& key) {
65 auto pos = map.find(key);
66 return (pos != map.end() ? &pos->second : nullptr);
71 #endif /* FOLLY_MAPUTIL_H_ */