Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #pragma once
  2. #include <list>
  3.  
  4. template<typename T>
  5. class tBidirectionalList : public std::list<T>
  6. {
  7. public:
  8.     using std::list<T>::insert;
  9.     using std::list<T>::erase;
  10.     tBidirectionalList(std::list<T>&& rhs) : std::list<T>(std::move(rhs)) {}
  11.  
  12.     class const_back_iterator;
  13.  
  14.     class back_iterator
  15.     {
  16.     public:
  17.         typedef back_iterator self_type;
  18.         typedef T value_type;
  19.         typedef T& reference;
  20.         typedef T* pointer;
  21.         typedef std::bidirectional_iterator_tag iterator_category;
  22.         typedef int difference_type;
  23.         back_iterator(iterator it, tBidirectionalList * list) : xIt(it), xList(list) {}
  24.         self_type operator++() {
  25.             self_type i = *this;
  26.             assertion(xList);
  27.             if (xIt == xList->begin())
  28.                 xIt = xList->end();
  29.             else
  30.                 --xIt;
  31.             return i;
  32.         }
  33.         self_type operator++(int) {
  34.             assertion(xList);
  35.             if (xIt == xList->begin())
  36.                 xIt = xList->end();
  37.             else
  38.                 --xIt;
  39.             return *this;
  40.         }
  41.         self_type operator--()
  42.         {
  43.             self_type i = *this;
  44.             assertion(xList);
  45.             if (xIt == xList->end())
  46.                 xIt = xList->begin();
  47.             else
  48.                 ++xIt;
  49.             return i;
  50.         }
  51.         self_type operator--(int)
  52.         {
  53.             assertion(xList);
  54.             if (xIt == xList->end())
  55.                 xIt = xList->begin();
  56.             else
  57.                 ++xIt;
  58.             return *this;
  59.         }
  60.         reference operator*() { return *xIt; }
  61.         pointer operator->() { return &*xIt; }
  62.         bool operator==(const self_type& rhs) { return xIt == rhs.xIt; }
  63.         bool operator!=(const self_type& rhs) { return xIt != rhs.xIt; }
  64.         iterator base() { return xIt; }
  65.         operator const_back_iterator() const { return const_back_iterator(xIt, xList); }
  66.     private:
  67.         iterator xIt;
  68.         tBidirectionalList* xList;
  69.     };
  70.  
  71.     class const_back_iterator
  72.     {
  73.     public:
  74.         typedef const_back_iterator self_type;
  75.         typedef T value_type;
  76.         typedef T& reference;
  77.         typedef T* pointer;
  78.         typedef std::bidirectional_iterator_tag iterator_category;
  79.         typedef int difference_type;
  80.         const_back_iterator(const_iterator it, tBidirectionalList const * list) : xIt(it), xList(list) {}
  81.         self_type operator++()
  82.         {
  83.             self_type i = *this;
  84.             assertion(xList);
  85.             if (xIt == xList->begin())
  86.                 xIt = xList->end();
  87.             else
  88.                 --xIt;
  89.             return i;
  90.         }
  91.         self_type operator++(int)
  92.         {
  93.             assertion(xList);
  94.             if (xIt == xList->begin())
  95.                 xIt = xList->end();
  96.             else
  97.                 --xIt;
  98.             return *this;
  99.         }
  100.         self_type operator--()
  101.         {
  102.             self_type i = *this;
  103.             assertion(xList);
  104.             if (xIt == xList->end())
  105.                 xIt = xList->begin();
  106.             else
  107.                 ++xIt;
  108.             return i;
  109.         }
  110.         self_type operator--(int)
  111.         {
  112.             assertion(xList);
  113.             if (xIt == xList->end())
  114.                 xIt = xList->begin();
  115.             else
  116.                 ++xIt;
  117.             return *this;
  118.         }
  119.         const reference operator*() { return *xIt; }
  120.         const pointer operator->() { return &*xIt; }
  121.         bool operator==(const self_type& rhs) { return xIt == rhs.xIt; }
  122.         bool operator!=(const self_type& rhs) { return xIt != rhs.xIt; }
  123.         const_iterator base() { return xIt; }
  124.     private:
  125.         const_iterator xIt;
  126.         tBidirectionalList const * xList;
  127.     };
  128.  
  129.     back_iterator bbegin()
  130.     {
  131.         if (empty())
  132.             return back_iterator(end(), this);
  133.         return back_iterator(std::prev(end()), this);
  134.     }
  135.     back_iterator bend()
  136.     {
  137.         return back_iterator(end(), this);
  138.     }
  139.     const_back_iterator cbbegin()
  140.     {
  141.         if (empty())
  142.             return const_back_iterator(end(), this);
  143.         return const_back_iterator(std::prev(end()), this);
  144.     }
  145.     const_back_iterator cbend()
  146.     {
  147.         return const_back_iterator(end(), this);
  148.     }
  149.  
  150.     back_iterator erase(const_back_iterator position)
  151.     {
  152.         iterator tmp = erase(position.base());
  153.         if (tmp == end() && empty() || tmp == begin())
  154.             return bend();
  155.         return back_iterator(--tmp, this);
  156.     }
  157.  
  158.     back_iterator insert(const_back_iterator position, const value_type& val)
  159.     {
  160.         const_back_iterator tmp = position.base();
  161.         if (tmp == end())
  162.             insert(begin(), val);
  163.         else
  164.             insert(++tmp, val);
  165.     }
  166.     back_iterator insert(const_back_iterator position, size_type n, const value_type& val)
  167.     {
  168.         for (size_type i = 0; i < n; i++)
  169.             insert(position, val);
  170.     }
  171.     template <class InputIterator>
  172.     back_iterator insert(const_back_iterator position, InputIterator begin, InputIterator end)
  173.     {
  174.         while (begin != end)
  175.         {
  176.             insert(position, *begin);
  177.             ++begin;
  178.         }
  179.     }
  180.     back_iterator insert(const_back_iterator position, value_type&& val)
  181.     {
  182.         const_back_iterator tmp = position.base();
  183.         if (tmp == end())
  184.             insert(begin(), std::move(val));
  185.         else
  186.             insert(++tmp, std::move(val));
  187.     }
  188.     back_iterator insert(const_back_iterator position, std::initializer_list<value_type> il)
  189.     {
  190.         insert(position, il.begin(), il.end());
  191.     }
  192. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement