35 typedef V mapped_type;
36 typedef std::pair<const K, V> value_type;
37 typedef COMP key_compare;
38 typedef ALLOC allocator_type;
39 typedef value_type &reference;
40 typedef const value_type &const_reference;
43 typedef std::list<value_type, ALLOC> list_type;
44 typedef typename list_type::iterator list_iterator;
48 typedef typename list_type::iterator iterator;
49 typedef typename list_type::const_iterator const_iterator;
50 typedef std::reverse_iterator<iterator> reverse_iterator;
51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 friend class ordered_map;
58 explicit value_compare(COMP c) : comp(c) {}
61 bool operator()(
const value_type &a,
const value_type &b)
const {
62 return comp(a.first, b.first);
69 bool operator()(
const K *a,
const K *b)
const {
return comp(*a, *b); }
71 using map_alloc =
typename std::allocator_traits<ALLOC>::template rebind_alloc<
72 std::pair<const K *const, list_iterator>>;
73 using map_type = std::map<const K *, list_iterator, mapcmp, map_alloc>;
75 void init_data_map() {
77 for (
auto it = data.begin(); it != data.end(); it++) data_map.emplace(&it->first, it);
79 iterator tr_iter(
typename map_type::iterator i) {
80 if (i == data_map.end())
return data.end();
83 const_iterator tr_iter(
typename map_type::const_iterator i)
const {
84 if (i == data_map.end())
return data.end();
89 typedef typename map_type::size_type size_type;
92 ordered_map(
const ordered_map &a) : data(a.data) { init_data_map(); }
93 template <
typename InputIt>
94 ordered_map(InputIt first, InputIt last) : data(first, last) {
97 ordered_map(ordered_map &&a) =
default;
98 ordered_map &operator=(
const ordered_map &a) {
102 for (
auto &el : a.data) data.push_back(el);
107 ordered_map &operator=(ordered_map &&a) =
default;
108 ordered_map(std::initializer_list<value_type> il) : data(il) { init_data_map(); }
111 iterator begin() noexcept {
return data.begin(); }
112 const_iterator begin() const noexcept {
return data.begin(); }
113 iterator end() noexcept {
return data.end(); }
114 const_iterator end() const noexcept {
return data.end(); }
115 reverse_iterator rbegin() noexcept {
return data.rbegin(); }
116 const_reverse_iterator rbegin() const noexcept {
return data.rbegin(); }
117 reverse_iterator rend() noexcept {
return data.rend(); }
118 const_reverse_iterator rend() const noexcept {
return data.rend(); }
119 const_iterator cbegin() const noexcept {
return data.cbegin(); }
120 const_iterator cend() const noexcept {
return data.cend(); }
121 const_reverse_iterator crbegin() const noexcept {
return data.crbegin(); }
122 const_reverse_iterator crend() const noexcept {
return data.crend(); }
124 bool empty() const noexcept {
return data.empty(); }
125 size_type size() const noexcept {
return data_map.size(); }
126 size_type max_size() const noexcept {
return data_map.max_size(); }
127 bool operator==(
const ordered_map &a)
const {
return data == a.data; }
128 bool operator!=(
const ordered_map &a)
const {
return data != a.data; }
134 iterator find(
const key_type &a) {
return tr_iter(data_map.find(&a)); }
135 const_iterator find(
const key_type &a)
const {
return tr_iter(data_map.find(&a)); }
136 size_type count(
const key_type &a)
const {
return data_map.count(&a); }
137 iterator lower_bound(
const key_type &a) {
return tr_iter(data_map.lower_bound(&a)); }
138 const_iterator lower_bound(
const key_type &a)
const {
139 return tr_iter(data_map.lower_bound(&a));
141 iterator upper_bound(
const key_type &a) {
return tr_iter(data_map.upper_bound(&a)); }
142 const_iterator upper_bound(
const key_type &a)
const {
143 return tr_iter(data_map.upper_bound(&a));
145 iterator upper_bound_pred(
const key_type &a) {
146 auto ub = data_map.upper_bound(&a);
147 if (ub == data_map.begin())
return end();
148 return tr_iter(--ub);
150 const_iterator upper_bound_pred(
const key_type &a)
const {
151 auto ub = data_map.upper_bound(&a);
152 if (ub == data_map.begin())
return end();
153 return tr_iter(--ub);
156 V &operator[](
const K &x) {
158 if (it == data.end()) {
159 it = data.emplace(data.end(), x, V());
160 data_map.emplace(&it->first, it);
164 V &operator[](K &&x) {
166 if (it == data.end()) {
167 it = data.emplace(data.end(), std::move(x), V());
168 data_map.emplace(&it->first, it);
172 V &at(
const K &x) {
return data_map.at(&x)->second; }
173 const V &at(
const K &x)
const {
return data_map.at(&x)->second; }
175 template <
typename KK,
typename... VV>
176 std::pair<iterator, bool> emplace(KK &&k, VV &&...v) {
178 if (it == data.end()) {
179 it = data.emplace(data.end(), std::piecewise_construct_t(), std::forward_as_tuple(k),
180 std::forward_as_tuple(std::forward<VV>(v)...));
181 data_map.emplace(&it->first, it);
182 return std::make_pair(it,
true);
184 return std::make_pair(it,
false);
186 template <
typename KK,
typename... VV>
187 std::pair<iterator, bool> emplace_hint(iterator pos, KK &&k, VV &&...v) {
190 if (it == data.end()) {
191 it = data.emplace(pos, std::piecewise_construct_t(), std::forward_as_tuple(k),
192 std::forward_as_tuple(std::forward<VV>(v)...));
193 data_map.emplace(&it->first, it);
194 return std::make_pair(it,
true);
196 return std::make_pair(it,
false);
199 std::pair<iterator, bool> insert(
const value_type &v) {
200 auto it = find(v.first);
201 if (it == data.end()) {
202 it = data.insert(data.end(), v);
203 data_map.emplace(&it->first, it);
204 return std::make_pair(it,
true);
206 return std::make_pair(it,
false);
211 std::pair<iterator, bool> insert(iterator pos,
const value_type &v) {
213 auto it = find(v.first);
214 if (it == data.end()) {
215 it = data.insert(pos, v);
216 data_map.emplace(&it->first, it);
217 return std::make_pair(it,
true);
219 return std::make_pair(it,
false);
221 template <
class InputIterator>
222 void insert(InputIterator b, InputIterator e) {
223 while (b != e) insert(*b++);
228 template <
class InputIterator>
229 void insert(iterator pos, InputIterator b, InputIterator e) {
231 while (b != e) insert(pos, *b++);
234 iterator erase(const_iterator pos) {
235 auto it = data_map.find(&pos->first);
236 assert(it != data_map.end());
239 auto list_it = it->second;
241 return data.erase(list_it);
243 size_type erase(
const K &k) {
245 if (it != data.end()) {
253 template <
class Compare>
254 void sort(Compare comp) {