Deduplicate the linear key search in ordered_map

emplace, at, erase(key), count and find each repeated the same
"for (auto it = begin(); it != end(); ++it) if (m_compare(it->first,
key)) ..." loop (15 copies across their key_type and transparent
KeyType&& overloads), and both erase(key) overloads additionally
repeated the exception-sensitive in-place reconstruction (destroy,
placement-new, pop_back) used to remove an element while keeping the
const Key non-movable.

Add two private helpers: find_impl(Self&, KeyType&&), a static member
template that runs the search once for either constness of the
receiver, and erase_at(iterator), which keeps the existing
pop_back-based reconstruction instead of switching to erase()/resize()
(which would add a DefaultInsertable requirement). Route find, at,
count, emplace, insert(const value_type&) and both erase(key)
overloads through them.

Same signatures, is_usable_as_key_type constraints and exception
messages/types.

Overlaps #5609 and #5685, which both rewrite emplace (#5609 also
touches insert and adds private members at the end of the class);
whichever of this commit and those PRs lands second will need a
rebase.

#5724 item 6

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-30 19:05:05 +02:00
parent 51233dc17d
commit 3e771c2ad7
2 changed files with 136 additions and 230 deletions
+68 -115
View File
@@ -70,14 +70,41 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return *this;
}
private:
/// @brief find the entry for @a key, for either constness of @a self
/// @note the single place that performs the linear key search
template<typename Self, typename KeyType>
static auto find_impl(Self& self, KeyType&& key) -> decltype(self.begin())
{
for (auto it = self.begin(); it != self.end(); ++it)
{
if (self.m_compare(it->first, key))
{
return it;
}
}
return self.end();
}
/// @brief remove the entry @a it points to, preserving order
/// @note keys are not movable, so the tail is destroyed and re-constructed in place
void erase_at(iterator it)
{
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
}
public:
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
return {it, false};
}
return {it, false};
}
Container::emplace_back(key, std::forward<T>(t));
return {std::prev(this->end()), true};
@@ -87,12 +114,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
std::pair<iterator, bool> emplace(KeyType && key, T && t)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
return {it, false};
}
return {it, false};
}
Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
return {std::prev(this->end()), true};
@@ -124,75 +149,55 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
T& at(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
const T& at(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
size_type erase(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
erase_at(it);
return 1;
}
return 0;
}
@@ -201,19 +206,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
erase_at(it);
return 1;
}
return 0;
}
@@ -278,80 +275,38 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
size_type count(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return 1;
}
}
return 0;
return find_impl(*this, key) != this->end() ? 1 : 0;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return 1;
}
}
return 0;
return find_impl(*this, key) != this->end() ? 1 : 0;
}
iterator find(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
const_iterator find(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
std::pair<iterator, bool> insert( value_type&& value )
@@ -361,12 +316,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
std::pair<iterator, bool> insert( const value_type& value )
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, value.first);
if (it != this->end())
{
if (m_compare(it->first, value.first))
{
return {it, false};
}
return {it, false};
}
Container::push_back(value);
return {--this->end(), true};
+68 -115
View File
@@ -25831,14 +25831,41 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return *this;
}
private:
/// @brief find the entry for @a key, for either constness of @a self
/// @note the single place that performs the linear key search
template<typename Self, typename KeyType>
static auto find_impl(Self& self, KeyType&& key) -> decltype(self.begin())
{
for (auto it = self.begin(); it != self.end(); ++it)
{
if (self.m_compare(it->first, key))
{
return it;
}
}
return self.end();
}
/// @brief remove the entry @a it points to, preserving order
/// @note keys are not movable, so the tail is destroyed and re-constructed in place
void erase_at(iterator it)
{
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
}
public:
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
return {it, false};
}
return {it, false};
}
Container::emplace_back(key, std::forward<T>(t));
return {std::prev(this->end()), true};
@@ -25848,12 +25875,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
std::pair<iterator, bool> emplace(KeyType && key, T && t)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
return {it, false};
}
return {it, false};
}
Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
return {std::prev(this->end()), true};
@@ -25885,75 +25910,55 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
T& at(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
const T& at(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it == this->end())
{
if (m_compare(it->first, key))
{
return it->second;
}
JSON_THROW(std::out_of_range("key not found"));
}
JSON_THROW(std::out_of_range("key not found"));
return it->second;
}
size_type erase(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
erase_at(it);
return 1;
}
return 0;
}
@@ -25962,19 +25967,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, key);
if (it != this->end())
{
if (m_compare(it->first, key))
{
// Since we cannot move const Keys, re-construct them in place
for (auto next = it; ++next != this->end(); ++it)
{
it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)};
}
Container::pop_back();
return 1;
}
erase_at(it);
return 1;
}
return 0;
}
@@ -26039,80 +26036,38 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
size_type count(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return 1;
}
}
return 0;
return find_impl(*this, key) != this->end() ? 1 : 0;
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return 1;
}
}
return 0;
return find_impl(*this, key) != this->end() ? 1 : 0;
}
iterator find(const key_type& key)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
const_iterator find(const key_type& key) const
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
template<class KeyType, detail::enable_if_t<
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
{
for (auto it = this->begin(); it != this->end(); ++it)
{
if (m_compare(it->first, key))
{
return it;
}
}
return Container::end();
return find_impl(*this, key);
}
std::pair<iterator, bool> insert( value_type&& value )
@@ -26122,12 +26077,10 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
std::pair<iterator, bool> insert( const value_type& value )
{
for (auto it = this->begin(); it != this->end(); ++it)
const auto it = find_impl(*this, value.first);
if (it != this->end())
{
if (m_compare(it->first, value.first))
{
return {it, false};
}
return {it, false};
}
Container::push_back(value);
return {--this->end(), true};