Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try_emplace c++17 STL feature #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bytell_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,29 @@ class bytell_hash_map
return insert_or_assign(std::move(key), std::forward<M>(m)).first;
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(const key_type & key, Args&&... args)
{
return this->try_emplace_impl(key, std::forward<Args>(args)...);
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(key_type && key, Args&&... args)
{
return try_emplace_impl(std::forward<key_type>(key), std::forward<Args>(args)...);
}

template<typename... Args >
typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args)
{
return try_emplace(key, std::forward<Args>(args)...).first;
}
template<typename... Args>
typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args)
{
return try_emplace(std::forward<key_type>(key), std::forward<Args>(args)...).first;
}

friend bool operator==(const bytell_hash_map & lhs, const bytell_hash_map & rhs)
{
if (lhs.size() != rhs.size())
Expand Down Expand Up @@ -1180,6 +1203,14 @@ class bytell_hash_map
return V();
}
};

template <typename KeyType = key_type, class... Args>
std::pair<typename Table::iterator, bool> try_emplace_impl(KeyType&& key, Args&&... args) {
auto res = this->find(key);
if (res == this->end())
return this->emplace(std::forward<KeyType>(key), std::forward<Args>(args)...);
return { { res }, false };
}
};

template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
Expand Down
31 changes: 31 additions & 0 deletions flat_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,29 @@ class flat_hash_map
return insert_or_assign(std::move(key), std::forward<M>(m)).first;
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(const key_type & key, Args&&... args)
{
return this->try_emplace_impl(key, std::forward<Args>(args)...);
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(key_type && key, Args&&... args)
{
return try_emplace_impl(std::forward<key_type>(key), std::forward<Args>(args)...);
}

template<typename... Args >
typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args)
{
return try_emplace(key, std::forward<Args>(args)...).first;
}
template<typename... Args>
typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args)
{
return try_emplace(std::forward<key_type>(key), std::forward<Args>(args)...).first;
}

friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs)
{
if (lhs.size() != rhs.size())
Expand Down Expand Up @@ -1411,6 +1434,14 @@ class flat_hash_map
return V();
}
};

template <typename KeyType = key_type, class... Args>
std::pair<typename Table::iterator, bool> try_emplace_impl(KeyType&& key, Args&&... args) {
auto res = this->find(key);
if (res == this->end())
return this->emplace(std::forward<KeyType>(key), std::forward<Args>(args)...);
return { { res }, false };
}
};

template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
Expand Down
31 changes: 31 additions & 0 deletions unordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,29 @@ class unordered_map
return emplace(key_type(), convertible_to_value());
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(const key_type & key, Args&&... args)
{
return this->try_emplace_impl(key, std::forward<Args>(args)...);
}

template<typename... Args>
std::pair<typename Table::iterator, bool> try_emplace(key_type && key, Args&&... args)
{
return try_emplace_impl(std::forward<key_type>(key), std::forward<Args>(args)...);
}

template<typename... Args >
typename Table::iterator try_emplace(typename Table::const_iterator, const key_type & key, Args&&... args)
{
return try_emplace(key, std::forward<Args>(args)...).first;
}
template<typename... Args>
typename Table::iterator try_emplace(typename Table::const_iterator, key_type && key, Args&&... args)
{
return try_emplace(std::forward<key_type>(key), std::forward<Args>(args)...).first;
}

friend bool operator==(const unordered_map & lhs, const unordered_map & rhs)
{
if (lhs.size() != rhs.size())
Expand Down Expand Up @@ -805,6 +828,14 @@ class unordered_map
return V();
}
};

template <typename KeyType = key_type, class... Args>
std::pair<typename Table::iterator, bool> try_emplace_impl(KeyType&& key, Args&&... args) {
auto res = this->find(key);
if (res == this->end())
return this->emplace(std::forward<KeyType>(key), std::forward<Args>(args)...);
return { { res }, false };
}
};

template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
Expand Down