Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <list>
- template<typename T>
- class tBidirectionalList : public std::list<T>
- {
- public:
- using std::list<T>::insert;
- using std::list<T>::erase;
- tBidirectionalList(std::list<T>&& rhs) : std::list<T>(std::move(rhs)) {}
- class const_back_iterator;
- class back_iterator
- {
- public:
- typedef back_iterator self_type;
- typedef T value_type;
- typedef T& reference;
- typedef T* pointer;
- typedef std::bidirectional_iterator_tag iterator_category;
- typedef int difference_type;
- back_iterator(iterator it, tBidirectionalList * list) : xIt(it), xList(list) {}
- self_type operator++() {
- self_type i = *this;
- assertion(xList);
- if (xIt == xList->begin())
- xIt = xList->end();
- else
- --xIt;
- return i;
- }
- self_type operator++(int) {
- assertion(xList);
- if (xIt == xList->begin())
- xIt = xList->end();
- else
- --xIt;
- return *this;
- }
- self_type operator--()
- {
- self_type i = *this;
- assertion(xList);
- if (xIt == xList->end())
- xIt = xList->begin();
- else
- ++xIt;
- return i;
- }
- self_type operator--(int)
- {
- assertion(xList);
- if (xIt == xList->end())
- xIt = xList->begin();
- else
- ++xIt;
- return *this;
- }
- reference operator*() { return *xIt; }
- pointer operator->() { return &*xIt; }
- bool operator==(const self_type& rhs) { return xIt == rhs.xIt; }
- bool operator!=(const self_type& rhs) { return xIt != rhs.xIt; }
- iterator base() { return xIt; }
- operator const_back_iterator() const { return const_back_iterator(xIt, xList); }
- private:
- iterator xIt;
- tBidirectionalList* xList;
- };
- class const_back_iterator
- {
- public:
- typedef const_back_iterator self_type;
- typedef T value_type;
- typedef T& reference;
- typedef T* pointer;
- typedef std::bidirectional_iterator_tag iterator_category;
- typedef int difference_type;
- const_back_iterator(const_iterator it, tBidirectionalList const * list) : xIt(it), xList(list) {}
- self_type operator++()
- {
- self_type i = *this;
- assertion(xList);
- if (xIt == xList->begin())
- xIt = xList->end();
- else
- --xIt;
- return i;
- }
- self_type operator++(int)
- {
- assertion(xList);
- if (xIt == xList->begin())
- xIt = xList->end();
- else
- --xIt;
- return *this;
- }
- self_type operator--()
- {
- self_type i = *this;
- assertion(xList);
- if (xIt == xList->end())
- xIt = xList->begin();
- else
- ++xIt;
- return i;
- }
- self_type operator--(int)
- {
- assertion(xList);
- if (xIt == xList->end())
- xIt = xList->begin();
- else
- ++xIt;
- return *this;
- }
- const reference operator*() { return *xIt; }
- const pointer operator->() { return &*xIt; }
- bool operator==(const self_type& rhs) { return xIt == rhs.xIt; }
- bool operator!=(const self_type& rhs) { return xIt != rhs.xIt; }
- const_iterator base() { return xIt; }
- private:
- const_iterator xIt;
- tBidirectionalList const * xList;
- };
- back_iterator bbegin()
- {
- if (empty())
- return back_iterator(end(), this);
- return back_iterator(std::prev(end()), this);
- }
- back_iterator bend()
- {
- return back_iterator(end(), this);
- }
- const_back_iterator cbbegin()
- {
- if (empty())
- return const_back_iterator(end(), this);
- return const_back_iterator(std::prev(end()), this);
- }
- const_back_iterator cbend()
- {
- return const_back_iterator(end(), this);
- }
- back_iterator erase(const_back_iterator position)
- {
- iterator tmp = erase(position.base());
- if (tmp == end() && empty() || tmp == begin())
- return bend();
- return back_iterator(--tmp, this);
- }
- back_iterator insert(const_back_iterator position, const value_type& val)
- {
- const_back_iterator tmp = position.base();
- if (tmp == end())
- insert(begin(), val);
- else
- insert(++tmp, val);
- }
- back_iterator insert(const_back_iterator position, size_type n, const value_type& val)
- {
- for (size_type i = 0; i < n; i++)
- insert(position, val);
- }
- template <class InputIterator>
- back_iterator insert(const_back_iterator position, InputIterator begin, InputIterator end)
- {
- while (begin != end)
- {
- insert(position, *begin);
- ++begin;
- }
- }
- back_iterator insert(const_back_iterator position, value_type&& val)
- {
- const_back_iterator tmp = position.base();
- if (tmp == end())
- insert(begin(), std::move(val));
- else
- insert(++tmp, std::move(val));
- }
- back_iterator insert(const_back_iterator position, std::initializer_list<value_type> il)
- {
- insert(position, il.begin(), il.end());
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement