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