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 output_iterator |
| 8 | { |
| 9 | It it_; |
| 10 | |
| 11 | template <class U> friend class output_iterator; |
| 12 | public: |
| 13 | typedef std::output_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 | explicit output_iterator(It it) : it_(it) {} |
| 22 | template <class U> |
| 23 | output_iterator(const output_iterator<U>& u) :it_(u.it_) {} |
| 24 | |
| 25 | reference operator*() const {return *it_;} |
| 26 | |
| 27 | output_iterator& operator++() {++it_; return *this;} |
| 28 | output_iterator operator++(int) |
| 29 | {output_iterator tmp(*this); ++(*this); return tmp;} |
| 30 | }; |
| 31 | |
| 32 | template <class Iter> |
| 33 | inline |
| 34 | Iter |
| 35 | base(output_iterator<Iter> i) |
| 36 | { |
| 37 | return i.base(); |
| 38 | } |
| 39 | |
| 40 | template <class It> |
| 41 | class input_iterator |
| 42 | { |
| 43 | It it_; |
| 44 | |
| 45 | template <class U> friend class input_iterator; |
| 46 | public: |
| 47 | typedef std::input_iterator_tag iterator_category; |
| 48 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 49 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 50 | typedef It pointer; |
| 51 | typedef typename std::iterator_traits<It>::reference reference; |
| 52 | |
| 53 | It base() const {return it_;} |
| 54 | |
| 55 | explicit input_iterator(It it) : it_(it) {} |
| 56 | template <class U> |
| 57 | input_iterator(const input_iterator<U>& u) :it_(u.it_) {} |
| 58 | |
| 59 | reference operator*() const {return *it_;} |
| 60 | pointer operator->() const {return it_;} |
| 61 | |
| 62 | input_iterator& operator++() {++it_; return *this;} |
| 63 | input_iterator operator++(int) |
| 64 | {input_iterator tmp(*this); ++(*this); return tmp;} |
| 65 | }; |
| 66 | |
| 67 | template <class T, class U> |
| 68 | inline |
| 69 | bool |
| 70 | operator==(const input_iterator<T>& x, const input_iterator<U>& y) |
| 71 | { |
| 72 | return x.base() == y.base(); |
| 73 | } |
| 74 | |
| 75 | template <class T, class U> |
| 76 | inline |
| 77 | bool |
| 78 | operator!=(const input_iterator<T>& x, const input_iterator<U>& y) |
| 79 | { |
| 80 | return !(x == y); |
| 81 | } |
| 82 | |
| 83 | template <class Iter> |
| 84 | inline |
| 85 | Iter |
| 86 | base(input_iterator<Iter> i) |
| 87 | { |
| 88 | return i.base(); |
| 89 | } |
| 90 | |
| 91 | template <class It> |
| 92 | class forward_iterator |
| 93 | { |
| 94 | It it_; |
| 95 | |
| 96 | template <class U> friend class forward_iterator; |
| 97 | public: |
| 98 | typedef std::forward_iterator_tag iterator_category; |
| 99 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 100 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 101 | typedef It pointer; |
| 102 | typedef typename std::iterator_traits<It>::reference reference; |
| 103 | |
| 104 | It base() const {return it_;} |
| 105 | |
| 106 | forward_iterator() : it_() {} |
| 107 | explicit forward_iterator(It it) : it_(it) {} |
| 108 | template <class U> |
| 109 | forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {} |
| 110 | |
| 111 | reference operator*() const {return *it_;} |
| 112 | pointer operator->() const {return it_;} |
| 113 | |
| 114 | forward_iterator& operator++() {++it_; return *this;} |
| 115 | forward_iterator operator++(int) |
| 116 | {forward_iterator tmp(*this); ++(*this); return tmp;} |
| 117 | }; |
| 118 | |
| 119 | template <class T, class U> |
| 120 | inline |
| 121 | bool |
| 122 | operator==(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 123 | { |
| 124 | return x.base() == y.base(); |
| 125 | } |
| 126 | |
| 127 | template <class T, class U> |
| 128 | inline |
| 129 | bool |
| 130 | operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 131 | { |
| 132 | return !(x == y); |
| 133 | } |
| 134 | |
| 135 | template <class Iter> |
| 136 | inline |
| 137 | Iter |
| 138 | base(forward_iterator<Iter> i) |
| 139 | { |
| 140 | return i.base(); |
| 141 | } |
| 142 | |
| 143 | template <class It> |
| 144 | class bidirectional_iterator |
| 145 | { |
| 146 | It it_; |
| 147 | |
| 148 | template <class U> friend class bidirectional_iterator; |
| 149 | public: |
| 150 | typedef std::bidirectional_iterator_tag iterator_category; |
| 151 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 152 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 153 | typedef It pointer; |
| 154 | typedef typename std::iterator_traits<It>::reference reference; |
| 155 | |
| 156 | It base() const {return it_;} |
| 157 | |
| 158 | bidirectional_iterator() : it_() {} |
| 159 | explicit bidirectional_iterator(It it) : it_(it) {} |
| 160 | template <class U> |
| 161 | bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {} |
| 162 | |
| 163 | reference operator*() const {return *it_;} |
| 164 | pointer operator->() const {return it_;} |
| 165 | |
| 166 | bidirectional_iterator& operator++() {++it_; return *this;} |
| 167 | bidirectional_iterator operator++(int) |
| 168 | {bidirectional_iterator tmp(*this); ++(*this); return tmp;} |
| 169 | |
| 170 | bidirectional_iterator& operator--() {--it_; return *this;} |
| 171 | bidirectional_iterator operator--(int) |
| 172 | {bidirectional_iterator tmp(*this); --(*this); return tmp;} |
| 173 | }; |
| 174 | |
| 175 | template <class T, class U> |
| 176 | inline |
| 177 | bool |
| 178 | operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y) |
| 179 | { |
| 180 | return x.base() == y.base(); |
| 181 | } |
| 182 | |
| 183 | template <class T, class U> |
| 184 | inline |
| 185 | bool |
| 186 | operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y) |
| 187 | { |
| 188 | return !(x == y); |
| 189 | } |
| 190 | |
| 191 | template <class Iter> |
| 192 | inline |
| 193 | Iter |
| 194 | base(bidirectional_iterator<Iter> i) |
| 195 | { |
| 196 | return i.base(); |
| 197 | } |
| 198 | |
| 199 | template <class It> |
| 200 | class random_access_iterator |
| 201 | { |
| 202 | It it_; |
| 203 | |
| 204 | template <class U> friend class random_access_iterator; |
| 205 | public: |
| 206 | typedef std::random_access_iterator_tag iterator_category; |
| 207 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 208 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 209 | typedef It pointer; |
| 210 | typedef typename std::iterator_traits<It>::reference reference; |
| 211 | |
| 212 | It base() const {return it_;} |
| 213 | |
| 214 | random_access_iterator() : it_() {} |
| 215 | explicit random_access_iterator(It it) : it_(it) {} |
| 216 | template <class U> |
| 217 | random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {} |
| 218 | |
| 219 | reference operator*() const {return *it_;} |
| 220 | pointer operator->() const {return it_;} |
| 221 | |
| 222 | random_access_iterator& operator++() {++it_; return *this;} |
| 223 | random_access_iterator operator++(int) |
| 224 | {random_access_iterator tmp(*this); ++(*this); return tmp;} |
| 225 | |
| 226 | random_access_iterator& operator--() {--it_; return *this;} |
| 227 | random_access_iterator operator--(int) |
| 228 | {random_access_iterator tmp(*this); --(*this); return tmp;} |
| 229 | |
| 230 | random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;} |
| 231 | random_access_iterator operator+(difference_type n) const |
| 232 | {random_access_iterator tmp(*this); tmp += n; return tmp;} |
| 233 | friend random_access_iterator operator+(difference_type n, random_access_iterator x) |
| 234 | {x += n; return x;} |
| 235 | random_access_iterator& operator-=(difference_type n) {return *this += -n;} |
| 236 | random_access_iterator operator-(difference_type n) const |
| 237 | {random_access_iterator tmp(*this); tmp -= n; return tmp;} |
| 238 | |
| 239 | reference operator[](difference_type n) const {return it_[n];} |
| 240 | }; |
| 241 | |
| 242 | template <class T, class U> |
| 243 | inline |
| 244 | bool |
| 245 | operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 246 | { |
| 247 | return x.base() == y.base(); |
| 248 | } |
| 249 | |
| 250 | template <class T, class U> |
| 251 | inline |
| 252 | bool |
| 253 | operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 254 | { |
| 255 | return !(x == y); |
| 256 | } |
| 257 | |
| 258 | template <class T, class U> |
| 259 | inline |
| 260 | bool |
| 261 | operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 262 | { |
| 263 | return x.base() < y.base(); |
| 264 | } |
| 265 | |
| 266 | template <class T, class U> |
| 267 | inline |
| 268 | bool |
| 269 | operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 270 | { |
| 271 | return !(y < x); |
| 272 | } |
| 273 | |
| 274 | template <class T, class U> |
| 275 | inline |
| 276 | bool |
| 277 | operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 278 | { |
| 279 | return y < x; |
| 280 | } |
| 281 | |
| 282 | template <class T, class U> |
| 283 | inline |
| 284 | bool |
| 285 | operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 286 | { |
| 287 | return !(x < y); |
| 288 | } |
| 289 | |
| 290 | template <class T, class U> |
| 291 | inline |
| 292 | typename std::iterator_traits<T>::difference_type |
| 293 | operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 294 | { |
| 295 | return x.base() - y.base(); |
| 296 | } |
| 297 | |
| 298 | template <class Iter> |
| 299 | inline |
| 300 | Iter |
| 301 | base(random_access_iterator<Iter> i) |
| 302 | { |
| 303 | return i.base(); |
| 304 | } |
| 305 | |
| 306 | template <class Iter> |
| 307 | inline |
| 308 | Iter |
| 309 | base(Iter i) |
| 310 | { |
| 311 | return i; |
| 312 | } |
| 313 | |
Howard Hinnant | 664ae81 | 2010-08-22 00:08:10 +0000 | [diff] [blame] | 314 | #endif // ITERATORS_H |