Add transparent at()

This commit is contained in:
Christian Mazakas
2022-11-16 14:00:22 -08:00
parent 1f4244ec27
commit 0e980577b0
2 changed files with 74 additions and 0 deletions

View File

@ -473,6 +473,34 @@ namespace boost {
std::out_of_range("key was not found in unordered_flat_map")); std::out_of_range("key was not found in unordered_flat_map"));
} }
template <class K>
typename std::enable_if<
boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
mapped_type&>::type
at(K&& key)
{
auto pos = table_.find(std::forward<K>(key));
if (pos != table_.end()) {
return pos->second;
}
boost::throw_exception(
std::out_of_range("key was not found in unordered_flat_map"));
}
template <class K>
typename std::enable_if<
boost::unordered::detail::are_transparent<K, hasher, key_equal>::value,
mapped_type const&>::type
at(K&& key) const
{
auto pos = table_.find(std::forward<K>(key));
if (pos != table_.end()) {
return pos->second;
}
boost::throw_exception(
std::out_of_range("key was not found in unordered_flat_map"));
}
BOOST_FORCEINLINE mapped_type& operator[](key_type const& key) BOOST_FORCEINLINE mapped_type& operator[](key_type const& key)
{ {
return table_.try_emplace(key).first->second; return table_.try_emplace(key).first->second;

View File

@ -973,13 +973,23 @@ namespace boost {
mapped_type& operator[](const key_type&); mapped_type& operator[](const key_type&);
mapped_type& operator[](BOOST_RV_REF(key_type)); mapped_type& operator[](BOOST_RV_REF(key_type));
template <class Key> template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value, typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
mapped_type&>::type mapped_type&>::type
operator[](BOOST_FWD_REF(Key) k); operator[](BOOST_FWD_REF(Key) k);
mapped_type& at(const key_type&); mapped_type& at(const key_type&);
mapped_type const& at(const key_type&) const; mapped_type const& at(const key_type&) const;
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
mapped_type&>::type at(BOOST_FWD_REF(Key) k);
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
mapped_type const&>::type at(BOOST_FWD_REF(Key) k) const;
// bucket interface // bucket interface
size_type bucket_count() const BOOST_NOEXCEPT size_type bucket_count() const BOOST_NOEXCEPT
@ -2260,6 +2270,42 @@ namespace boost {
std::out_of_range("Unable to find key in unordered_map.")); std::out_of_range("Unable to find key in unordered_map."));
} }
template <class K, class T, class H, class P, class A>
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
typename unordered_map<K, T, H, P, A>::mapped_type&>::type
unordered_map<K, T, H, P, A>::at(BOOST_FWD_REF(Key) k)
{
typedef typename table::node_pointer node_pointer;
if (table_.size_) {
node_pointer p = table_.find_node(boost::forward<Key>(k));
if (p)
return p->value().second;
}
boost::throw_exception(
std::out_of_range("Unable to find key in unordered_map."));
}
template <class K, class T, class H, class P, class A>
template <class Key>
typename boost::enable_if_c<detail::are_transparent<Key, H, P>::value,
typename unordered_map<K, T, H, P, A>::mapped_type const&>::type
unordered_map<K, T, H, P, A>::at(BOOST_FWD_REF(Key) k) const
{
typedef typename table::node_pointer node_pointer;
if (table_.size_) {
node_pointer p = table_.find_node(boost::forward<Key>(k));
if (p)
return p->value().second;
}
boost::throw_exception(
std::out_of_range("Unable to find key in unordered_map."));
}
template <class K, class T, class H, class P, class A> template <class K, class T, class H, class P, class A>
typename unordered_map<K, T, H, P, A>::size_type typename unordered_map<K, T, H, P, A>::size_type
unordered_map<K, T, H, P, A>::bucket_size(size_type n) const unordered_map<K, T, H, P, A>::bucket_size(size_type n) const