improved ordered map
This commit is contained in:
parent
d4ea10f548
commit
5190c4d643
@ -24,8 +24,7 @@ NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
|
||||
/// ordered_map: a minimal map-like container that preserves insertion order
|
||||
/// for use within nlohmann::basic_json<ordered_map>
|
||||
template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
class Allocator = std::allocator<std::pair<const Key, T>>>
|
||||
template<class Key, class T, class IgnoredLess = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T>>>
|
||||
struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
|
||||
{
|
||||
using key_type = Key;
|
||||
@ -43,17 +42,24 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
|
||||
// Explicit constructors instead of `using Container::Container`
|
||||
// otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
|
||||
ordered_map() noexcept(noexcept(Container())) : Container{} {}
|
||||
explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
|
||||
ordered_map() noexcept(noexcept(Container()))
|
||||
: Container{}
|
||||
{}
|
||||
explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc)))
|
||||
: Container{alloc}
|
||||
{}
|
||||
template<class It>
|
||||
ordered_map(It first, It last, const Allocator& alloc = Allocator())
|
||||
: Container{first, last, alloc} {}
|
||||
: Container{first, last, alloc}
|
||||
{}
|
||||
ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator())
|
||||
: Container{init, alloc} {}
|
||||
: Container{init, alloc}
|
||||
{}
|
||||
|
||||
std::pair<iterator, bool> emplace(const key_type& key, T&& t)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -64,11 +70,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return {std::prev(this->end()), true};
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
template<class KeyType, detail::enable_if_t<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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -84,8 +90,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return emplace(key, T{}).first->second;
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
T & operator[](KeyType && key)
|
||||
{
|
||||
return emplace(std::forward<KeyType>(key), T{}).first->second;
|
||||
@ -96,8 +101,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return at(key);
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
const T & operator[](KeyType && key) const
|
||||
{
|
||||
return at(std::forward<KeyType>(key));
|
||||
@ -105,22 +109,15 @@ 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)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
const ordered_map<Key, T, IgnoredLess, Allocator>& cThis = *this;
|
||||
return const_cast<T&>(cThis.at(key));
|
||||
}
|
||||
|
||||
JSON_THROW(std::out_of_range("key not found"));
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -133,7 +130,8 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
|
||||
const T& at(const key_type& key) const
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -144,11 +142,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
JSON_THROW(std::out_of_range("key not found"));
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -161,12 +159,13 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
|
||||
size_type erase(const key_type& key)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
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)
|
||||
for (auto next = it; ++next != endItr; ++it)
|
||||
{
|
||||
it->~value_type(); // Destroy but keep allocation
|
||||
new (&*it) value_type{std::move(*next)};
|
||||
@ -178,16 +177,16 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
template<class KeyType, detail::enable_if_t<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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
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)
|
||||
for (auto next = it; ++next != endItr; ++it)
|
||||
{
|
||||
it->~value_type(); // Destroy but keep allocation
|
||||
new (&*it) value_type{std::move(*next)};
|
||||
@ -259,7 +258,8 @@ 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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -269,11 +269,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -285,7 +285,8 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
|
||||
iterator find(const key_type& key)
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -295,11 +296,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
return Container::end();
|
||||
}
|
||||
|
||||
template<class KeyType, detail::enable_if_t<
|
||||
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
|
||||
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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -311,7 +312,8 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
|
||||
|
||||
const_iterator find(const key_type& key) const
|
||||
{
|
||||
for (auto it = this->begin(); it != this->end(); ++it)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, key))
|
||||
{
|
||||
@ -328,7 +330,8 @@ 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)
|
||||
auto endItr = this->end();
|
||||
for (auto it = this->begin(); it != endItr; ++it)
|
||||
{
|
||||
if (m_compare(it->first, value.first))
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user