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