Marshall Clow | 354d39c | 2014-01-16 16:58:45 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 10 | #ifndef ITERATORS_H |
| 11 | #define ITERATORS_H |
| 12 | |
| 13 | #include <iterator> |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 14 | #include <stdexcept> |
Eric Fiselier | 71c425f | 2016-05-27 23:43:29 +0000 | [diff] [blame] | 15 | #include <cstddef> |
Marshall Clow | 2929866 | 2014-09-17 04:09:35 +0000 | [diff] [blame] | 16 | #include <cassert> |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 17 | |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 18 | #include "test_macros.h" |
| 19 | |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 20 | #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
| 21 | #define DELETE_FUNCTION = delete |
| 22 | #else |
| 23 | #define DELETE_FUNCTION |
| 24 | #endif |
| 25 | |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 26 | template <class It> |
Howard Hinnant | 48b242a | 2010-08-14 18:14:02 +0000 | [diff] [blame] | 27 | class output_iterator |
| 28 | { |
| 29 | It it_; |
| 30 | |
| 31 | template <class U> friend class output_iterator; |
| 32 | public: |
| 33 | typedef std::output_iterator_tag iterator_category; |
Marshall Clow | 4476100 | 2013-01-09 17:20:02 +0000 | [diff] [blame] | 34 | typedef void value_type; |
Howard Hinnant | 48b242a | 2010-08-14 18:14:02 +0000 | [diff] [blame] | 35 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 36 | typedef It pointer; |
| 37 | typedef typename std::iterator_traits<It>::reference reference; |
| 38 | |
| 39 | It base() const {return it_;} |
| 40 | |
Marshall Clow | cf1589f | 2013-01-03 02:29:29 +0000 | [diff] [blame] | 41 | output_iterator () {} |
Howard Hinnant | 48b242a | 2010-08-14 18:14:02 +0000 | [diff] [blame] | 42 | explicit output_iterator(It it) : it_(it) {} |
| 43 | template <class U> |
| 44 | output_iterator(const output_iterator<U>& u) :it_(u.it_) {} |
| 45 | |
| 46 | reference operator*() const {return *it_;} |
| 47 | |
| 48 | output_iterator& operator++() {++it_; return *this;} |
| 49 | output_iterator operator++(int) |
| 50 | {output_iterator tmp(*this); ++(*this); return tmp;} |
Eric Fiselier | 910285b | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 51 | |
| 52 | template <class T> |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 53 | void operator,(T const &) DELETE_FUNCTION; |
Howard Hinnant | 48b242a | 2010-08-14 18:14:02 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | template <class It> |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 57 | class input_iterator |
| 58 | { |
| 59 | It it_; |
| 60 | |
| 61 | template <class U> friend class input_iterator; |
| 62 | public: |
| 63 | typedef std::input_iterator_tag iterator_category; |
| 64 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 65 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 66 | typedef It pointer; |
| 67 | typedef typename std::iterator_traits<It>::reference reference; |
| 68 | |
| 69 | It base() const {return it_;} |
| 70 | |
| 71 | input_iterator() : it_() {} |
| 72 | explicit input_iterator(It it) : it_(it) {} |
| 73 | template <class U> |
| 74 | input_iterator(const input_iterator<U>& u) :it_(u.it_) {} |
| 75 | |
| 76 | reference operator*() const {return *it_;} |
| 77 | pointer operator->() const {return it_;} |
| 78 | |
| 79 | input_iterator& operator++() {++it_; return *this;} |
| 80 | input_iterator operator++(int) |
| 81 | {input_iterator tmp(*this); ++(*this); return tmp;} |
| 82 | |
| 83 | friend bool operator==(const input_iterator& x, const input_iterator& y) |
| 84 | {return x.it_ == y.it_;} |
| 85 | friend bool operator!=(const input_iterator& x, const input_iterator& y) |
| 86 | {return !(x == y);} |
Eric Fiselier | 910285b | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 87 | |
| 88 | template <class T> |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 89 | void operator,(T const &) DELETE_FUNCTION; |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | template <class T, class U> |
| 93 | inline |
| 94 | bool |
| 95 | operator==(const input_iterator<T>& x, const input_iterator<U>& y) |
| 96 | { |
| 97 | return x.base() == y.base(); |
| 98 | } |
| 99 | |
| 100 | template <class T, class U> |
| 101 | inline |
| 102 | bool |
| 103 | operator!=(const input_iterator<T>& x, const input_iterator<U>& y) |
| 104 | { |
| 105 | return !(x == y); |
| 106 | } |
| 107 | |
| 108 | template <class It> |
| 109 | class forward_iterator |
| 110 | { |
| 111 | It it_; |
| 112 | |
| 113 | template <class U> friend class forward_iterator; |
| 114 | public: |
| 115 | typedef std::forward_iterator_tag iterator_category; |
| 116 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 117 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 118 | typedef It pointer; |
| 119 | typedef typename std::iterator_traits<It>::reference reference; |
| 120 | |
| 121 | It base() const {return it_;} |
| 122 | |
| 123 | forward_iterator() : it_() {} |
| 124 | explicit forward_iterator(It it) : it_(it) {} |
| 125 | template <class U> |
| 126 | forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {} |
| 127 | |
| 128 | reference operator*() const {return *it_;} |
| 129 | pointer operator->() const {return it_;} |
| 130 | |
| 131 | forward_iterator& operator++() {++it_; return *this;} |
| 132 | forward_iterator operator++(int) |
| 133 | {forward_iterator tmp(*this); ++(*this); return tmp;} |
| 134 | |
| 135 | friend bool operator==(const forward_iterator& x, const forward_iterator& y) |
| 136 | {return x.it_ == y.it_;} |
| 137 | friend bool operator!=(const forward_iterator& x, const forward_iterator& y) |
| 138 | {return !(x == y);} |
Eric Fiselier | 910285b | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 139 | |
| 140 | template <class T> |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 141 | void operator,(T const &) DELETE_FUNCTION; |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | template <class T, class U> |
| 145 | inline |
| 146 | bool |
| 147 | operator==(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 148 | { |
| 149 | return x.base() == y.base(); |
| 150 | } |
| 151 | |
| 152 | template <class T, class U> |
| 153 | inline |
| 154 | bool |
| 155 | operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y) |
| 156 | { |
| 157 | return !(x == y); |
| 158 | } |
| 159 | |
| 160 | template <class It> |
| 161 | class bidirectional_iterator |
| 162 | { |
| 163 | It it_; |
| 164 | |
| 165 | template <class U> friend class bidirectional_iterator; |
| 166 | public: |
| 167 | typedef std::bidirectional_iterator_tag iterator_category; |
| 168 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 169 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 170 | typedef It pointer; |
| 171 | typedef typename std::iterator_traits<It>::reference reference; |
| 172 | |
| 173 | It base() const {return it_;} |
| 174 | |
| 175 | bidirectional_iterator() : it_() {} |
| 176 | explicit bidirectional_iterator(It it) : it_(it) {} |
| 177 | template <class U> |
| 178 | bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {} |
| 179 | |
| 180 | reference operator*() const {return *it_;} |
| 181 | pointer operator->() const {return it_;} |
| 182 | |
| 183 | bidirectional_iterator& operator++() {++it_; return *this;} |
| 184 | bidirectional_iterator operator++(int) |
| 185 | {bidirectional_iterator tmp(*this); ++(*this); return tmp;} |
| 186 | |
| 187 | bidirectional_iterator& operator--() {--it_; return *this;} |
| 188 | bidirectional_iterator operator--(int) |
| 189 | {bidirectional_iterator tmp(*this); --(*this); return tmp;} |
Eric Fiselier | 910285b | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 190 | |
| 191 | template <class T> |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 192 | void operator,(T const &) DELETE_FUNCTION; |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | template <class T, class U> |
| 196 | inline |
| 197 | bool |
| 198 | operator==(const bidirectional_iterator<T>& x, const bidirectional_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 bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y) |
| 207 | { |
| 208 | return !(x == y); |
| 209 | } |
| 210 | |
| 211 | template <class It> |
| 212 | class random_access_iterator |
| 213 | { |
| 214 | It it_; |
| 215 | |
| 216 | template <class U> friend class random_access_iterator; |
| 217 | public: |
| 218 | typedef std::random_access_iterator_tag iterator_category; |
| 219 | typedef typename std::iterator_traits<It>::value_type value_type; |
| 220 | typedef typename std::iterator_traits<It>::difference_type difference_type; |
| 221 | typedef It pointer; |
| 222 | typedef typename std::iterator_traits<It>::reference reference; |
| 223 | |
| 224 | It base() const {return it_;} |
| 225 | |
| 226 | random_access_iterator() : it_() {} |
| 227 | explicit random_access_iterator(It it) : it_(it) {} |
| 228 | template <class U> |
| 229 | random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {} |
| 230 | |
| 231 | reference operator*() const {return *it_;} |
| 232 | pointer operator->() const {return it_;} |
| 233 | |
| 234 | random_access_iterator& operator++() {++it_; return *this;} |
| 235 | random_access_iterator operator++(int) |
| 236 | {random_access_iterator tmp(*this); ++(*this); return tmp;} |
| 237 | |
| 238 | random_access_iterator& operator--() {--it_; return *this;} |
| 239 | random_access_iterator operator--(int) |
| 240 | {random_access_iterator tmp(*this); --(*this); return tmp;} |
| 241 | |
| 242 | random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;} |
| 243 | random_access_iterator operator+(difference_type n) const |
| 244 | {random_access_iterator tmp(*this); tmp += n; return tmp;} |
| 245 | friend random_access_iterator operator+(difference_type n, random_access_iterator x) |
| 246 | {x += n; return x;} |
| 247 | random_access_iterator& operator-=(difference_type n) {return *this += -n;} |
| 248 | random_access_iterator operator-(difference_type n) const |
| 249 | {random_access_iterator tmp(*this); tmp -= n; return tmp;} |
| 250 | |
| 251 | reference operator[](difference_type n) const {return it_[n];} |
Eric Fiselier | 910285b | 2014-10-27 19:28:20 +0000 | [diff] [blame] | 252 | |
| 253 | template <class T> |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 254 | void operator,(T const &) DELETE_FUNCTION; |
Howard Hinnant | c1198c3 | 2010-07-16 19:08:36 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | template <class T, class U> |
| 258 | inline |
| 259 | bool |
| 260 | operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 261 | { |
| 262 | return x.base() == y.base(); |
| 263 | } |
| 264 | |
| 265 | template <class T, class U> |
| 266 | inline |
| 267 | bool |
| 268 | operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 269 | { |
| 270 | return !(x == y); |
| 271 | } |
| 272 | |
| 273 | template <class T, class U> |
| 274 | inline |
| 275 | bool |
| 276 | operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 277 | { |
| 278 | return x.base() < y.base(); |
| 279 | } |
| 280 | |
| 281 | template <class T, class U> |
| 282 | inline |
| 283 | bool |
| 284 | operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 285 | { |
| 286 | return !(y < x); |
| 287 | } |
| 288 | |
| 289 | template <class T, class U> |
| 290 | inline |
| 291 | bool |
| 292 | operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 293 | { |
| 294 | return y < x; |
| 295 | } |
| 296 | |
| 297 | template <class T, class U> |
| 298 | inline |
| 299 | bool |
| 300 | operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 301 | { |
| 302 | return !(x < y); |
| 303 | } |
| 304 | |
| 305 | template <class T, class U> |
| 306 | inline |
| 307 | typename std::iterator_traits<T>::difference_type |
| 308 | operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y) |
| 309 | { |
| 310 | return x.base() - y.base(); |
| 311 | } |
| 312 | |
Marshall Clow | f8c2b82 | 2013-01-04 18:24:04 +0000 | [diff] [blame] | 313 | template <class Iter> |
| 314 | inline Iter base(output_iterator<Iter> i) { return i.base(); } |
| 315 | |
| 316 | template <class Iter> |
| 317 | inline Iter base(input_iterator<Iter> i) { return i.base(); } |
| 318 | |
| 319 | template <class Iter> |
| 320 | inline Iter base(forward_iterator<Iter> i) { return i.base(); } |
| 321 | |
| 322 | template <class Iter> |
| 323 | inline Iter base(bidirectional_iterator<Iter> i) { return i.base(); } |
| 324 | |
| 325 | template <class Iter> |
| 326 | inline Iter base(random_access_iterator<Iter> i) { return i.base(); } |
| 327 | |
Howard Hinnant | e0fe3d2 | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 328 | template <class Iter> // everything else |
Marshall Clow | f8c2b82 | 2013-01-04 18:24:04 +0000 | [diff] [blame] | 329 | inline Iter base(Iter i) { return i; } |
| 330 | |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 331 | template <typename T> |
| 332 | struct ThrowingIterator { |
| 333 | typedef std::bidirectional_iterator_tag iterator_category; |
| 334 | typedef ptrdiff_t difference_type; |
| 335 | typedef const T value_type; |
| 336 | typedef const T * pointer; |
| 337 | typedef const T & reference; |
| 338 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 339 | enum ThrowingAction { TAIncrement, TADecrement, TADereference, TAAssignment, TAComparison }; |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 340 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 341 | // Constructors |
| 342 | ThrowingIterator () |
| 343 | : begin_(nullptr), end_(nullptr), current_(nullptr), action_(TADereference), index_(0) {} |
| 344 | ThrowingIterator (const T *first, const T *last, size_t index = 0, ThrowingAction action = TADereference) |
| 345 | : begin_(first), end_(last), current_(first), action_(action), index_(index) {} |
| 346 | ThrowingIterator (const ThrowingIterator &rhs) |
| 347 | : begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_), action_(rhs.action_), index_(rhs.index_) {} |
| 348 | ThrowingIterator & operator= (const ThrowingIterator &rhs) |
| 349 | { |
| 350 | if (action_ == TAAssignment) |
| 351 | { |
| 352 | if (index_ == 0) |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 353 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 354 | throw std::runtime_error ("throw from iterator assignment"); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 355 | #else |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 356 | assert(false); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 357 | #endif |
| 358 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 359 | else |
| 360 | --index_; |
| 361 | } |
| 362 | begin_ = rhs.begin_; |
| 363 | end_ = rhs.end_; |
| 364 | current_ = rhs.current_; |
| 365 | action_ = rhs.action_; |
| 366 | index_ = rhs.index_; |
| 367 | return *this; |
| 368 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 369 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 370 | // iterator operations |
| 371 | reference operator*() const |
| 372 | { |
| 373 | if (action_ == TADereference) |
| 374 | { |
| 375 | if (index_ == 0) |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 376 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 377 | throw std::runtime_error ("throw from iterator dereference"); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 378 | #else |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 379 | assert(false); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 380 | #endif |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 381 | else |
| 382 | --index_; |
| 383 | } |
| 384 | return *current_; |
| 385 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 386 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 387 | ThrowingIterator & operator++() |
| 388 | { |
| 389 | if (action_ == TAIncrement) |
| 390 | { |
| 391 | if (index_ == 0) |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 392 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 393 | throw std::runtime_error ("throw from iterator increment"); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 394 | #else |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 395 | assert(false); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 396 | #endif |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 397 | else |
| 398 | --index_; |
| 399 | } |
| 400 | ++current_; |
| 401 | return *this; |
| 402 | } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 403 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 404 | ThrowingIterator operator++(int) |
| 405 | { |
| 406 | ThrowingIterator temp = *this; |
| 407 | ++(*this); |
| 408 | return temp; |
| 409 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 410 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 411 | ThrowingIterator & operator--() |
| 412 | { |
| 413 | if (action_ == TADecrement) |
| 414 | { |
| 415 | if (index_ == 0) |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 416 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 417 | throw std::runtime_error ("throw from iterator decrement"); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 418 | #else |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 419 | assert(false); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 420 | #endif |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 421 | else |
| 422 | --index_; |
| 423 | } |
| 424 | --current_; |
| 425 | return *this; |
| 426 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 427 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 428 | ThrowingIterator operator--(int) { |
| 429 | ThrowingIterator temp = *this; |
| 430 | --(*this); |
| 431 | return temp; |
| 432 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 433 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 434 | bool operator== (const ThrowingIterator &rhs) const |
| 435 | { |
| 436 | if (action_ == TAComparison) |
| 437 | { |
| 438 | if (index_ == 0) |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 439 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 440 | throw std::runtime_error ("throw from iterator comparison"); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 441 | #else |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 442 | assert(false); |
Marshall Clow | 9d10d27 | 2016-01-20 03:19:15 +0000 | [diff] [blame] | 443 | #endif |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 444 | else |
| 445 | --index_; |
| 446 | } |
| 447 | bool atEndL = current_ == end_; |
| 448 | bool atEndR = rhs.current_ == rhs.end_; |
| 449 | if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not. |
| 450 | if (atEndL) return true; // both are at the end (or empty) |
| 451 | return current_ == rhs.current_; |
| 452 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 453 | |
| 454 | private: |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 455 | const T* begin_; |
| 456 | const T* end_; |
| 457 | const T* current_; |
| 458 | ThrowingAction action_; |
| 459 | mutable size_t index_; |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | template <typename T> |
| 463 | bool operator== (const ThrowingIterator<T>& a, const ThrowingIterator<T>& b) |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 464 | { return a.operator==(b); } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 465 | |
| 466 | template <typename T> |
| 467 | bool operator!= (const ThrowingIterator<T>& a, const ThrowingIterator<T>& b) |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 468 | { return !a.operator==(b); } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 469 | |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 470 | template <typename T> |
| 471 | struct NonThrowingIterator { |
| 472 | typedef std::bidirectional_iterator_tag iterator_category; |
| 473 | typedef ptrdiff_t difference_type; |
| 474 | typedef const T value_type; |
| 475 | typedef const T * pointer; |
| 476 | typedef const T & reference; |
| 477 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 478 | // Constructors |
| 479 | NonThrowingIterator () |
| 480 | : begin_(nullptr), end_(nullptr), current_(nullptr) {} |
| 481 | NonThrowingIterator (const T *first, const T* last) |
| 482 | : begin_(first), end_(last), current_(first) {} |
| 483 | NonThrowingIterator (const NonThrowingIterator &rhs) |
| 484 | : begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_) {} |
| 485 | NonThrowingIterator & operator= (const NonThrowingIterator &rhs) TEST_NOEXCEPT |
| 486 | { |
| 487 | begin_ = rhs.begin_; |
| 488 | end_ = rhs.end_; |
| 489 | current_ = rhs.current_; |
| 490 | return *this; |
| 491 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 492 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 493 | // iterator operations |
| 494 | reference operator*() const TEST_NOEXCEPT |
| 495 | { |
| 496 | return *current_; |
| 497 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 498 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 499 | NonThrowingIterator & operator++() TEST_NOEXCEPT |
| 500 | { |
| 501 | ++current_; |
| 502 | return *this; |
| 503 | } |
Eric Fiselier | d04c685 | 2016-06-01 21:35:39 +0000 | [diff] [blame] | 504 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 505 | NonThrowingIterator operator++(int) TEST_NOEXCEPT |
| 506 | { |
| 507 | NonThrowingIterator temp = *this; |
| 508 | ++(*this); |
| 509 | return temp; |
| 510 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 511 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 512 | NonThrowingIterator & operator--() TEST_NOEXCEPT |
| 513 | { |
| 514 | --current_; |
| 515 | return *this; |
| 516 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 517 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 518 | NonThrowingIterator operator--(int) TEST_NOEXCEPT |
| 519 | { |
| 520 | NonThrowingIterator temp = *this; |
| 521 | --(*this); |
| 522 | return temp; |
| 523 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 524 | |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 525 | bool operator== (const NonThrowingIterator &rhs) const TEST_NOEXCEPT |
| 526 | { |
| 527 | bool atEndL = current_ == end_; |
| 528 | bool atEndR = rhs.current_ == rhs.end_; |
| 529 | if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not. |
| 530 | if (atEndL) return true; // both are at the end (or empty) |
| 531 | return current_ == rhs.current_; |
| 532 | } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 533 | |
| 534 | private: |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 535 | const T* begin_; |
| 536 | const T* end_; |
| 537 | const T* current_; |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | template <typename T> |
| 541 | bool operator== (const NonThrowingIterator<T>& a, const NonThrowingIterator<T>& b) TEST_NOEXCEPT |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 542 | { return a.operator==(b); } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 543 | |
| 544 | template <typename T> |
| 545 | bool operator!= (const NonThrowingIterator<T>& a, const NonThrowingIterator<T>& b) TEST_NOEXCEPT |
Marshall Clow | 64ca686 | 2016-01-20 03:37:46 +0000 | [diff] [blame] | 546 | { return !a.operator==(b); } |
Marshall Clow | 76b4afc | 2016-01-13 21:54:34 +0000 | [diff] [blame] | 547 | |
Eric Fiselier | 847ee13 | 2014-10-27 20:26:25 +0000 | [diff] [blame] | 548 | #undef DELETE_FUNCTION |
| 549 | |
Howard Hinnant | f36101d | 2010-08-22 00:45:01 +0000 | [diff] [blame] | 550 | #endif // ITERATORS_H |