33    typedef V mapped_type;
 
   34    typedef std::pair<const K, V> value_type;
 
   35    typedef COMP key_compare;
 
   36    typedef ALLOC allocator_type;
 
   37    typedef value_type &reference;
 
   38    typedef const value_type &const_reference;
 
   41    typedef std::list<value_type, ALLOC> list_type;
 
   42    typedef typename list_type::iterator list_iterator;
 
   46    typedef typename list_type::iterator iterator;
 
   47    typedef typename list_type::const_iterator const_iterator;
 
   48    typedef std::reverse_iterator<iterator> reverse_iterator;
 
   49    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
   59        bool operator()(
const value_type &a, 
const value_type &b)
 const {
 
   60            return comp(a.first, b.first);
 
 
   67        bool operator()(
const K *a, 
const K *b)
 const { 
return comp(*a, *b); }
 
   69    using map_alloc = 
typename std::allocator_traits<ALLOC>::template rebind_alloc<
 
   70        std::pair<const K *const, list_iterator>>;
 
   71    using map_type = std::map<const K *, list_iterator, mapcmp, map_alloc>;
 
   73    void init_data_map() {
 
   75        for (
auto it = data.begin(); it != data.end(); it++) data_map.emplace(&it->first, it);
 
   77    iterator tr_iter(
typename map_type::iterator i) {
 
   78        if (i == data_map.end()) 
return data.end();
 
   81    const_iterator tr_iter(
typename map_type::const_iterator i)
 const {
 
   82        if (i == data_map.end()) 
return data.end();
 
   87    typedef typename map_type::size_type size_type;
 
   91    template <
typename InputIt>
 
   92    ordered_map(InputIt first, InputIt last) : data(first, last) {
 
  100            for (
auto &el : a.data) data.push_back(el);
 
  106    ordered_map(std::initializer_list<value_type> il) : data(il) { init_data_map(); }
 
  109    iterator begin() noexcept { 
return data.begin(); }
 
  110    const_iterator begin() const noexcept { 
return data.begin(); }
 
  111    iterator end() noexcept { 
return data.end(); }
 
  112    const_iterator end() const noexcept { 
return data.end(); }
 
  113    reverse_iterator rbegin() noexcept { 
return data.rbegin(); }
 
  114    const_reverse_iterator rbegin() const noexcept { 
return data.rbegin(); }
 
  115    reverse_iterator rend() noexcept { 
return data.rend(); }
 
  116    const_reverse_iterator rend() const noexcept { 
return data.rend(); }
 
  117    const_iterator cbegin() const noexcept { 
return data.cbegin(); }
 
  118    const_iterator cend() const noexcept { 
return data.cend(); }
 
  119    const_reverse_iterator crbegin() const noexcept { 
return data.crbegin(); }
 
  120    const_reverse_iterator crend() const noexcept { 
return data.crend(); }
 
  122    bool empty() const noexcept { 
return data.empty(); }
 
  123    size_type size() const noexcept { 
return data_map.size(); }
 
  124    size_type max_size() const noexcept { 
return data_map.max_size(); }
 
  125    bool operator==(
const ordered_map &a)
 const { 
return data == a.data; }
 
  126    bool operator!=(
const ordered_map &a)
 const { 
return data != a.data; }
 
  132    iterator find(
const key_type &a) { 
return tr_iter(data_map.find(&a)); }
 
  133    const_iterator find(
const key_type &a)
 const { 
return tr_iter(data_map.find(&a)); }
 
  134    size_type count(
const key_type &a)
 const { 
return data_map.count(&a); }
 
  135    iterator lower_bound(
const key_type &a) { 
return tr_iter(data_map.lower_bound(&a)); }
 
  136    const_iterator lower_bound(
const key_type &a)
 const {
 
  137        return tr_iter(data_map.lower_bound(&a));
 
  139    iterator upper_bound(
const key_type &a) { 
return tr_iter(data_map.upper_bound(&a)); }
 
  140    const_iterator upper_bound(
const key_type &a)
 const {
 
  141        return tr_iter(data_map.upper_bound(&a));
 
  143    iterator upper_bound_pred(
const key_type &a) {
 
  144        auto ub = data_map.upper_bound(&a);
 
  145        if (ub == data_map.begin()) 
return end();
 
  146        return tr_iter(--ub);
 
  148    const_iterator upper_bound_pred(
const key_type &a)
 const {
 
  149        auto ub = data_map.upper_bound(&a);
 
  150        if (ub == data_map.begin()) 
return end();
 
  151        return tr_iter(--ub);
 
  154    V &operator[](
const K &x) {
 
  156        if (it == data.end()) {
 
  157            it = data.emplace(data.end(), x, V());
 
  158            data_map.emplace(&it->first, it);
 
  162    V &operator[](K &&x) {
 
  164        if (it == data.end()) {
 
  165            it = data.emplace(data.end(), std::move(x), V());
 
  166            data_map.emplace(&it->first, it);
 
  170    V &at(
const K &x) { 
return data_map.at(&x)->second; }
 
  171    const V &at(
const K &x)
 const { 
return data_map.at(&x)->second; }
 
  173    template <
typename KK, 
typename... VV>
 
  174    std::pair<iterator, bool> emplace(KK &&k, VV &&...v) {
 
  176        if (it == data.end()) {
 
  177            it = data.emplace(data.end(), std::piecewise_construct_t(), std::forward_as_tuple(k),
 
  178                              std::forward_as_tuple(std::forward<VV>(v)...));
 
  179            data_map.emplace(&it->first, it);
 
  180            return std::make_pair(it, 
true);
 
  182        return std::make_pair(it, 
false);
 
  184    template <
typename KK, 
typename... VV>
 
  185    std::pair<iterator, bool> emplace_hint(iterator pos, KK &&k, VV &&...v) {
 
  188        if (it == data.end()) {
 
  189            it = data.emplace(pos, std::piecewise_construct_t(), std::forward_as_tuple(k),
 
  190                              std::forward_as_tuple(std::forward<VV>(v)...));
 
  191            data_map.emplace(&it->first, it);
 
  192            return std::make_pair(it, 
true);
 
  194        return std::make_pair(it, 
false);
 
  197    std::pair<iterator, bool> insert(
const value_type &v) {
 
  198        auto it = find(v.first);
 
  199        if (it == data.end()) {
 
  200            it = data.insert(data.end(), v);
 
  201            data_map.emplace(&it->first, it);
 
  202            return std::make_pair(it, 
true);
 
  204        return std::make_pair(it, 
false);
 
  209    std::pair<iterator, bool> insert(iterator pos, 
const value_type &v) {
 
  211        auto it = find(v.first);
 
  212        if (it == data.end()) {
 
  213            it = data.insert(pos, v);
 
  214            data_map.emplace(&it->first, it);
 
  215            return std::make_pair(it, 
true);
 
  217        return std::make_pair(it, 
false);
 
  219    template <
class InputIterator>
 
  220    void insert(InputIterator b, InputIterator e) {
 
  221        while (b != e) insert(*b++);
 
  226    template <
class InputIterator>
 
  227    void insert(iterator pos, InputIterator b, InputIterator e) {
 
  229        while (b != e) insert(pos, *b++);
 
  232    iterator erase(const_iterator pos) {
 
  233        auto it = data_map.find(&pos->first);
 
  234        assert(it != data_map.end());
 
  237        auto list_it = it->second;
 
  239        return data.erase(list_it);
 
  241    size_type erase(
const K &k) {
 
  243        if (it != data.end()) {
 
  251    template <
class Compare>
 
  252    void sort(Compare comp) {