X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FMapUtil.h;h=433489273785a6f2677c6f36563ea2a292d8fb58;hp=d6be57115c7d55d884abf49360f1a7372ec6145b;hb=5277a636b62a8f9c80756012de9bae80358ee30e;hpb=cc84c39c1c042b20af82b8f54d72a6bf65deb58e diff --git a/folly/MapUtil.h b/folly/MapUtil.h index d6be5711..43348927 100644 --- a/folly/MapUtil.h +++ b/folly/MapUtil.h @@ -18,6 +18,7 @@ #include #include +#include #include namespace folly { @@ -26,13 +27,21 @@ namespace folly { * Given a map and a key, return the value corresponding to the key in the map, * or a given default value if the key doesn't exist in the map. */ -template -typename Map::mapped_type get_default( - const Map& map, const typename Map::key_type& key, - const typename Map::mapped_type& dflt = - typename Map::mapped_type()) { +template +typename Map::mapped_type get_default(const Map& map, const Key& key) { auto pos = map.find(key); - return (pos != map.end() ? pos->second : dflt); + return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{}); +} +template < + class Map, + typename Key = typename Map::key_type, + typename Value = typename Map::mapped_type, + typename std::enable_if::value>::type* = nullptr> +typename Map::mapped_type +get_default(const Map& map, const Key& key, Value&& dflt) { + using M = typename Map::mapped_type; + auto pos = map.find(key); + return (pos != map.end()) ? (pos->second) : M(std::forward(dflt)); } /** @@ -41,12 +50,13 @@ typename Map::mapped_type get_default( */ template < class Map, + typename Key = typename Map::key_type, typename Func, typename = typename std::enable_if::type, typename Map::mapped_type>::value>::type> typename Map::mapped_type -get_default(const Map& map, const typename Map::key_type& key, Func&& dflt) { +get_default(const Map& map, const Key& key, Func&& dflt) { auto pos = map.find(key); return pos != map.end() ? pos->second : dflt(); } @@ -55,10 +65,13 @@ get_default(const Map& map, const typename Map::key_type& key, Func&& dflt) { * Given a map and a key, return the value corresponding to the key in the map, * or throw an exception of the specified type. */ -template +template < + class E = std::out_of_range, + class Map, + typename Key = typename Map::key_type> const typename Map::mapped_type& get_or_throw( const Map& map, - const typename Map::key_type& key, + const Key& key, const std::string& exceptionStrPrefix = std::string()) { auto pos = map.find(key); if (pos != map.end()) { @@ -67,10 +80,13 @@ const typename Map::mapped_type& get_or_throw( throw E(folly::to(exceptionStrPrefix, key)); } -template +template < + class E = std::out_of_range, + class Map, + typename Key = typename Map::key_type> typename Map::mapped_type& get_or_throw( Map& map, - const typename Map::key_type& key, + const Key& key, const std::string& exceptionStrPrefix = std::string()) { auto pos = map.find(key); if (pos != map.end()) { @@ -83,9 +99,10 @@ typename Map::mapped_type& get_or_throw( * Given a map and a key, return a Optional if the key exists and None if the * key does not exist in the map. */ -template +template folly::Optional get_optional( - const Map& map, const typename Map::key_type& key) { + const Map& map, + const Key& key) { auto pos = map.find(key); if (pos != map.end()) { return folly::Optional(pos->second); @@ -99,9 +116,10 @@ folly::Optional get_optional( * key in the map, or the given default reference if the key doesn't exist in * the map. */ -template +template const typename Map::mapped_type& get_ref_default( - const Map& map, const typename Map::key_type& key, + const Map& map, + const Key& key, const typename Map::mapped_type& dflt) { auto pos = map.find(key); return (pos != map.end() ? pos->second : dflt); @@ -113,16 +131,16 @@ const typename Map::mapped_type& get_ref_default( * The caller must ensure that the default value outlives the reference returned * by get_ref_default(). */ -template +template const typename Map::mapped_type& get_ref_default( const Map& map, - const typename Map::key_type& key, + const Key& key, typename Map::mapped_type&& dflt) = delete; -template +template const typename Map::mapped_type& get_ref_default( const Map& map, - const typename Map::key_type& key, + const Key& key, const typename Map::mapped_type&& dflt) = delete; /** @@ -132,16 +150,15 @@ const typename Map::mapped_type& get_ref_default( */ template < class Map, + typename Key = typename Map::key_type, typename Func, typename = typename std::enable_if::type, const typename Map::mapped_type&>::value>::type, typename = typename std::enable_if< std::is_reference::type>::value>::type> -const typename Map::mapped_type& get_ref_default( - const Map& map, - const typename Map::key_type& key, - Func&& dflt) { +const typename Map::mapped_type& +get_ref_default(const Map& map, const Key& key, Func&& dflt) { auto pos = map.find(key); return (pos != map.end() ? pos->second : dflt()); } @@ -150,9 +167,8 @@ const typename Map::mapped_type& get_ref_default( * Given a map and a key, return a pointer to the value corresponding to the * key in the map, or nullptr if the key doesn't exist in the map. */ -template -const typename Map::mapped_type* get_ptr( - const Map& map, const typename Map::key_type& key) { +template +const typename Map::mapped_type* get_ptr(const Map& map, const Key& key) { auto pos = map.find(key); return (pos != map.end() ? &pos->second : nullptr); } @@ -160,9 +176,8 @@ const typename Map::mapped_type* get_ptr( /** * Non-const overload of the above. */ -template -typename Map::mapped_type* get_ptr( - Map& map, const typename Map::key_type& key) { +template +typename Map::mapped_type* get_ptr(Map& map, const Key& key) { auto pos = map.find(key); return (pos != map.end() ? &pos->second : nullptr); } @@ -201,7 +216,7 @@ auto extract_default(const KeysDefault&... keysDefault) -> typename DefaultType::type const& { return std::get(std::tie(keysDefault...)); } -} +} // namespace detail /** * Given a map of maps and a path of keys, return a pointer to the nested value, @@ -274,4 +289,4 @@ auto get_ref_default( } return detail::extract_default(keysDefault...); } -} // namespace folly +} // namespace folly