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;
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; }
129 bool operator<(
const ordered_map &a)
const {
136 auto it = a.data_map.begin();
137 for (
auto &el : data_map) {
138 if (it == a.data_map.end())
return false;
139 if (mapcmp()(el.first, it->first))
return true;
140 if (mapcmp()(it->first, el.first))
return false;
143 return it != a.data_map.end();
150 iterator find(
const key_type &a) {
return tr_iter(data_map.find(&a)); }
151 const_iterator find(
const key_type &a)
const {
return tr_iter(data_map.find(&a)); }
152 size_type count(
const key_type &a)
const {
return data_map.count(&a); }
153 iterator lower_bound(
const key_type &a) {
return tr_iter(data_map.lower_bound(&a)); }
154 const_iterator lower_bound(
const key_type &a)
const {
155 return tr_iter(data_map.lower_bound(&a));
157 iterator upper_bound(
const key_type &a) {
return tr_iter(data_map.upper_bound(&a)); }
158 const_iterator upper_bound(
const key_type &a)
const {
159 return tr_iter(data_map.upper_bound(&a));
161 iterator upper_bound_pred(
const key_type &a) {
162 auto ub = data_map.upper_bound(&a);
163 if (ub == data_map.begin())
return end();
164 return tr_iter(--ub);
166 const_iterator upper_bound_pred(
const key_type &a)
const {
167 auto ub = data_map.upper_bound(&a);
168 if (ub == data_map.begin())
return end();
169 return tr_iter(--ub);
172 V &operator[](
const K &x) {
174 if (it == data.end()) {
175 it = data.emplace(data.end(), x, V());
176 data_map.emplace(&it->first, it);
180 V &operator[](K &&x) {
182 if (it == data.end()) {
183 it = data.emplace(data.end(), std::move(x), V());
184 data_map.emplace(&it->first, it);
188 V &at(
const K &x) {
return data_map.at(&x)->second; }
189 const V &at(
const K &x)
const {
return data_map.at(&x)->second; }
191 template <
typename KK,
typename... VV>
192 std::pair<iterator, bool> emplace(KK &&k, VV &&...v) {
194 if (it == data.end()) {
195 it = data.emplace(data.end(), std::piecewise_construct_t(), std::forward_as_tuple(k),
196 std::forward_as_tuple(std::forward<VV>(v)...));
197 data_map.emplace(&it->first, it);
198 return std::make_pair(it,
true);
200 return std::make_pair(it,
false);
202 template <
typename KK,
typename... VV>
203 std::pair<iterator, bool> emplace_hint(iterator pos, KK &&k, VV &&...v) {
206 if (it == data.end()) {
207 it = data.emplace(pos, std::piecewise_construct_t(), std::forward_as_tuple(k),
208 std::forward_as_tuple(std::forward<VV>(v)...));
209 data_map.emplace(&it->first, it);
210 return std::make_pair(it,
true);
212 return std::make_pair(it,
false);
215 std::pair<iterator, bool> insert(
const value_type &v) {
216 auto it = find(v.first);
217 if (it == data.end()) {
218 it = data.insert(data.end(), v);
219 data_map.emplace(&it->first, it);
220 return std::make_pair(it,
true);
222 return std::make_pair(it,
false);
227 std::pair<iterator, bool> insert(iterator pos,
const value_type &v) {
229 auto it = find(v.first);
230 if (it == data.end()) {
231 it = data.insert(pos, v);
232 data_map.emplace(&it->first, it);
233 return std::make_pair(it,
true);
235 return std::make_pair(it,
false);
237 template <
class InputIterator>
238 void insert(InputIterator b, InputIterator e) {
239 while (b != e) insert(*b++);
244 template <
class InputIterator>
245 void insert(iterator pos, InputIterator b, InputIterator e) {
247 while (b != e) insert(pos, *b++);
250 iterator erase(const_iterator pos) {
251 auto it = data_map.find(&pos->first);
252 assert(it != data_map.end());
255 auto list_it = it->second;
257 return data.erase(list_it);
259 size_type erase(
const K &k) {
261 if (it != data.end()) {
269 template <
class Compare>
270 void sort(Compare comp) {