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