Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | #ifndef ITERATORS_H |
| 2 | #define ITERATORS_H |
| 3 | |
| 4 | #include <iterator> |
| 5 | |
| 6 | template <class It> |
| 7 | class input_iterator |
| 8 | { |
| 9 | It it_; |
| 10 | |
| 11 | template <class U> friend class input_iterator; |
| 12 | public: |
| 13 | typedef std::input_iterator_tag iterator_category; |
| 14 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 15 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 16 | typedef It pointer; |
| 17 | typedef typename std::iterator_traits<It>::reference reference; |
| 18 | |
| 19 | It base() const {return it_;} |
| 20 | |
| 21 | input_iterator() : it_() {} |
| 22 | explicit input_iterator(It it) : it_(it) {} |
| 23 | template <class U> |
| 24 | input_iterator(const input_iterator<U>& u) :it_(u.it_) {} |
| 25 | |
| 26 | reference operator*() const {return *it_;} |
| 27 | pointer operator->() const {return it_;} |
| 28 | |
| 29 | input_iterator& operator++() {++it_; return *this;} |
| 30 | input_iterator operator++(int) |
| 31 | {input_iterator tmp(*this); ++(*this); return tmp;} |
| 32 | |
| 33 | friend bool operator==(const input_iterator& x, const input_iterator& y) |
| 34 | {return x.it_ == y.it_;} |
| 35 | friend bool operator!=(const input_iterator& x, const input_iterator& y) |
| 36 | {return !(x == y);} |
| 37 | }; |
| 38 | |
| 39 | template <class T, class U> |
| 40 | inline |
| 41 | bool |
| 42 | operator==(const input_iterator<T>& x, const input_iterator<U>& y) |
| 43 | { |
| 44 | return x.base() == y.base(); |
| 45 | } |
| 46 | |
| 47 | template <class T, class U> |
| 48 | inline |
| 49 | bool |
| 50 | operator!=(const input_iterator<T>& x, const input_iterator<U>& y) |
| 51 | { |
| 52 | return !(x == y); |
| 53 | } |
| 54 | |
| 55 | template <class It> |
| 56 | class forward_iterator |
| 57 | { |
| 58 | It it_; |
| 59 | |
| 60 | template <class U> friend class forward_iterator; |
| 61 | public: |
| 62 | typedef std::forward_iterator_tag iterator_category; |
| 63 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 64 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 65 | typedef It pointer; |
| 66 | typedef typename std::iterator_traits<It>::reference reference; |
| 67 | |
| 68 | It base() const {return it_;} |
| 69 | |
| 70 | forward_iterator() : it_() {} |
| 71 | explicit forward_iterator(It it) : it_(it) {} |
| 72 | template <class U> |
| 73 | forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {} |
| 74 | |
| 75 | reference operator*() const {return *it_;} |
| 76 | pointer operator->() const {return it_;} |
| 77 | |
| 78 | forward_iterator& operator++() {++it_; return *this;} |
| 79 | forward_iterator operator++(int) |
| 80 | {forward_iterator tmp(*this); ++(*this); return tmp;} |
| 81 | |
| 82 | friend bool operator==(const forward_iterator& x, const forward_iterator& y) |
| 83 | {return x.it_ == y.it_;} |
| 84 | friend bool operator!=(const forward_iterator& x, const forward_iterator& y) |
| 85 | {return !(x == y);} |
| 86 | }; |
| 87 | |
| 88 | template <class T, class U> |
| 89 | inline |
| 90 | bool |
| 91 | operator==(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 92 | { |
| 93 | return x.base() == y.base(); |
| 94 | } |
| 95 | |
| 96 | template <class T, class U> |
| 97 | inline |
| 98 | bool |
| 99 | operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 100 | { |
| 101 | return !(x == y); |
| 102 | } |
| 103 | |
| 104 | template <class It> |
| 105 | class bidirectional_iterator |
| 106 | { |
| 107 | It it_; |
| 108 | |
| 109 | template <class U> friend class bidirectional_iterator; |
| 110 | public: |
| 111 | typedef std::bidirectional_iterator_tag iterator_category; |
| 112 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 113 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 114 | typedef It pointer; |
| 115 | typedef typename std::iterator_traits<It>::reference reference; |
| 116 | |
| 117 | It base() const {return it_;} |
| 118 | |
| 119 | bidirectional_iterator() : it_() {} |
| 120 | explicit bidirectional_iterator(It it) : it_(it) {} |
| 121 | template <class U> |
| 122 | bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {} |
| 123 | |
| 124 | reference operator*() const {return *it_;} |
| 125 | pointer operator->() const {return it_;} |
| 126 | |
| 127 | bidirectional_iterator& operator++() {++it_; return *this;} |
| 128 | bidirectional_iterator operator++(int) |
| 129 | {bidirectional_iterator tmp(*this); ++(*this); return tmp;} |
| 130 | |
| 131 | bidirectional_iterator& operator--() {--it_; return *this;} |
| 132 | bidirectional_iterator operator--(int) |
| 133 | {bidirectional_iterator tmp(*this); --(*this); return tmp;} |
| 134 | }; |
| 135 | |
| 136 | template <class T, class U> |
| 137 | inline |
| 138 | bool |
| 139 | operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y) |
| 140 | { |
| 141 | return x.base() == y.base(); |
| 142 | } |
| 143 | |
| 144 | template <class T, class U> |
| 145 | inline |
| 146 | bool |
| 147 | operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y) |
| 148 | { |
| 149 | return !(x == y); |
| 150 | } |
| 151 | |
| 152 | template <class It> |
| 153 | class random_access_iterator |
| 154 | { |
| 155 | It it_; |
| 156 | |
| 157 | template <class U> friend class random_access_iterator; |
| 158 | public: |
| 159 | typedef std::random_access_iterator_tag iterator_category; |
| 160 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 161 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 162 | typedef It pointer; |
| 163 | typedef typename std::iterator_traits<It>::reference reference; |
| 164 | |
| 165 | It base() const {return it_;} |
| 166 | |
| 167 | random_access_iterator() : it_() {} |
| 168 | explicit random_access_iterator(It it) : it_(it) {} |
| 169 | template <class U> |
| 170 | random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {} |
| 171 | |
| 172 | reference operator*() const {return *it_;} |
| 173 | pointer operator->() const {return it_;} |
| 174 | |
| 175 | random_access_iterator& operator++() {++it_; return *this;} |
| 176 | random_access_iterator operator++(int) |
| 177 | {random_access_iterator tmp(*this); ++(*this); return tmp;} |
| 178 | |
| 179 | random_access_iterator& operator--() {--it_; return *this;} |
| 180 | random_access_iterator operator--(int) |
| 181 | {random_access_iterator tmp(*this); --(*this); return tmp;} |
| 182 | |
| 183 | random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;} |
| 184 | random_access_iterator operator+(difference_type n) const |
| 185 | {random_access_iterator tmp(*this); tmp += n; return tmp;} |
| 186 | friend random_access_iterator operator+(difference_type n, random_access_iterator x) |
| 187 | {x += n; return x;} |
| 188 | random_access_iterator& operator-=(difference_type n) {return *this += -n;} |
| 189 | random_access_iterator operator-(difference_type n) const |
| 190 | {random_access_iterator tmp(*this); tmp -= n; return tmp;} |
| 191 | |
| 192 | reference operator[](difference_type n) const {return it_[n];} |
| 193 | }; |
| 194 | |
| 195 | template <class T, class U> |
| 196 | inline |
| 197 | bool |
| 198 | operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 199 | { |
| 200 | return x.base() == y.base(); |
| 201 | } |
| 202 | |
| 203 | template <class T, class U> |
| 204 | inline |
| 205 | bool |
| 206 | operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 207 | { |
| 208 | return !(x == y); |
| 209 | } |
| 210 | |
| 211 | template <class T, class U> |
| 212 | inline |
| 213 | bool |
| 214 | operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 215 | { |
| 216 | return x.base() < y.base(); |
| 217 | } |
| 218 | |
| 219 | template <class T, class U> |
| 220 | inline |
| 221 | bool |
| 222 | operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 223 | { |
| 224 | return !(y < x); |
| 225 | } |
| 226 | |
| 227 | template <class T, class U> |
| 228 | inline |
| 229 | bool |
| 230 | operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 231 | { |
| 232 | return y < x; |
| 233 | } |
| 234 | |
| 235 | template <class T, class U> |
| 236 | inline |
| 237 | bool |
| 238 | operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 239 | { |
| 240 | return !(x < y); |
| 241 | } |
| 242 | |
| 243 | template <class T, class U> |
| 244 | inline |
| 245 | typename std::iterator_traits<T>::difference_type |
| 246 | operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 247 | { |
| 248 | return x.base() - y.base(); |
| 249 | } |
| 250 | |
Howard Hinnant | 8f2f7e7 | 2010-08-22 00:15:28 +0000 | [diff] [blame^] | 251 | #endif // ITERATORS_H |