From ba27bd294e4b6fdd325265413c63a88b10ed1b6e Mon Sep 17 00:00:00 2001 From: WeHaveCookie Date: Wed, 7 Nov 2018 16:29:09 +0100 Subject: [PATCH 1/2] try_emplace c++17 feature --- flat_hash_map.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/flat_hash_map.hpp b/flat_hash_map.hpp index a8723ee..3b4b3cc 100644 --- a/flat_hash_map.hpp +++ b/flat_hash_map.hpp @@ -1384,6 +1384,29 @@ class flat_hash_map return insert_or_assign(std::move(key), std::forward(m)).first; } + template + std::pair try_emplace(const key_type & key, Args&&... args) + { + return this->try_emplace_impl(key, std::forward(args)...); + } + + template + std::pair try_emplace(key_type && key, Args&&... args) + { + return try_emplace_impl(std::forward(key), std::forward(args)...); + } + + template + typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args) + { + return try_emplace(key, std::forward(args)...).first; + } + template + typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args) + { + return try_emplace(std::forward(key), std::forward(args)...).first; + } + friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs) { if (lhs.size() != rhs.size()) @@ -1411,6 +1434,14 @@ class flat_hash_map return V(); } }; + + template + std::pair try_emplace_impl(KeyType&& key, Args&&... args) { + auto res = this->find(key); + if (res == this->end()) + return this->emplace(std::forward(key), std::forward(args)...); + return { { res }, false }; + } }; template, typename E = std::equal_to, typename A = std::allocator > From 6141c31620d102f2bc7ec73525bffa19c2896907 Mon Sep 17 00:00:00 2001 From: WeHaveCookie Date: Wed, 7 Nov 2018 16:55:40 +0100 Subject: [PATCH 2/2] Try_emplace for bytell & unordered map --- bytell_hash_map.hpp | 31 +++++++++++++++++++++++++++++++ unordered_map.hpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/bytell_hash_map.hpp b/bytell_hash_map.hpp index 9c9a246..9afaa8c 100644 --- a/bytell_hash_map.hpp +++ b/bytell_hash_map.hpp @@ -1153,6 +1153,29 @@ class bytell_hash_map return insert_or_assign(std::move(key), std::forward(m)).first; } + template + std::pair try_emplace(const key_type & key, Args&&... args) + { + return this->try_emplace_impl(key, std::forward(args)...); + } + + template + std::pair try_emplace(key_type && key, Args&&... args) + { + return try_emplace_impl(std::forward(key), std::forward(args)...); + } + + template + typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args) + { + return try_emplace(key, std::forward(args)...).first; + } + template + typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args) + { + return try_emplace(std::forward(key), std::forward(args)...).first; + } + friend bool operator==(const bytell_hash_map & lhs, const bytell_hash_map & rhs) { if (lhs.size() != rhs.size()) @@ -1180,6 +1203,14 @@ class bytell_hash_map return V(); } }; + + template + std::pair try_emplace_impl(KeyType&& key, Args&&... args) { + auto res = this->find(key); + if (res == this->end()) + return this->emplace(std::forward(key), std::forward(args)...); + return { { res }, false }; + } }; template, typename E = std::equal_to, typename A = std::allocator > diff --git a/unordered_map.hpp b/unordered_map.hpp index 5e6e876..462b590 100644 --- a/unordered_map.hpp +++ b/unordered_map.hpp @@ -778,6 +778,29 @@ class unordered_map return emplace(key_type(), convertible_to_value()); } + template + std::pair try_emplace(const key_type & key, Args&&... args) + { + return this->try_emplace_impl(key, std::forward(args)...); + } + + template + std::pair try_emplace(key_type && key, Args&&... args) + { + return try_emplace_impl(std::forward(key), std::forward(args)...); + } + + template + typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args) + { + return try_emplace(key, std::forward(args)...).first; + } + template + typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args) + { + return try_emplace(std::forward(key), std::forward(args)...).first; + } + friend bool operator==(const unordered_map & lhs, const unordered_map & rhs) { if (lhs.size() != rhs.size()) @@ -805,6 +828,14 @@ class unordered_map return V(); } }; + + template + std::pair try_emplace_impl(KeyType&& key, Args&&... args) { + auto res = this->find(key); + if (res == this->end()) + return this->emplace(std::forward(key), std::forward(args)...); + return { { res }, false }; + } }; template, typename E = std::equal_to, typename A = std::allocator >