improved ordered map

This commit is contained in:
Tomerkm 2023-06-16 13:32:47 +03:00
parent d4ea10f548
commit 5190c4d643

View File

@ -8,14 +8,14 @@
#pragma once #pragma once
#include <functional> // equal_to, less #include <functional> // equal_to, less
#include <initializer_list> // initializer_list #include <initializer_list> // initializer_list
#include <iterator> // input_iterator_tag, iterator_traits #include <iterator> // input_iterator_tag, iterator_traits
#include <memory> // allocator #include <memory> // allocator
#include <stdexcept> // for out_of_range #include <stdexcept> // for out_of_range
#include <type_traits> // enable_if, is_convertible #include <type_traits> // enable_if, is_convertible
#include <utility> // pair #include <utility> // pair
#include <vector> // vector #include <vector> // vector
#include <nlohmann/detail/macro_scope.hpp> #include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/type_traits.hpp> #include <nlohmann/detail/meta/type_traits.hpp>
@ -24,9 +24,8 @@ NLOHMANN_JSON_NAMESPACE_BEGIN
/// ordered_map: a minimal map-like container that preserves insertion order /// ordered_map: a minimal map-like container that preserves insertion order
/// for use within nlohmann::basic_json<ordered_map> /// for use within nlohmann::basic_json<ordered_map>
template <class Key, class T, class IgnoredLess = std::less<Key>, template<class Key, class T, class IgnoredLess = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T>>>
class Allocator = std::allocator<std::pair<const Key, T>>> struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
{ {
using key_type = Key; using key_type = Key;
using mapped_type = T; using mapped_type = T;
@ -43,17 +42,24 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
// Explicit constructors instead of `using Container::Container` // Explicit constructors instead of `using Container::Container`
// otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4) // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
ordered_map() noexcept(noexcept(Container())) : Container{} {} ordered_map() noexcept(noexcept(Container()))
explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {} : Container{}
template <class It> {}
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()) 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} {} ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator())
: Container{init, alloc}
{}
std::pair<iterator, bool> emplace(const key_type& key, T&& t) 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)) 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}; return {std::prev(this->end()), true};
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
std::pair<iterator, bool> emplace(KeyType && key, T && t) 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)) 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; return emplace(key, T{}).first->second;
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
T & operator[](KeyType && key) T & operator[](KeyType && key)
{ {
return emplace(std::forward<KeyType>(key), T{}).first->second; 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); return at(key);
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
const T & operator[](KeyType && key) const const T & operator[](KeyType && key) const
{ {
return at(std::forward<KeyType>(key)); 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) T& at(const key_type& key)
{ {
for (auto it = this->begin(); it != this->end(); ++it) const ordered_map<Key, T, IgnoredLess, Allocator>& cThis = *this;
{ return const_cast<T&>(cThis.at(key));
if (m_compare(it->first, key))
{
return it->second;
}
}
JSON_THROW(std::out_of_range("key not found"));
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward) 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)) 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 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)) 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")); JSON_THROW(std::out_of_range("key not found"));
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
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) 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)) if (m_compare(it->first, key))
{ {
@ -161,14 +159,15 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
size_type erase(const key_type& 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)) if (m_compare(it->first, key))
{ {
// Since we cannot move const Keys, re-construct them in place // 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 it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)}; new (&*it) value_type{std::move(*next)};
} }
Container::pop_back(); Container::pop_back();
@ -178,18 +177,18 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return 0; return 0;
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward) 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)) if (m_compare(it->first, key))
{ {
// Since we cannot move const Keys, re-construct them in place // 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 it->~value_type(); // Destroy but keep allocation
new (&*it) value_type{std::move(*next)}; new (&*it) value_type{std::move(*next)};
} }
Container::pop_back(); Container::pop_back();
@ -236,8 +235,8 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it) for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
{ {
it->~value_type(); // destroy but keep allocation it->~value_type(); // destroy but keep allocation
new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
} }
// [ a, b, c, d, h, i, j, h, i, j ] // [ a, b, c, d, h, i, j, h, i, j ]
@ -259,7 +258,8 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
size_type count(const key_type& key) const 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)) if (m_compare(it->first, key))
{ {
@ -269,11 +269,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return 0; return 0;
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
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) 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)) 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) 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)) if (m_compare(it->first, key))
{ {
@ -295,11 +296,11 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return Container::end(); return Container::end();
} }
template<class KeyType, detail::enable_if_t< template<class KeyType, detail::enable_if_t<detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward) 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)) 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 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)) if (m_compare(it->first, key))
{ {
@ -321,14 +323,15 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
return Container::end(); return Container::end();
} }
std::pair<iterator, bool> insert( value_type&& value ) std::pair<iterator, bool> insert(value_type&& value)
{ {
return emplace(value.first, std::move(value.second)); return emplace(value.first, std::move(value.second));
} }
std::pair<iterator, bool> insert( const value_type& value ) 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)) if (m_compare(it->first, value.first))
{ {