34    typedef COMP key_compare;
 
   35    typedef COMP value_compare;
 
   36    typedef ALLOC allocator_type;
 
   37    typedef const T &reference;
 
   38    typedef const T &const_reference;
 
   41    typedef std::list<T, ALLOC> list_type;
 
   42    typedef typename list_type::iterator list_iterator;
 
   50    typedef typename list_type::const_iterator iterator;
 
   51    typedef typename list_type::const_iterator const_iterator;
 
   52    typedef std::reverse_iterator<iterator> reverse_iterator;
 
   53    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
   58        bool operator()(
const T *a, 
const T *b)
 const { 
return comp(*a, *b); }
 
   60    using map_alloc = 
typename std::allocator_traits<ALLOC>::template rebind_alloc<
 
   61        std::pair<const T *const, list_iterator>>;
 
   62    using map_type = std::map<const T *, list_iterator, mapcmp, map_alloc>;
 
   64    void init_data_map() {
 
   66        for (
auto it = data.begin(); it != data.end(); it++) data_map.emplace(&*it, it);
 
   68    iterator tr_iter(
typename map_type::iterator i) {
 
   69        if (i == data_map.end()) 
return data.end();
 
   72    const_iterator tr_iter(
typename map_type::const_iterator i)
 const {
 
   73        if (i == data_map.end()) 
return data.end();
 
   78    typedef typename map_type::size_type size_type;
 
   79    class sorted_iterator : 
public std::iterator<std::bidirectional_iterator_tag, T> {
 
   81        typename map_type::const_iterator iter;
 
   86        const T &operator*()
 const { 
return *iter->first; }
 
   87        const T *operator->()
 const { 
return iter->first; }
 
  106        bool operator==(
const sorted_iterator i)
 const { 
return iter == i.iter; }
 
  107        bool operator!=(
const sorted_iterator i)
 const { 
return iter != i.iter; }
 
 
  112    ordered_set(std::initializer_list<T> init) : data(init) { init_data_map(); }
 
  113    template <
typename InputIt>
 
  114    ordered_set(InputIt first, InputIt last) : data(first, last) {
 
  124    bool operator==(
const ordered_set &a)
 const { 
return data == a.data; }
 
  125    bool operator!=(
const ordered_set &a)
 const { 
return data != a.data; }
 
  131        auto it = a.data_map.begin();
 
  132        for (
auto &el : data_map) {
 
  133            if (it == a.data_map.end()) 
return false;
 
  134            if (mapcmp()(el.first, it->first)) 
return true;
 
  135            if (mapcmp()(it->first, el.first)) 
return false;
 
  138        return it != a.data_map.end();
 
  143    iterator begin() noexcept { 
return data.begin(); }
 
  144    const_iterator begin() const noexcept { 
return data.begin(); }
 
  145    iterator end() noexcept { 
return data.end(); }
 
  146    const_iterator end() const noexcept { 
return data.end(); }
 
  147    reverse_iterator rbegin() noexcept { 
return data.rbegin(); }
 
  148    const_reverse_iterator rbegin() const noexcept { 
return data.rbegin(); }
 
  149    reverse_iterator rend() noexcept { 
return data.rend(); }
 
  150    const_reverse_iterator rend() const noexcept { 
return data.rend(); }
 
  151    const_iterator cbegin() const noexcept { 
return data.cbegin(); }
 
  152    const_iterator cend() const noexcept { 
return data.cend(); }
 
  153    const_reverse_iterator crbegin() const noexcept { 
return data.crbegin(); }
 
  154    const_reverse_iterator crend() const noexcept { 
return data.crend(); }
 
  155    sorted_iterator sorted_begin() const noexcept { 
return data_map.begin(); }
 
  156    sorted_iterator sorted_end() const noexcept { 
return data_map.end(); }
 
  158    reference front() const noexcept { 
return *data.begin(); }
 
  159    reference back() const noexcept { 
return *data.rbegin(); }
 
  161    bool empty() const noexcept { 
return data.empty(); }
 
  162    size_type size() const noexcept { 
return data_map.size(); }
 
  163    size_type max_size() const noexcept { 
return data_map.max_size(); }
 
  169    iterator find(
const T &a) { 
return tr_iter(data_map.find(&a)); }
 
  170    const_iterator find(
const T &a)
 const { 
return tr_iter(data_map.find(&a)); }
 
  171    size_type count(
const T &a)
 const { 
return data_map.count(&a); }
 
  172    iterator upper_bound(
const T &a) { 
return tr_iter(data_map.upper_bound(&a)); }
 
  173    const_iterator upper_bound(
const T &a)
 const { 
return tr_iter(data_map.upper_bound(&a)); }
 
  174    iterator lower_bound(
const T &a) { 
return tr_iter(data_map.lower_bound(&a)); }
 
  175    const_iterator lower_bound(
const T &a)
 const { 
return tr_iter(data_map.lower_bound(&a)); }
 
  177    std::pair<iterator, bool> insert(
const T &v) {
 
  178        iterator it = find(v);
 
  179        if (it == data.end()) {
 
  180            list_iterator it = data.insert(data.end(), v);
 
  181            data_map.emplace(&*it, it);
 
  182            return std::make_pair(it, 
true);
 
  184        return std::make_pair(it, 
false);
 
  186    std::pair<iterator, bool> insert(T &&v) {
 
  187        iterator it = find(v);
 
  188        if (it == data.end()) {
 
  189            list_iterator it = data.insert(data.end(), std::move(v));
 
  190            data_map.emplace(&*it, it);
 
  191            return std::make_pair(it, 
true);
 
  193        return std::make_pair(it, 
false);
 
  195    void insert(ordered_set::const_iterator begin, ordered_set::const_iterator end) {
 
  196        for (
auto it = begin; it != end; ++it) insert(*it);
 
  198    iterator insert(const_iterator pos, 
const T &v) {
 
  199        iterator it = find(v);
 
  200        if (it == data.end()) {
 
  201            list_iterator it = data.insert(pos, v);
 
  202            data_map.emplace(&*it, it);
 
  207    iterator insert(const_iterator pos, T &&v) {
 
  208        iterator it = find(v);
 
  209        if (it == data.end()) {
 
  210            list_iterator it = data.insert(pos, std::move(v));
 
  211            data_map.emplace(&*it, it);
 
  217    void push_back(
const T &v) {
 
  218        iterator it = find(v);
 
  219        if (it == data.end()) {
 
  220            list_iterator it = data.insert(data.end(), v);
 
  221            data_map.emplace(&*it, it);
 
  223            data.splice(data.end(), data, it);
 
  226    void push_back(T &&v) {
 
  227        iterator it = find(v);
 
  228        if (it == data.end()) {
 
  229            list_iterator it = data.insert(data.end(), std::move(v));
 
  230            data_map.emplace(&*it, it);
 
  232            data.splice(data.end(), data, it);
 
  236    template <
class... Args>
 
  237    std::pair<iterator, bool> emplace(Args &&...args) {
 
  238        auto it = data.emplace(data.end(), std::forward<Args>(args)...);
 
  239        auto old = find(*it);
 
  240        if (old == data.end()) {
 
  241            data_map.emplace(&*it, it);
 
  242            return std::make_pair(it, 
true);
 
  245            return std::make_pair(old, 
false);
 
  249    template <
class... Args>
 
  250    std::pair<iterator, bool> emplace_back(Args &&...args) {
 
  251        auto it = data.emplace(data.end(), std::forward<Args>(args)...);
 
  252        auto old = find(*it);
 
  253        if (old != data.end()) {
 
  256        data_map.emplace(&*it, it);
 
  257        return std::make_pair(it, 
true);
 
  260    iterator erase(const_iterator pos) {
 
  261        auto it = data_map.find(&*pos);
 
  262        assert(it != data_map.end());
 
  265        auto list_it = it->second;
 
  267        return data.erase(list_it);
 
  269    size_type erase(
const T &v) {
 
  271        if (it != data.end()) {