12#include <initializer_list>
27template <
class Key,
class T,
class IgnoredLess = std::less<Key>,
28 class Allocator = std::allocator<std::pair<const Key, T>>>
29 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
33 using Container = std::vector<std::pair<const Key, T>, Allocator>;
51 ordered_map(std::initializer_list<value_type> init,
const Allocator& alloc = Allocator() )
56 for (
auto it = this->begin(); it != this->end(); ++it)
58 if (m_compare(it->first, key))
63 Container::emplace_back(key, std::forward<T>(t));
64 return {std::prev(this->end()),
true};
69 std::pair<iterator, bool>
emplace(KeyType && key, T && t)
71 for (
auto it = this->begin(); it != this->end(); ++it)
73 if (m_compare(it->first, key))
78 Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
79 return {std::prev(this->end()),
true};
91 return emplace(std::forward<KeyType>(key), T{}).
first->second;
103 return at(std::forward<KeyType>(key));
108 for (
auto it = this->begin(); it != this->end(); ++it)
110 if (m_compare(it->first, key))
116 JSON_THROW(std::out_of_range(
"key not found"));
121 T &
at(KeyType && key)
123 for (
auto it = this->begin(); it != this->end(); ++it)
125 if (m_compare(it->first, key))
131 JSON_THROW(std::out_of_range(
"key not found"));
136 for (
auto it = this->begin(); it != this->end(); ++it)
138 if (m_compare(it->first, key))
144 JSON_THROW(std::out_of_range(
"key not found"));
149 const T &
at(KeyType && key)
const
151 for (
auto it = this->begin(); it != this->end(); ++it)
153 if (m_compare(it->first, key))
159 JSON_THROW(std::out_of_range(
"key not found"));
164 for (
auto it = this->begin(); it != this->end(); ++it)
166 if (m_compare(it->first, key))
169 for (
auto next = it; ++next != this->end(); ++it)
174 Container::pop_back();
185 for (
auto it = this->begin(); it != this->end(); ++it)
187 if (m_compare(it->first, key))
190 for (
auto next = it; ++next != this->end(); ++it)
195 Container::pop_back();
204 return erase(pos, std::next(pos));
214 const auto elements_affected = std::distance(
first, last);
215 const auto offset = std::distance(Container::begin(),
first);
237 for (
auto it =
first; std::next(it, elements_affected) != Container::end(); ++it)
240 new (&*it)
value_type{std::move(*std::next(it, elements_affected))};
248 Container::resize(this->size() -
static_cast<size_type>(elements_affected));
257 return Container::begin() + offset;
262 for (
auto it = this->begin(); it != this->end(); ++it)
264 if (m_compare(it->first, key))
276 for (
auto it = this->begin(); it != this->end(); ++it)
278 if (m_compare(it->first, key))
288 for (
auto it = this->begin(); it != this->end(); ++it)
290 if (m_compare(it->first, key))
295 return Container::end();
302 for (
auto it = this->begin(); it != this->end(); ++it)
304 if (m_compare(it->first, key))
309 return Container::end();
314 for (
auto it = this->begin(); it != this->end(); ++it)
316 if (m_compare(it->first, key))
321 return Container::end();
326 return emplace(value.first, std::move(value.second));
331 for (
auto it = this->begin(); it != this->end(); ++it)
333 if (m_compare(it->first, value.first))
338 Container::push_back(value);
339 return {--this->end(),
true};
342 template<
typename InputIt>
343 using require_input_iter =
typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
344 std::input_iterator_tag>::value>
::type;
346 template<
typename InputIt,
typename = require_input_iter<InputIt>>
349 for (
auto it =
first; it != last; ++it)
#define WPI_JSON_NAMESPACE_END
Definition: abi_macros.h:59
#define WPI_JSON_NAMESPACE_BEGIN
Definition: abi_macros.h:53
#define JSON_THROW(exception)
Definition: macro_scope.h:163
#define JSON_NO_UNIQUE_ADDRESS
Definition: macro_scope.h:145
typename std::enable_if< B, T >::type enable_if_t
Definition: cpp_future.h:38
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
typename std::conditional< is_comparable< Comparator, ObjectKeyType, KeyTypeCVRef >::value &&!(ExcludeObjectKeyType &&std::is_same< KeyType, ObjectKeyType >::value) &&(!RequireTransparentComparator||is_detected< detect_is_transparent, Comparator >::value) &&!is_json_pointer< KeyType >::value, std::true_type, std::false_type >::type is_usable_as_key_type
Definition: type_traits.h:531
type
Definition: core.h:556
a minimal map-like container that preserves insertion order
Definition: ordered_map.h:30
std::vector< std::pair< const Key, T >, Allocator > Container
Definition: ordered_map.h:33
std::pair< iterator, bool > insert(value_type &&value)
Definition: ordered_map.h:324
typename Container::value_type value_type
Definition: ordered_map.h:37
std::equal_to< Key > key_compare
Definition: ordered_map.h:41
iterator erase(iterator pos)
Definition: ordered_map.h:202
T mapped_type
Definition: ordered_map.h:32
ordered_map(const Allocator &alloc) noexcept(noexcept(Container(alloc)))
Definition: ordered_map.h:47
T & operator[](KeyType &&key)
Definition: ordered_map.h:89
typename Container::iterator iterator
Definition: ordered_map.h:34
const T & at(KeyType &&key) const
Definition: ordered_map.h:149
T & at(KeyType &&key)
Definition: ordered_map.h:121
const T & operator[](KeyType &&key) const
Definition: ordered_map.h:101
iterator find(const key_type &key)
Definition: ordered_map.h:286
iterator erase(iterator first, iterator last)
Definition: ordered_map.h:207
const T & at(const key_type &key) const
Definition: ordered_map.h:134
const_iterator find(const key_type &key) const
Definition: ordered_map.h:312
T & operator[](const key_type &key)
Definition: ordered_map.h:82
size_type erase(KeyType &&key)
Definition: ordered_map.h:183
typename Container::size_type size_type
Definition: ordered_map.h:36
ordered_map() noexcept(noexcept(Container()))
Definition: ordered_map.h:46
void insert(InputIt first, InputIt last)
Definition: ordered_map.h:347
size_type count(const key_type &key) const
Definition: ordered_map.h:260
std::pair< iterator, bool > emplace(KeyType &&key, T &&t)
Definition: ordered_map.h:69
size_type erase(const key_type &key)
Definition: ordered_map.h:162
ordered_map(std::initializer_list< value_type > init, const Allocator &alloc=Allocator())
Definition: ordered_map.h:51
std::pair< iterator, bool > insert(const value_type &value)
Definition: ordered_map.h:329
size_type count(KeyType &&key) const
Definition: ordered_map.h:274
Key key_type
Definition: ordered_map.h:31
ordered_map(It first, It last, const Allocator &alloc=Allocator())
Definition: ordered_map.h:49
typename std::enable_if< std::is_convertible< typename std::iterator_traits< InputIt >::iterator_category, std::input_iterator_tag >::value >::type require_input_iter
Definition: ordered_map.h:344
const T & operator[](const key_type &key) const
Definition: ordered_map.h:94
iterator find(KeyType &&key)
Definition: ordered_map.h:300
T & at(const key_type &key)
Definition: ordered_map.h:106
typename Container::const_iterator const_iterator
Definition: ordered_map.h:35
std::pair< iterator, bool > emplace(const key_type &key, T &&t)
Definition: ordered_map.h:54