1/////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2014-2014
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// See http://www.boost.org/libs/intrusive for documentation.
10//
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef BOOST_INTRUSIVE_DETAIL_REVERSE_ITERATOR_HPP
14#define BOOST_INTRUSIVE_DETAIL_REVERSE_ITERATOR_HPP
15
16#ifndef BOOST_CONFIG_HPP
17# include <boost/config.hpp>
18#endif
19
20#if defined(BOOST_HAS_PRAGMA_ONCE)
21# pragma once
22#endif
23
24#include <boost/intrusive/detail/config_begin.hpp>
25#include <boost/intrusive/detail/iterator.hpp>
26
27namespace boost {
28namespace intrusive {
29
30template<class It>
31class reverse_iterator
32{
33 public:
34 typedef typename boost::intrusive::iterator_traits<It>::pointer pointer;
35 typedef typename boost::intrusive::iterator_traits<It>::reference reference;
36 typedef typename boost::intrusive::iterator_traits<It>::difference_type difference_type;
37 typedef typename boost::intrusive::iterator_traits<It>::iterator_category iterator_category;
38 typedef typename boost::intrusive::iterator_traits<It>::value_type value_type;
39
40
41 typedef It iterator_type;
42
43 reverse_iterator()
44 : m_current() //Value initialization to achieve "null iterators" (N3644)
45 {}
46
47 explicit reverse_iterator(It r)
48 : m_current(r)
49 {}
50
51 template<class OtherIt>
52 reverse_iterator(const reverse_iterator<OtherIt>& r)
53 : m_current(r.base())
54 {}
55
56 It base() const
57 { return m_current; }
58
59 reference operator*() const
60 { It temp(m_current); --temp; return *temp; }
61
62 pointer operator->() const
63 { It temp(m_current); --temp; return temp.operator->(); }
64
65 reference operator[](difference_type off) const
66 { return this->m_current[-off-1]; }
67
68 reverse_iterator& operator++()
69 { --m_current; return *this; }
70
71 reverse_iterator operator++(int)
72 {
73 reverse_iterator temp = *this;
74 --m_current;
75 return temp;
76 }
77
78 reverse_iterator& operator--()
79 {
80 ++m_current;
81 return *this;
82 }
83
84 reverse_iterator operator--(int)
85 {
86 reverse_iterator temp(*this);
87 ++m_current;
88 return temp;
89 }
90
91 friend bool operator==(const reverse_iterator& l, const reverse_iterator& r)
92 { return l.m_current == r.m_current; }
93
94 friend bool operator!=(const reverse_iterator& l, const reverse_iterator& r)
95 { return l.m_current != r.m_current; }
96
97 friend bool operator<(const reverse_iterator& l, const reverse_iterator& r)
98 { return l.m_current > r.m_current; }
99
100 friend bool operator<=(const reverse_iterator& l, const reverse_iterator& r)
101 { return l.m_current >= r.m_current; }
102
103 friend bool operator>(const reverse_iterator& l, const reverse_iterator& r)
104 { return l.m_current < r.m_current; }
105
106 friend bool operator>=(const reverse_iterator& l, const reverse_iterator& r)
107 { return l.m_current <= r.m_current; }
108
109 reverse_iterator& operator+=(difference_type off)
110 { m_current -= off; return *this; }
111
112 friend reverse_iterator operator+(const reverse_iterator & l, difference_type off)
113 {
114 reverse_iterator tmp(l.m_current);
115 tmp.m_current -= off;
116 return tmp;
117 }
118
119 reverse_iterator& operator-=(difference_type off)
120 { m_current += off; return *this; }
121
122 friend reverse_iterator operator-(const reverse_iterator & l, difference_type off)
123 {
124 reverse_iterator tmp(l.m_current);
125 tmp.m_current += off;
126 return tmp;
127 }
128
129 friend difference_type operator-(const reverse_iterator& l, const reverse_iterator& r)
130 { return r.m_current - l.m_current; }
131
132 private:
133 It m_current; // the wrapped iterator
134};
135
136} //namespace intrusive {
137} //namespace boost {
138
139#include <boost/intrusive/detail/config_end.hpp>
140
141#endif //BOOST_INTRUSIVE_DETAIL_REVERSE_ITERATOR_HPP
142