4 #include <initializer_list>
12 #include <nlohmann/detail/macro_scope.hpp>
19 template <
class Key,
class T,
class IgnoredLess = std::less<Key>,
20 class Allocator = std::allocator<std::pair<const Key, T>>>
21 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
24 using mapped_type = T;
25 using Container = std::vector<std::pair<const Key, T>, Allocator>;
26 using typename Container::iterator;
27 using typename Container::const_iterator;
28 using typename Container::size_type;
29 using typename Container::value_type;
33 ordered_map(
const Allocator& alloc = Allocator()) : Container{alloc} {}
35 ordered_map(It first, It last,
const Allocator& alloc = Allocator())
36 : Container{first, last, alloc} {}
37 ordered_map(std::initializer_list<T> init,
const Allocator& alloc = Allocator() )
38 : Container{init, alloc} {}
40 std::pair<iterator, bool> emplace(
const key_type& key, T&& t)
42 for (
auto it = this->begin(); it != this->end(); ++it)
49 Container::emplace_back(key, t);
50 return {--this->end(),
true};
53 T& operator[](
const Key& key)
55 return emplace(key, T{}).first->second;
58 const T& operator[](
const Key& key)
const
65 for (
auto it = this->begin(); it != this->end(); ++it)
73 JSON_THROW(std::out_of_range(
"key not found"));
76 const T& at(
const Key& key)
const
78 for (
auto it = this->begin(); it != this->end(); ++it)
86 JSON_THROW(std::out_of_range(
"key not found"));
89 size_type erase(
const Key& key)
91 for (
auto it = this->begin(); it != this->end(); ++it)
96 for (
auto next = it; ++next != this->end(); ++it)
99 new (&*it) value_type{std::move(*next)};
101 Container::pop_back();
108 iterator erase(iterator pos)
113 for (
auto next = it; ++next != this->end(); ++it)
116 new (&*it) value_type{std::move(*next)};
118 Container::pop_back();
122 size_type count(
const Key& key)
const
124 for (
auto it = this->begin(); it != this->end(); ++it)
126 if (it->first == key)
134 iterator find(
const Key& key)
136 for (
auto it = this->begin(); it != this->end(); ++it)
138 if (it->first == key)
143 return Container::end();
146 const_iterator find(
const Key& key)
const
148 for (
auto it = this->begin(); it != this->end(); ++it)
150 if (it->first == key)
155 return Container::end();
158 std::pair<iterator, bool> insert( value_type&& value )
160 return emplace(value.first, std::move(value.second));
163 std::pair<iterator, bool> insert(
const value_type& value )
165 for (
auto it = this->begin(); it != this->end(); ++it)
167 if (it->first == value.first)
172 Container::push_back(value);
173 return {--this->end(),
true};
176 template<
typename InputIt>
177 using require_input_iter =
typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
178 std::input_iterator_tag>::value>::type;
180 template<
typename InputIt,
typename = require_input_iter<InputIt>>
181 void insert(InputIt first, InputIt last)
183 for (
auto it = first; it != last; ++it)
namespace for Niels Lohmann
Definition: adl_serializer.hpp:12
ordered_map: a minimal map-like container that preserves insertion order for use within nlohmann::bas...
Definition: ordered_map.hpp:22