-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystl_iterator.hpp
160 lines (126 loc) · 5.75 KB
/
mystl_iterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef _MYSTL_ITERATOR_H
#define _MYSTL_ITERATOR_H
#include "mystl_iterator_base.hpp"
namespace numb {
using numb::Iterator;
using numb::Iterator_traits;
// Its primary purpose is to convert an iterator that is
// not a class, e.g. a pointer, into an iterator that is a class.
// The _Container parameter exists solely so that different containers
// using this template can instantiate different types, even if the
// _Iterator parameter is the same.
template <typename _Iter, typename _Container>
class _Normal_iterator {
protected:
_Iter _M_current;
typedef Iterator_traits<_Iter> _traits_type;
public:
typedef _Iter iterator_type;
typedef typename _traits_type::iterator_category iterator_category;
typedef typename _traits_type::value_type value_type;
typedef typename _traits_type::difference_type difference_type;
typedef typename _traits_type::reference reference;
typedef typename _traits_type::pointer pointer;
_Normal_iterator() : _M_current(_Iter()) {}
explicit _Normal_iterator(const _Iter& i) : _M_current(i) { }
// Allow iterator to const iterator conversion
reference operator*() const
{ return *_M_current; }
pointer operator->() const
{ return _M_current; }
_Normal_iterator & operator++()
{
++_M_current;
return *this;
}
_Normal_iterator operator++(int)
{ return _Normal_iterator(_M_current++); }
_Normal_iterator& operator--()
{
--_M_current;
return *this;
}
_Normal_iterator operator--(int)
{ return _Normal_iterator(_M_current--); }
// Random Access iterator requirements
reference operator[](const difference_type& _n) const
{ return _M_current[_n]; }
_Normal_iterator& operator+=(const difference_type& _n)
{ _M_current += _n; return *this; }
_Normal_iterator operator+(const difference_type& _n) const
{ return _Normal_iterator(_M_current + _n); }
_Normal_iterator& operator-=(const difference_type& _n)
{ _M_current -= _n; return *this; }
_Normal_iterator operator-(const difference_type& _n) const
{ return _Normal_iterator(_M_current - _n); }
const _Iter& base() const
{ return _M_current; }
};
// Forward iterator requiements
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator==(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() == _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator==(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() == _rhs.base(); }
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator!=(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() != _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator!=(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() != _rhs.base(); }
// Random access iterator requiements
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator<(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() < _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator<(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() < _rhs.base(); }
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator>(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() > _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator>(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() > _rhs.base(); }
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator<=(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() <= _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator<=(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() <= _rhs.base(); }
template <typename _IterL, typename _IterR, typename _Container>
inline bool operator>=(const _Normal_iterator<_IterL, _Container> &_lhs,
const _Normal_iterator<_IterR, _Container> &_rhs)
{ return _lhs.base() >= _rhs.base(); }
template <typename _Iter, typename _Container>
inline bool operator>=(const _Normal_iterator<_Iter, _Container> &_lhs,
const _Normal_iterator<_Iter, _Container> &_rhs)
{ return _lhs.base() >= _rhs.base(); }
//accept mixed iterator/const_iterator
template <typename _IterL, typename _IterR, typename _Container>
inline typename _Normal_iterator<_IterL, _Container>::difference_type
operator-(const _Normal_iterator<_IterL, _Container>& _lhs,
const _Normal_iterator<_IterR, _Container>& _rhs)
{ return _lhs.base() - _rhs.base(); }
template <typename _Iter, typename _Container>
inline typename _Normal_iterator<_Iter, _Container>::difference_type
operator-(const _Normal_iterator<_Iter, _Container>& _lhs,
const _Normal_iterator<_Iter, _Container>& _rhs)
{ return _lhs.base() - _rhs.base(); }
template <typename _Iter, typename _Container>
inline typename _Normal_iterator<_Iter, _Container>::difference_type
operator+(const _Normal_iterator<_Iter, _Container>& _lhs,
const _Normal_iterator<_Iter, _Container>& _rhs)
{ return _lhs.base() - _rhs.base(); }
}
#endif