Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------- forward_list ---------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_FORWARD_LIST |
| 12 | #define _LIBCPP_FORWARD_LIST |
| 13 | |
| 14 | /* |
| 15 | forward_list synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class T, class Allocator = allocator<T>> |
| 21 | class forward_list |
| 22 | { |
| 23 | public: |
| 24 | typedef T value_type; |
| 25 | typedef Allocator allocator_type; |
| 26 | |
| 27 | typedef value_type& reference; |
| 28 | typedef const value_type& const_reference; |
| 29 | typedef typename allocator_traits<allocator_type>::pointer pointer; |
| 30 | typedef typename allocator_traits<allocator_type>::const_pointer const_pointer; |
| 31 | typedef typename allocator_traits<allocator_type>::size_type size_type; |
| 32 | typedef typename allocator_traits<allocator_type>::difference_type difference_type; |
| 33 | |
| 34 | typedef <details> iterator; |
| 35 | typedef <details> const_iterator; |
| 36 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 37 | forward_list() |
| 38 | noexcept(is_nothrow_default_constructible<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | explicit forward_list(const allocator_type& a); |
| 40 | explicit forward_list(size_type n); |
| 41 | forward_list(size_type n, const value_type& v); |
| 42 | forward_list(size_type n, const value_type& v, const allocator_type& a); |
| 43 | template <class InputIterator> |
| 44 | forward_list(InputIterator first, InputIterator last); |
| 45 | template <class InputIterator> |
| 46 | forward_list(InputIterator first, InputIterator last, const allocator_type& a); |
| 47 | forward_list(const forward_list& x); |
| 48 | forward_list(const forward_list& x, const allocator_type& a); |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 49 | forward_list(forward_list&& x) |
| 50 | noexcept(is_nothrow_move_constructible<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 51 | forward_list(forward_list&& x, const allocator_type& a); |
| 52 | forward_list(initializer_list<value_type> il); |
| 53 | forward_list(initializer_list<value_type> il, const allocator_type& a); |
| 54 | |
| 55 | ~forward_list(); |
| 56 | |
| 57 | forward_list& operator=(const forward_list& x); |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 58 | forward_list& operator=(forward_list&& x) |
| 59 | noexcept( |
| 60 | allocator_type::propagate_on_container_move_assignment::value && |
| 61 | is_nothrow_move_assignable<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | forward_list& operator=(initializer_list<value_type> il); |
| 63 | |
| 64 | template <class InputIterator> |
| 65 | void assign(InputIterator first, InputIterator last); |
| 66 | void assign(size_type n, const value_type& v); |
| 67 | void assign(initializer_list<value_type> il); |
| 68 | |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 69 | allocator_type get_allocator() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 71 | iterator begin() noexcept; |
| 72 | const_iterator begin() const noexcept; |
| 73 | iterator end() noexcept; |
| 74 | const_iterator end() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 75 | |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 76 | const_iterator cbegin() const noexcept; |
| 77 | const_iterator cend() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 79 | iterator before_begin() noexcept; |
| 80 | const_iterator before_begin() const noexcept; |
| 81 | const_iterator cbefore_begin() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 83 | bool empty() const noexcept; |
| 84 | size_type max_size() const noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | |
| 86 | reference front(); |
| 87 | const_reference front() const; |
| 88 | |
| 89 | template <class... Args> void emplace_front(Args&&... args); |
| 90 | void push_front(const value_type& v); |
| 91 | void push_front(value_type&& v); |
| 92 | |
| 93 | void pop_front(); |
| 94 | |
| 95 | template <class... Args> |
| 96 | iterator emplace_after(const_iterator p, Args&&... args); |
| 97 | iterator insert_after(const_iterator p, const value_type& v); |
| 98 | iterator insert_after(const_iterator p, value_type&& v); |
| 99 | iterator insert_after(const_iterator p, size_type n, const value_type& v); |
| 100 | template <class InputIterator> |
| 101 | iterator insert_after(const_iterator p, |
| 102 | InputIterator first, InputIterator last); |
| 103 | iterator insert_after(const_iterator p, initializer_list<value_type> il); |
| 104 | |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 105 | iterator erase_after(const_iterator p); |
| 106 | iterator erase_after(const_iterator first, const_iterator last); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 107 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 108 | void swap(forward_list& x) |
| 109 | noexcept(!allocator_type::propagate_on_container_swap::value || |
| 110 | __is_nothrow_swappable<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 111 | |
| 112 | void resize(size_type n); |
| 113 | void resize(size_type n, const value_type& v); |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 114 | void clear() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 116 | void splice_after(const_iterator p, forward_list& x); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | void splice_after(const_iterator p, forward_list&& x); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 118 | void splice_after(const_iterator p, forward_list& x, const_iterator i); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | void splice_after(const_iterator p, forward_list&& x, const_iterator i); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 120 | void splice_after(const_iterator p, forward_list& x, |
| 121 | const_iterator first, const_iterator last); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 122 | void splice_after(const_iterator p, forward_list&& x, |
| 123 | const_iterator first, const_iterator last); |
| 124 | void remove(const value_type& v); |
| 125 | template <class Predicate> void remove_if(Predicate pred); |
| 126 | void unique(); |
| 127 | template <class BinaryPredicate> void unique(BinaryPredicate binary_pred); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 128 | void merge(forward_list& x); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 129 | void merge(forward_list&& x); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 130 | template <class Compare> void merge(forward_list& x, Compare comp); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | template <class Compare> void merge(forward_list&& x, Compare comp); |
| 132 | void sort(); |
| 133 | template <class Compare> void sort(Compare comp); |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 134 | void reverse() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | template <class T, class Allocator> |
| 138 | bool operator==(const forward_list<T, Allocator>& x, |
| 139 | const forward_list<T, Allocator>& y); |
| 140 | |
| 141 | template <class T, class Allocator> |
| 142 | bool operator< (const forward_list<T, Allocator>& x, |
| 143 | const forward_list<T, Allocator>& y); |
| 144 | |
| 145 | template <class T, class Allocator> |
| 146 | bool operator!=(const forward_list<T, Allocator>& x, |
| 147 | const forward_list<T, Allocator>& y); |
| 148 | |
| 149 | template <class T, class Allocator> |
| 150 | bool operator> (const forward_list<T, Allocator>& x, |
| 151 | const forward_list<T, Allocator>& y); |
| 152 | |
| 153 | template <class T, class Allocator> |
| 154 | bool operator>=(const forward_list<T, Allocator>& x, |
| 155 | const forward_list<T, Allocator>& y); |
| 156 | |
| 157 | template <class T, class Allocator> |
| 158 | bool operator<=(const forward_list<T, Allocator>& x, |
| 159 | const forward_list<T, Allocator>& y); |
| 160 | |
| 161 | template <class T, class Allocator> |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 162 | void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y) |
Howard Hinnant | c560727 | 2011-06-03 17:30:28 +0000 | [diff] [blame] | 163 | noexcept(noexcept(x.swap(y))); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | |
| 165 | } // std |
| 166 | |
| 167 | */ |
| 168 | |
| 169 | #include <__config> |
| 170 | |
| 171 | #include <initializer_list> |
| 172 | #include <memory> |
| 173 | #include <limits> |
| 174 | #include <iterator> |
| 175 | #include <algorithm> |
| 176 | |
| 177 | #pragma GCC system_header |
| 178 | |
| 179 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 180 | |
| 181 | template <class, class> struct __forward_list_node; |
| 182 | |
| 183 | template <class _NodePtr> |
| 184 | struct __forward_begin_node |
| 185 | { |
| 186 | typedef __forward_begin_node __self; |
| 187 | typedef _NodePtr pointer; |
| 188 | |
| 189 | pointer __next_; |
| 190 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 191 | _LIBCPP_INLINE_VISIBILITY __forward_begin_node() : __next_(nullptr) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | template <class _Tp, class _VoidPtr> |
| 195 | struct __forward_list_node |
| 196 | : public __forward_begin_node |
| 197 | < |
| 198 | typename pointer_traits<_VoidPtr>::template |
| 199 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 200 | rebind<__forward_list_node<_Tp, _VoidPtr> > |
| 201 | #else |
| 202 | rebind<__forward_list_node<_Tp, _VoidPtr> >::other |
| 203 | #endif |
| 204 | > |
| 205 | { |
| 206 | typedef _Tp value_type; |
| 207 | |
| 208 | value_type __value_; |
| 209 | }; |
| 210 | |
| 211 | template<class, class> class forward_list; |
| 212 | template<class> class __forward_list_const_iterator; |
| 213 | |
| 214 | template <class _NodePtr> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 215 | class _LIBCPP_VISIBLE __forward_list_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | { |
| 217 | typedef _NodePtr __node_pointer; |
| 218 | |
| 219 | __node_pointer __ptr_; |
| 220 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 221 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 222 | explicit __forward_list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | |
| 224 | template<class, class> friend class forward_list; |
| 225 | template<class> friend class __forward_list_const_iterator; |
| 226 | |
| 227 | public: |
| 228 | typedef forward_iterator_tag iterator_category; |
| 229 | typedef typename pointer_traits<__node_pointer>::element_type::value_type |
| 230 | value_type; |
| 231 | typedef value_type& reference; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 232 | typedef typename pointer_traits<__node_pointer>::difference_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | difference_type; |
| 234 | typedef typename pointer_traits<__node_pointer>::template |
| 235 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 236 | rebind<value_type> |
| 237 | #else |
| 238 | rebind<value_type>::other |
| 239 | #endif |
| 240 | pointer; |
| 241 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 242 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 243 | __forward_list_iterator() _NOEXCEPT : __ptr_(nullptr) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 244 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 245 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | reference operator*() const {return __ptr_->__value_;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 247 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 248 | pointer operator->() const {return &__ptr_->__value_;} |
| 249 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 250 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | __forward_list_iterator& operator++() |
| 252 | { |
| 253 | __ptr_ = __ptr_->__next_; |
| 254 | return *this; |
| 255 | } |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 256 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | __forward_list_iterator operator++(int) |
| 258 | { |
| 259 | __forward_list_iterator __t(*this); |
| 260 | ++(*this); |
| 261 | return __t; |
| 262 | } |
| 263 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 264 | friend _LIBCPP_INLINE_VISIBILITY |
| 265 | bool operator==(const __forward_list_iterator& __x, |
| 266 | const __forward_list_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | {return __x.__ptr_ == __y.__ptr_;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 268 | friend _LIBCPP_INLINE_VISIBILITY |
| 269 | bool operator!=(const __forward_list_iterator& __x, |
| 270 | const __forward_list_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | {return !(__x == __y);} |
| 272 | }; |
| 273 | |
| 274 | template <class _NodeConstPtr> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 275 | class _LIBCPP_VISIBLE __forward_list_const_iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 276 | { |
| 277 | typedef _NodeConstPtr __node_const_pointer; |
| 278 | |
| 279 | __node_const_pointer __ptr_; |
| 280 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 281 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 282 | explicit __forward_list_const_iterator(__node_const_pointer __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 283 | : __ptr_(__p) {} |
| 284 | |
| 285 | typedef typename remove_const |
| 286 | < |
| 287 | typename pointer_traits<__node_const_pointer>::element_type |
| 288 | >::type __node; |
| 289 | typedef typename pointer_traits<__node_const_pointer>::template |
| 290 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 291 | rebind<__node> |
| 292 | #else |
| 293 | rebind<__node>::other |
| 294 | #endif |
| 295 | __node_pointer; |
| 296 | |
| 297 | template<class, class> friend class forward_list; |
| 298 | |
| 299 | public: |
| 300 | typedef forward_iterator_tag iterator_category; |
| 301 | typedef typename __node::value_type value_type; |
| 302 | typedef const value_type& reference; |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 303 | typedef typename pointer_traits<__node_const_pointer>::difference_type |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 304 | difference_type; |
| 305 | typedef typename pointer_traits<__node_const_pointer>::template |
| 306 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 307 | rebind<const value_type> |
| 308 | #else |
| 309 | rebind<const value_type>::other |
| 310 | #endif |
| 311 | pointer; |
| 312 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 313 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 314 | __forward_list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 315 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 316 | __forward_list_const_iterator(__forward_list_iterator<__node_pointer> __p) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 317 | : __ptr_(__p.__ptr_) {} |
| 318 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 319 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 320 | reference operator*() const {return __ptr_->__value_;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 321 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 322 | pointer operator->() const {return &__ptr_->__value_;} |
| 323 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 324 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 325 | __forward_list_const_iterator& operator++() |
| 326 | { |
| 327 | __ptr_ = __ptr_->__next_; |
| 328 | return *this; |
| 329 | } |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 330 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 331 | __forward_list_const_iterator operator++(int) |
| 332 | { |
| 333 | __forward_list_const_iterator __t(*this); |
| 334 | ++(*this); |
| 335 | return __t; |
| 336 | } |
| 337 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 338 | friend _LIBCPP_INLINE_VISIBILITY |
| 339 | bool operator==(const __forward_list_const_iterator& __x, |
| 340 | const __forward_list_const_iterator& __y) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 341 | {return __x.__ptr_ == __y.__ptr_;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 342 | friend _LIBCPP_INLINE_VISIBILITY |
| 343 | bool operator!=(const __forward_list_const_iterator& __x, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 344 | const __forward_list_const_iterator& __y) |
| 345 | {return !(__x == __y);} |
| 346 | }; |
| 347 | |
| 348 | template <class _Tp, class _Alloc> |
| 349 | class __forward_list_base |
| 350 | { |
| 351 | protected: |
| 352 | typedef _Tp value_type; |
| 353 | typedef _Alloc allocator_type; |
| 354 | |
| 355 | typedef typename allocator_traits<allocator_type>::void_pointer void_pointer; |
| 356 | typedef __forward_list_node<value_type, void_pointer> __node; |
| 357 | typedef typename __node::__self __begin_node; |
| 358 | typedef typename allocator_traits<allocator_type>::template |
| 359 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 360 | rebind_alloc<__node> |
| 361 | #else |
| 362 | rebind_alloc<__node>::other |
| 363 | #endif |
| 364 | __node_allocator; |
| 365 | typedef allocator_traits<__node_allocator> __node_traits; |
| 366 | typedef typename __node_traits::pointer __node_pointer; |
| 367 | typedef typename __node_traits::const_pointer __node_const_pointer; |
| 368 | |
| 369 | __compressed_pair<__begin_node, __node_allocator> __before_begin_; |
| 370 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 371 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 372 | __node_pointer __before_begin() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 373 | {return pointer_traits<__node_pointer>::pointer_to( |
| 374 | static_cast<__node&>(__before_begin_.first()));} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 375 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 376 | __node_const_pointer __before_begin() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 377 | {return pointer_traits<__node_const_pointer>::pointer_to( |
| 378 | static_cast<const __node&>(__before_begin_.first()));} |
| 379 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 380 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 381 | __node_allocator& __alloc() _NOEXCEPT |
| 382 | {return __before_begin_.second();} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 384 | const __node_allocator& __alloc() const _NOEXCEPT |
| 385 | {return __before_begin_.second();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 386 | |
| 387 | typedef __forward_list_iterator<__node_pointer> iterator; |
| 388 | typedef __forward_list_const_iterator<__node_const_pointer> const_iterator; |
| 389 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 390 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | __forward_list_base() |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 392 | _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 393 | : __before_begin_(__begin_node()) {} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 394 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 395 | __forward_list_base(const allocator_type& __a) |
| 396 | : __before_begin_(__begin_node(), __node_allocator(__a)) {} |
| 397 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 398 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 399 | public: |
| 400 | __forward_list_base(__forward_list_base&& __x) |
| 401 | _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 402 | __forward_list_base(__forward_list_base&& __x, const allocator_type& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 403 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 404 | |
| 405 | private: |
| 406 | __forward_list_base(const __forward_list_base&); |
| 407 | __forward_list_base& operator=(const __forward_list_base&); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 409 | public: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 410 | ~__forward_list_base(); |
| 411 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 412 | protected: |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | void __copy_assign_alloc(const __forward_list_base& __x) |
| 415 | {__copy_assign_alloc(__x, integral_constant<bool, |
| 416 | __node_traits::propagate_on_container_copy_assignment::value>());} |
| 417 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 418 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 419 | void __move_assign_alloc(__forward_list_base& __x) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 420 | _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value || |
| 421 | is_nothrow_move_assignable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | {__move_assign_alloc(__x, integral_constant<bool, |
| 423 | __node_traits::propagate_on_container_move_assignment::value>());} |
| 424 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 425 | public: |
| 426 | void swap(__forward_list_base& __x) |
| 427 | _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value || |
| 428 | __is_nothrow_swappable<__node_allocator>::value); |
| 429 | protected: |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 430 | void clear() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 431 | |
| 432 | private: |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | void __copy_assign_alloc(const __forward_list_base&, false_type) {} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 435 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 436 | void __copy_assign_alloc(const __forward_list_base& __x, true_type) |
| 437 | { |
| 438 | if (__alloc() != __x.__alloc()) |
| 439 | clear(); |
| 440 | __alloc() = __x.__alloc(); |
| 441 | } |
| 442 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 443 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 444 | void __move_assign_alloc(__forward_list_base& __x, false_type) _NOEXCEPT |
| 445 | {} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 446 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 447 | void __move_assign_alloc(__forward_list_base& __x, true_type) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 448 | _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 449 | {__alloc() = _STD::move(__x.__alloc());} |
| 450 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 451 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 452 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 453 | _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value || |
| 454 | __is_nothrow_swappable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 455 | {__swap_alloc(__x, __y, integral_constant<bool, |
| 456 | __node_traits::propagate_on_container_swap::value>());} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 457 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 458 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, |
| 459 | false_type) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 460 | _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 461 | {} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 462 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 463 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, |
| 464 | true_type) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 465 | _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 466 | { |
| 467 | using _STD::swap; |
| 468 | swap(__x, __y); |
| 469 | } |
| 470 | }; |
| 471 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 472 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 473 | |
| 474 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 475 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 476 | __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 477 | _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 478 | : __before_begin_(_STD::move(__x.__before_begin_)) |
| 479 | { |
| 480 | __x.__before_begin()->__next_ = nullptr; |
| 481 | } |
| 482 | |
| 483 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 484 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x, |
| 486 | const allocator_type& __a) |
| 487 | : __before_begin_(__begin_node(), __node_allocator(__a)) |
| 488 | { |
| 489 | if (__alloc() == __x.__alloc()) |
| 490 | { |
| 491 | __before_begin()->__next_ = __x.__before_begin()->__next_; |
| 492 | __x.__before_begin()->__next_ = nullptr; |
| 493 | } |
| 494 | } |
| 495 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 496 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 497 | |
| 498 | template <class _Tp, class _Alloc> |
| 499 | __forward_list_base<_Tp, _Alloc>::~__forward_list_base() |
| 500 | { |
| 501 | clear(); |
| 502 | } |
| 503 | |
| 504 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 505 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 506 | void |
| 507 | __forward_list_base<_Tp, _Alloc>::swap(__forward_list_base& __x) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 508 | _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value || |
| 509 | __is_nothrow_swappable<__node_allocator>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 510 | { |
| 511 | __swap_alloc(__alloc(), __x.__alloc()); |
| 512 | using _STD::swap; |
| 513 | swap(__before_begin()->__next_, __x.__before_begin()->__next_); |
| 514 | } |
| 515 | |
| 516 | template <class _Tp, class _Alloc> |
| 517 | void |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 518 | __forward_list_base<_Tp, _Alloc>::clear() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 519 | { |
| 520 | __node_allocator& __a = __alloc(); |
| 521 | for (__node_pointer __p = __before_begin()->__next_; __p != nullptr;) |
| 522 | { |
| 523 | __node_pointer __next = __p->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 524 | __node_traits::destroy(__a, _STD::addressof(__p->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 525 | __node_traits::deallocate(__a, __p, 1); |
| 526 | __p = __next; |
| 527 | } |
| 528 | __before_begin()->__next_ = nullptr; |
| 529 | } |
| 530 | |
| 531 | template <class _Tp, class _Alloc = allocator<_Tp> > |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 532 | class _LIBCPP_VISIBLE forward_list |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 533 | : private __forward_list_base<_Tp, _Alloc> |
| 534 | { |
| 535 | typedef __forward_list_base<_Tp, _Alloc> base; |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 536 | typedef typename base::__node_allocator __node_allocator; |
| 537 | typedef typename base::__node __node; |
| 538 | typedef typename base::__node_traits __node_traits; |
| 539 | typedef typename base::__node_pointer __node_pointer; |
| 540 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 541 | public: |
| 542 | typedef _Tp value_type; |
| 543 | typedef _Alloc allocator_type; |
| 544 | |
| 545 | typedef value_type& reference; |
| 546 | typedef const value_type& const_reference; |
| 547 | typedef typename allocator_traits<allocator_type>::pointer pointer; |
| 548 | typedef typename allocator_traits<allocator_type>::const_pointer const_pointer; |
| 549 | typedef typename allocator_traits<allocator_type>::size_type size_type; |
| 550 | typedef typename allocator_traits<allocator_type>::difference_type difference_type; |
| 551 | |
| 552 | typedef typename base::iterator iterator; |
| 553 | typedef typename base::const_iterator const_iterator; |
| 554 | |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 555 | _LIBCPP_INLINE_VISIBILITY |
| 556 | forward_list() |
| 557 | _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) |
| 558 | {} // = default; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 559 | explicit forward_list(const allocator_type& __a); |
| 560 | explicit forward_list(size_type __n); |
| 561 | forward_list(size_type __n, const value_type& __v); |
| 562 | forward_list(size_type __n, const value_type& __v, const allocator_type& __a); |
| 563 | template <class _InputIterator> |
| 564 | forward_list(_InputIterator __f, _InputIterator __l, |
| 565 | typename enable_if< |
| 566 | __is_input_iterator<_InputIterator>::value |
| 567 | >::type* = nullptr); |
| 568 | template <class _InputIterator> |
| 569 | forward_list(_InputIterator __f, _InputIterator __l, |
| 570 | const allocator_type& __a, |
| 571 | typename enable_if< |
| 572 | __is_input_iterator<_InputIterator>::value |
| 573 | >::type* = nullptr); |
| 574 | forward_list(const forward_list& __x); |
| 575 | forward_list(const forward_list& __x, const allocator_type& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 576 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 577 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 578 | forward_list(forward_list&& __x) |
| 579 | _NOEXCEPT_(is_nothrow_move_constructible<base>::value) |
| 580 | : base(_STD::move(__x)) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 581 | forward_list(forward_list&& __x, const allocator_type& __a); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 582 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 583 | forward_list(initializer_list<value_type> __il); |
| 584 | forward_list(initializer_list<value_type> __il, const allocator_type& __a); |
| 585 | |
| 586 | // ~forward_list() = default; |
| 587 | |
| 588 | forward_list& operator=(const forward_list& __x); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 589 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 590 | forward_list& operator=(forward_list&& __x) |
| 591 | _NOEXCEPT_( |
| 592 | __node_traits::propagate_on_container_move_assignment::value && |
| 593 | is_nothrow_move_assignable<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 594 | #endif |
| 595 | forward_list& operator=(initializer_list<value_type> __il); |
| 596 | |
| 597 | template <class _InputIterator> |
| 598 | typename enable_if |
| 599 | < |
| 600 | __is_input_iterator<_InputIterator>::value, |
| 601 | void |
| 602 | >::type |
| 603 | assign(_InputIterator __f, _InputIterator __l); |
| 604 | void assign(size_type __n, const value_type& __v); |
| 605 | void assign(initializer_list<value_type> __il); |
| 606 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 607 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 608 | allocator_type get_allocator() const _NOEXCEPT |
| 609 | {return allocator_type(base::__alloc());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 610 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 611 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 612 | iterator begin() _NOEXCEPT |
| 613 | {return iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 614 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 615 | const_iterator begin() const _NOEXCEPT |
| 616 | {return const_iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 617 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 618 | iterator end() _NOEXCEPT |
| 619 | {return iterator(nullptr);} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 620 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 621 | const_iterator end() const _NOEXCEPT |
| 622 | {return const_iterator(nullptr);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 623 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 624 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 625 | const_iterator cbegin() const _NOEXCEPT |
| 626 | {return const_iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 627 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 628 | const_iterator cend() const _NOEXCEPT |
| 629 | {return const_iterator(nullptr);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 631 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 632 | iterator before_begin() _NOEXCEPT |
| 633 | {return iterator(base::__before_begin());} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 634 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 635 | const_iterator before_begin() const _NOEXCEPT |
| 636 | {return const_iterator(base::__before_begin());} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 637 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 638 | const_iterator cbefore_begin() const _NOEXCEPT |
| 639 | {return const_iterator(base::__before_begin());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 640 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 641 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 642 | bool empty() const _NOEXCEPT |
| 643 | {return base::__before_begin()->__next_ == nullptr;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 644 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 645 | size_type max_size() const _NOEXCEPT |
| 646 | {return numeric_limits<size_type>::max();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 647 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 648 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 649 | reference front() {return base::__before_begin()->__next_->__value_;} |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 650 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 651 | const_reference front() const {return base::__before_begin()->__next_->__value_;} |
| 652 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 653 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 654 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 655 | template <class... _Args> void emplace_front(_Args&&... __args); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 656 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 657 | void push_front(value_type&& __v); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 658 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 659 | void push_front(const value_type& __v); |
| 660 | |
| 661 | void pop_front(); |
| 662 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 663 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 664 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 665 | template <class... _Args> |
| 666 | iterator emplace_after(const_iterator __p, _Args&&... __args); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 667 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 668 | iterator insert_after(const_iterator __p, value_type&& __v); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 669 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 670 | iterator insert_after(const_iterator __p, const value_type& __v); |
| 671 | iterator insert_after(const_iterator __p, size_type __n, const value_type& __v); |
| 672 | template <class _InputIterator> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 673 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 674 | typename enable_if |
| 675 | < |
| 676 | __is_input_iterator<_InputIterator>::value, |
| 677 | iterator |
| 678 | >::type |
| 679 | insert_after(const_iterator __p, _InputIterator __f, _InputIterator __l); |
| 680 | iterator insert_after(const_iterator __p, initializer_list<value_type> __il) |
| 681 | {return insert_after(__p, __il.begin(), __il.end());} |
| 682 | |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 683 | iterator erase_after(const_iterator __p); |
| 684 | iterator erase_after(const_iterator __f, const_iterator __l); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 685 | |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 686 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 687 | void swap(forward_list& __x) |
| 688 | _NOEXCEPT_(!__node_traits::propagate_on_container_swap::value || |
| 689 | __is_nothrow_swappable<__node_allocator>::value) |
| 690 | {base::swap(__x);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 691 | |
| 692 | void resize(size_type __n); |
| 693 | void resize(size_type __n, const value_type& __v); |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 694 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 695 | void clear() _NOEXCEPT {base::clear();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 696 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 697 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 698 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 699 | void splice_after(const_iterator __p, forward_list&& __x); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 700 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 701 | void splice_after(const_iterator __p, forward_list&& __x, const_iterator __i); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 702 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 703 | void splice_after(const_iterator __p, forward_list&& __x, |
| 704 | const_iterator __f, const_iterator __l); |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 705 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 706 | void splice_after(const_iterator __p, forward_list& __x); |
| 707 | void splice_after(const_iterator __p, forward_list& __x, const_iterator __i); |
| 708 | void splice_after(const_iterator __p, forward_list& __x, |
| 709 | const_iterator __f, const_iterator __l); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 710 | void remove(const value_type& __v); |
| 711 | template <class _Predicate> void remove_if(_Predicate __pred); |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 712 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 713 | void unique() {unique(__equal_to<value_type>());} |
| 714 | template <class _BinaryPredicate> void unique(_BinaryPredicate __binary_pred); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 715 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 716 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 717 | void merge(forward_list&& __x) {merge(__x, __less<value_type>());} |
| 718 | template <class _Compare> |
| 719 | _LIBCPP_INLINE_VISIBILITY |
| 720 | void merge(forward_list&& __x, _Compare __comp) |
| 721 | {merge(__x, _STD::move(__comp));} |
| 722 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 723 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 724 | void merge(forward_list& __x) {merge(__x, __less<value_type>());} |
| 725 | template <class _Compare> void merge(forward_list& __x, _Compare __comp); |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 726 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 727 | void sort() {sort(__less<value_type>());} |
| 728 | template <class _Compare> void sort(_Compare __comp); |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 729 | void reverse() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 730 | |
| 731 | private: |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 732 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 733 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 734 | void __move_assign(forward_list& __x, true_type) |
| 735 | _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 736 | void __move_assign(forward_list& __x, false_type); |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 737 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 738 | |
| 739 | template <class _Compare> |
| 740 | static |
| 741 | __node_pointer |
| 742 | __merge(__node_pointer __f1, __node_pointer __f2, _Compare& __comp); |
| 743 | |
| 744 | template <class _Compare> |
| 745 | static |
| 746 | __node_pointer |
| 747 | __sort(__node_pointer __f, difference_type __sz, _Compare& __comp); |
| 748 | }; |
| 749 | |
| 750 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 751 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 752 | forward_list<_Tp, _Alloc>::forward_list(const allocator_type& __a) |
| 753 | : base(__a) |
| 754 | { |
| 755 | } |
| 756 | |
| 757 | template <class _Tp, class _Alloc> |
| 758 | forward_list<_Tp, _Alloc>::forward_list(size_type __n) |
| 759 | { |
| 760 | if (__n > 0) |
| 761 | { |
| 762 | __node_allocator& __a = base::__alloc(); |
| 763 | typedef __allocator_destructor<__node_allocator> _D; |
| 764 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 765 | for (__node_pointer __p = base::__before_begin(); __n > 0; --__n, |
| 766 | __p = __p->__next_) |
| 767 | { |
| 768 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 769 | __node_traits::construct(__a, _STD::addressof(__h->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 770 | __h->__next_ = nullptr; |
| 771 | __p->__next_ = __h.release(); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | template <class _Tp, class _Alloc> |
| 777 | forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v) |
| 778 | { |
| 779 | insert_after(cbefore_begin(), __n, __v); |
| 780 | } |
| 781 | |
| 782 | template <class _Tp, class _Alloc> |
| 783 | forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v, |
| 784 | const allocator_type& __a) |
| 785 | : base(__a) |
| 786 | { |
| 787 | insert_after(cbefore_begin(), __n, __v); |
| 788 | } |
| 789 | |
| 790 | template <class _Tp, class _Alloc> |
| 791 | template <class _InputIterator> |
| 792 | forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l, |
| 793 | typename enable_if< |
| 794 | __is_input_iterator<_InputIterator>::value |
| 795 | >::type*) |
| 796 | { |
| 797 | insert_after(cbefore_begin(), __f, __l); |
| 798 | } |
| 799 | |
| 800 | template <class _Tp, class _Alloc> |
| 801 | template <class _InputIterator> |
| 802 | forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l, |
| 803 | const allocator_type& __a, |
| 804 | typename enable_if< |
| 805 | __is_input_iterator<_InputIterator>::value |
| 806 | >::type*) |
| 807 | : base(__a) |
| 808 | { |
| 809 | insert_after(cbefore_begin(), __f, __l); |
| 810 | } |
| 811 | |
| 812 | template <class _Tp, class _Alloc> |
| 813 | forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x) |
| 814 | : base(allocator_type( |
| 815 | __node_traits::select_on_container_copy_construction(__x.__alloc()) |
| 816 | ) |
| 817 | ) |
| 818 | { |
| 819 | insert_after(cbefore_begin(), __x.begin(), __x.end()); |
| 820 | } |
| 821 | |
| 822 | template <class _Tp, class _Alloc> |
| 823 | forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x, |
| 824 | const allocator_type& __a) |
| 825 | : base(__a) |
| 826 | { |
| 827 | insert_after(cbefore_begin(), __x.begin(), __x.end()); |
| 828 | } |
| 829 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 830 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 831 | |
| 832 | template <class _Tp, class _Alloc> |
| 833 | forward_list<_Tp, _Alloc>::forward_list(forward_list&& __x, |
| 834 | const allocator_type& __a) |
| 835 | : base(_STD::move(__x), __a) |
| 836 | { |
| 837 | if (base::__alloc() != __x.__alloc()) |
| 838 | { |
| 839 | typedef move_iterator<iterator> _I; |
| 840 | insert_after(cbefore_begin(), _I(__x.begin()), _I(__x.end())); |
| 841 | } |
| 842 | } |
| 843 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 844 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 845 | |
| 846 | template <class _Tp, class _Alloc> |
| 847 | forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il) |
| 848 | { |
| 849 | insert_after(cbefore_begin(), __il.begin(), __il.end()); |
| 850 | } |
| 851 | |
| 852 | template <class _Tp, class _Alloc> |
| 853 | forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il, |
| 854 | const allocator_type& __a) |
| 855 | : base(__a) |
| 856 | { |
| 857 | insert_after(cbefore_begin(), __il.begin(), __il.end()); |
| 858 | } |
| 859 | |
| 860 | template <class _Tp, class _Alloc> |
| 861 | forward_list<_Tp, _Alloc>& |
| 862 | forward_list<_Tp, _Alloc>::operator=(const forward_list& __x) |
| 863 | { |
| 864 | if (this != &__x) |
| 865 | { |
| 866 | base::__copy_assign_alloc(__x); |
| 867 | assign(__x.begin(), __x.end()); |
| 868 | } |
| 869 | return *this; |
| 870 | } |
| 871 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 872 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 873 | |
| 874 | template <class _Tp, class _Alloc> |
| 875 | void |
| 876 | forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, true_type) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 877 | _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 878 | { |
| 879 | clear(); |
| 880 | base::__move_assign_alloc(__x); |
| 881 | base::__before_begin()->__next_ = __x.__before_begin()->__next_; |
| 882 | __x.__before_begin()->__next_ = nullptr; |
| 883 | } |
| 884 | |
| 885 | template <class _Tp, class _Alloc> |
| 886 | void |
| 887 | forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, false_type) |
| 888 | { |
| 889 | if (base::__alloc() == __x.__alloc()) |
| 890 | __move_assign(__x, true_type()); |
| 891 | else |
| 892 | { |
| 893 | typedef move_iterator<iterator> _I; |
| 894 | assign(_I(__x.begin()), _I(__x.end())); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 899 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 900 | forward_list<_Tp, _Alloc>& |
| 901 | forward_list<_Tp, _Alloc>::operator=(forward_list&& __x) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 902 | _NOEXCEPT_( |
| 903 | __node_traits::propagate_on_container_move_assignment::value && |
| 904 | is_nothrow_move_assignable<allocator_type>::value) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 905 | { |
| 906 | __move_assign(__x, integral_constant<bool, |
| 907 | __node_traits::propagate_on_container_move_assignment::value>()); |
| 908 | return *this; |
| 909 | } |
| 910 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 911 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 912 | |
| 913 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 914 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 915 | forward_list<_Tp, _Alloc>& |
| 916 | forward_list<_Tp, _Alloc>::operator=(initializer_list<value_type> __il) |
| 917 | { |
| 918 | assign(__il.begin(), __il.end()); |
| 919 | return *this; |
| 920 | } |
| 921 | |
| 922 | template <class _Tp, class _Alloc> |
| 923 | template <class _InputIterator> |
| 924 | typename enable_if |
| 925 | < |
| 926 | __is_input_iterator<_InputIterator>::value, |
| 927 | void |
| 928 | >::type |
| 929 | forward_list<_Tp, _Alloc>::assign(_InputIterator __f, _InputIterator __l) |
| 930 | { |
| 931 | iterator __i = before_begin(); |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 932 | iterator __j = _STD::next(__i); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 933 | iterator __e = end(); |
| 934 | for (; __j != __e && __f != __l; ++__i, ++__j, ++__f) |
| 935 | *__j = *__f; |
| 936 | if (__j == __e) |
| 937 | insert_after(__i, __f, __l); |
| 938 | else |
| 939 | erase_after(__i, __e); |
| 940 | } |
| 941 | |
| 942 | template <class _Tp, class _Alloc> |
| 943 | void |
| 944 | forward_list<_Tp, _Alloc>::assign(size_type __n, const value_type& __v) |
| 945 | { |
| 946 | iterator __i = before_begin(); |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 947 | iterator __j = _STD::next(__i); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 948 | iterator __e = end(); |
| 949 | for (; __j != __e && __n > 0; --__n, ++__i, ++__j) |
| 950 | *__j = __v; |
| 951 | if (__j == __e) |
| 952 | insert_after(__i, __n, __v); |
| 953 | else |
| 954 | erase_after(__i, __e); |
| 955 | } |
| 956 | |
| 957 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 958 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 959 | void |
| 960 | forward_list<_Tp, _Alloc>::assign(initializer_list<value_type> __il) |
| 961 | { |
| 962 | assign(__il.begin(), __il.end()); |
| 963 | } |
| 964 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 965 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 966 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 967 | |
| 968 | template <class _Tp, class _Alloc> |
| 969 | template <class... _Args> |
| 970 | void |
| 971 | forward_list<_Tp, _Alloc>::emplace_front(_Args&&... __args) |
| 972 | { |
| 973 | __node_allocator& __a = base::__alloc(); |
| 974 | typedef __allocator_destructor<__node_allocator> _D; |
| 975 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 976 | __node_traits::construct(__a, _STD::addressof(__h->__value_), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 977 | _STD::forward<_Args>(__args)...); |
| 978 | __h->__next_ = base::__before_begin()->__next_; |
| 979 | base::__before_begin()->__next_ = __h.release(); |
| 980 | } |
| 981 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 982 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 983 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 984 | template <class _Tp, class _Alloc> |
| 985 | void |
| 986 | forward_list<_Tp, _Alloc>::push_front(value_type&& __v) |
| 987 | { |
| 988 | __node_allocator& __a = base::__alloc(); |
| 989 | typedef __allocator_destructor<__node_allocator> _D; |
| 990 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 991 | __node_traits::construct(__a, _STD::addressof(__h->__value_), _STD::move(__v)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 992 | __h->__next_ = base::__before_begin()->__next_; |
| 993 | base::__before_begin()->__next_ = __h.release(); |
| 994 | } |
| 995 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 996 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 997 | |
| 998 | template <class _Tp, class _Alloc> |
| 999 | void |
| 1000 | forward_list<_Tp, _Alloc>::push_front(const value_type& __v) |
| 1001 | { |
| 1002 | __node_allocator& __a = base::__alloc(); |
| 1003 | typedef __allocator_destructor<__node_allocator> _D; |
| 1004 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1005 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1006 | __h->__next_ = base::__before_begin()->__next_; |
| 1007 | base::__before_begin()->__next_ = __h.release(); |
| 1008 | } |
| 1009 | |
| 1010 | template <class _Tp, class _Alloc> |
| 1011 | void |
| 1012 | forward_list<_Tp, _Alloc>::pop_front() |
| 1013 | { |
| 1014 | __node_allocator& __a = base::__alloc(); |
| 1015 | __node_pointer __p = base::__before_begin()->__next_; |
| 1016 | base::__before_begin()->__next_ = __p->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1017 | __node_traits::destroy(__a, _STD::addressof(__p->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1018 | __node_traits::deallocate(__a, __p, 1); |
| 1019 | } |
| 1020 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1021 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1022 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1023 | |
| 1024 | template <class _Tp, class _Alloc> |
| 1025 | template <class... _Args> |
| 1026 | typename forward_list<_Tp, _Alloc>::iterator |
| 1027 | forward_list<_Tp, _Alloc>::emplace_after(const_iterator __p, _Args&&... __args) |
| 1028 | { |
| 1029 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 1030 | __node_allocator& __a = base::__alloc(); |
| 1031 | typedef __allocator_destructor<__node_allocator> _D; |
| 1032 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1033 | __node_traits::construct(__a, _STD::addressof(__h->__value_), |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1034 | _STD::forward<_Args>(__args)...); |
| 1035 | __h->__next_ = __r->__next_; |
| 1036 | __r->__next_ = __h.release(); |
| 1037 | return iterator(__r->__next_); |
| 1038 | } |
| 1039 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1040 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1041 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1042 | template <class _Tp, class _Alloc> |
| 1043 | typename forward_list<_Tp, _Alloc>::iterator |
| 1044 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, value_type&& __v) |
| 1045 | { |
| 1046 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 1047 | __node_allocator& __a = base::__alloc(); |
| 1048 | typedef __allocator_destructor<__node_allocator> _D; |
| 1049 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1050 | __node_traits::construct(__a, _STD::addressof(__h->__value_), _STD::move(__v)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1051 | __h->__next_ = __r->__next_; |
| 1052 | __r->__next_ = __h.release(); |
| 1053 | return iterator(__r->__next_); |
| 1054 | } |
| 1055 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 1056 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1057 | |
| 1058 | template <class _Tp, class _Alloc> |
| 1059 | typename forward_list<_Tp, _Alloc>::iterator |
| 1060 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __v) |
| 1061 | { |
| 1062 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 1063 | __node_allocator& __a = base::__alloc(); |
| 1064 | typedef __allocator_destructor<__node_allocator> _D; |
| 1065 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1066 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1067 | __h->__next_ = __r->__next_; |
| 1068 | __r->__next_ = __h.release(); |
| 1069 | return iterator(__r->__next_); |
| 1070 | } |
| 1071 | |
| 1072 | template <class _Tp, class _Alloc> |
| 1073 | typename forward_list<_Tp, _Alloc>::iterator |
| 1074 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n, |
| 1075 | const value_type& __v) |
| 1076 | { |
Howard Hinnant | ba590bd | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1077 | __node_pointer __r = const_cast<__node_pointer>(__p.__ptr_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1078 | if (__n > 0) |
| 1079 | { |
| 1080 | __node_allocator& __a = base::__alloc(); |
| 1081 | typedef __allocator_destructor<__node_allocator> _D; |
| 1082 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1083 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1084 | __node_pointer __first = __h.release(); |
| 1085 | __node_pointer __last = __first; |
| 1086 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1087 | try |
| 1088 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1089 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1090 | for (--__n; __n != 0; --__n, __last = __last->__next_) |
| 1091 | { |
| 1092 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1093 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1094 | __last->__next_ = __h.release(); |
| 1095 | } |
| 1096 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1097 | } |
| 1098 | catch (...) |
| 1099 | { |
| 1100 | while (__first != nullptr) |
| 1101 | { |
| 1102 | __node_pointer __next = __first->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1103 | __node_traits::destroy(__a, _STD::addressof(__first->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1104 | __node_traits::deallocate(__a, __first, 1); |
| 1105 | __first = __next; |
| 1106 | } |
| 1107 | throw; |
| 1108 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1109 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1110 | __last->__next_ = __r->__next_; |
| 1111 | __r->__next_ = __first; |
Howard Hinnant | ba590bd | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1112 | __r = __last; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1113 | } |
| 1114 | return iterator(__r); |
| 1115 | } |
| 1116 | |
| 1117 | template <class _Tp, class _Alloc> |
| 1118 | template <class _InputIterator> |
| 1119 | typename enable_if |
| 1120 | < |
| 1121 | __is_input_iterator<_InputIterator>::value, |
| 1122 | typename forward_list<_Tp, _Alloc>::iterator |
| 1123 | >::type |
| 1124 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, |
| 1125 | _InputIterator __f, _InputIterator __l) |
| 1126 | { |
Howard Hinnant | ba590bd | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1127 | __node_pointer __r = const_cast<__node_pointer>(__p.__ptr_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1128 | if (__f != __l) |
| 1129 | { |
| 1130 | __node_allocator& __a = base::__alloc(); |
| 1131 | typedef __allocator_destructor<__node_allocator> _D; |
| 1132 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1133 | __node_traits::construct(__a, _STD::addressof(__h->__value_), *__f); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1134 | __node_pointer __first = __h.release(); |
| 1135 | __node_pointer __last = __first; |
| 1136 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1137 | try |
| 1138 | { |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1139 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1140 | for (++__f; __f != __l; ++__f, __last = __last->__next_) |
| 1141 | { |
| 1142 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1143 | __node_traits::construct(__a, _STD::addressof(__h->__value_), *__f); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1144 | __last->__next_ = __h.release(); |
| 1145 | } |
| 1146 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1147 | } |
| 1148 | catch (...) |
| 1149 | { |
| 1150 | while (__first != nullptr) |
| 1151 | { |
| 1152 | __node_pointer __next = __first->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1153 | __node_traits::destroy(__a, _STD::addressof(__first->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1154 | __node_traits::deallocate(__a, __first, 1); |
| 1155 | __first = __next; |
| 1156 | } |
| 1157 | throw; |
| 1158 | } |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1159 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1160 | __last->__next_ = __r->__next_; |
| 1161 | __r->__next_ = __first; |
Howard Hinnant | ba590bd | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1162 | __r = __last; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1163 | } |
| 1164 | return iterator(__r); |
| 1165 | } |
| 1166 | |
| 1167 | template <class _Tp, class _Alloc> |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1168 | typename forward_list<_Tp, _Alloc>::iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1169 | forward_list<_Tp, _Alloc>::erase_after(const_iterator __f) |
| 1170 | { |
| 1171 | __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); |
| 1172 | __node_pointer __n = __p->__next_; |
| 1173 | __p->__next_ = __n->__next_; |
| 1174 | __node_allocator& __a = base::__alloc(); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1175 | __node_traits::destroy(__a, _STD::addressof(__n->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1176 | __node_traits::deallocate(__a, __n, 1); |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1177 | return iterator(__p->__next_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | template <class _Tp, class _Alloc> |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1181 | typename forward_list<_Tp, _Alloc>::iterator |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1182 | forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l) |
| 1183 | { |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1184 | __node_pointer __e = const_cast<__node_pointer>(__l.__ptr_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1185 | if (__f != __l) |
| 1186 | { |
| 1187 | __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); |
| 1188 | __node_pointer __n = __p->__next_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1189 | if (__n != __e) |
| 1190 | { |
| 1191 | __p->__next_ = __e; |
| 1192 | __node_allocator& __a = base::__alloc(); |
| 1193 | do |
| 1194 | { |
| 1195 | __p = __n->__next_; |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1196 | __node_traits::destroy(__a, _STD::addressof(__n->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1197 | __node_traits::deallocate(__a, __n, 1); |
| 1198 | __n = __p; |
| 1199 | } while (__n != __e); |
| 1200 | } |
| 1201 | } |
Howard Hinnant | 7a2523b | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1202 | return iterator(__e); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | template <class _Tp, class _Alloc> |
| 1206 | void |
| 1207 | forward_list<_Tp, _Alloc>::resize(size_type __n) |
| 1208 | { |
| 1209 | size_type __sz = 0; |
| 1210 | iterator __p = before_begin(); |
| 1211 | iterator __i = begin(); |
| 1212 | iterator __e = end(); |
| 1213 | for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz) |
| 1214 | ; |
| 1215 | if (__i != __e) |
| 1216 | erase_after(__p, __e); |
| 1217 | else |
| 1218 | { |
| 1219 | __n -= __sz; |
| 1220 | if (__n > 0) |
| 1221 | { |
| 1222 | __node_allocator& __a = base::__alloc(); |
| 1223 | typedef __allocator_destructor<__node_allocator> _D; |
| 1224 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 1225 | for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n, |
| 1226 | __ptr = __ptr->__next_) |
| 1227 | { |
| 1228 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1229 | __node_traits::construct(__a, _STD::addressof(__h->__value_)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1230 | __h->__next_ = nullptr; |
| 1231 | __ptr->__next_ = __h.release(); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | template <class _Tp, class _Alloc> |
| 1238 | void |
| 1239 | forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v) |
| 1240 | { |
| 1241 | size_type __sz = 0; |
| 1242 | iterator __p = before_begin(); |
| 1243 | iterator __i = begin(); |
| 1244 | iterator __e = end(); |
| 1245 | for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz) |
| 1246 | ; |
| 1247 | if (__i != __e) |
| 1248 | erase_after(__p, __e); |
| 1249 | else |
| 1250 | { |
| 1251 | __n -= __sz; |
| 1252 | if (__n > 0) |
| 1253 | { |
| 1254 | __node_allocator& __a = base::__alloc(); |
| 1255 | typedef __allocator_destructor<__node_allocator> _D; |
| 1256 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 1257 | for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n, |
| 1258 | __ptr = __ptr->__next_) |
| 1259 | { |
| 1260 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 2529d02 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1261 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1262 | __h->__next_ = nullptr; |
| 1263 | __ptr->__next_ = __h.release(); |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | template <class _Tp, class _Alloc> |
| 1270 | void |
| 1271 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1272 | forward_list& __x) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1273 | { |
| 1274 | if (!__x.empty()) |
| 1275 | { |
| 1276 | if (__p.__ptr_->__next_ != nullptr) |
| 1277 | { |
| 1278 | const_iterator __lm1 = __x.before_begin(); |
| 1279 | while (__lm1.__ptr_->__next_ != nullptr) |
| 1280 | ++__lm1; |
| 1281 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1282 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1283 | } |
| 1284 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1285 | const_cast<__node_pointer>(__x.__before_begin())->__next_; |
| 1286 | const_cast<__node_pointer>(__x.__before_begin())->__next_ = nullptr; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | template <class _Tp, class _Alloc> |
| 1291 | void |
| 1292 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1293 | forward_list& __x, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1294 | const_iterator __i) |
| 1295 | { |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 1296 | const_iterator __lm1 = _STD::next(__i); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1297 | if (__p != __i && __p != __lm1) |
| 1298 | { |
| 1299 | const_cast<__node_pointer>(__i.__ptr_)->__next_ = |
| 1300 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_; |
| 1301 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1302 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1303 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1304 | const_cast<__node_pointer>(__lm1.__ptr_); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | template <class _Tp, class _Alloc> |
| 1309 | void |
| 1310 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1311 | forward_list& __x, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1312 | const_iterator __f, const_iterator __l) |
| 1313 | { |
| 1314 | if (__f != __l && __p != __f) |
| 1315 | { |
| 1316 | const_iterator __lm1 = __f; |
| 1317 | while (__lm1.__ptr_->__next_ != __l.__ptr_) |
| 1318 | ++__lm1; |
| 1319 | if (__f != __lm1) |
| 1320 | { |
| 1321 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1322 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1323 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1324 | const_cast<__node_pointer>(__f.__ptr_)->__next_; |
| 1325 | const_cast<__node_pointer>(__f.__ptr_)->__next_ = |
| 1326 | const_cast<__node_pointer>(__l.__ptr_); |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | |
Howard Hinnant | 99b2f76 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 1331 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1332 | |
| 1333 | template <class _Tp, class _Alloc> |
| 1334 | inline _LIBCPP_INLINE_VISIBILITY |
| 1335 | void |
| 1336 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1337 | forward_list&& __x) |
| 1338 | { |
| 1339 | splice_after(__p, __x); |
| 1340 | } |
| 1341 | |
| 1342 | template <class _Tp, class _Alloc> |
| 1343 | inline _LIBCPP_INLINE_VISIBILITY |
| 1344 | void |
| 1345 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1346 | forward_list&& __x, |
| 1347 | const_iterator __i) |
| 1348 | { |
| 1349 | splice_after(__p, __x, __i); |
| 1350 | } |
| 1351 | |
| 1352 | template <class _Tp, class _Alloc> |
| 1353 | inline _LIBCPP_INLINE_VISIBILITY |
| 1354 | void |
| 1355 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1356 | forward_list&& __x, |
| 1357 | const_iterator __f, const_iterator __l) |
| 1358 | { |
| 1359 | splice_after(__p, __x, __f, __l); |
| 1360 | } |
| 1361 | |
| 1362 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1363 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1364 | template <class _Tp, class _Alloc> |
| 1365 | void |
| 1366 | forward_list<_Tp, _Alloc>::remove(const value_type& __v) |
| 1367 | { |
| 1368 | iterator __e = end(); |
| 1369 | for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;) |
| 1370 | { |
| 1371 | if (__i.__ptr_->__next_->__value_ == __v) |
| 1372 | { |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 1373 | iterator __j = _STD::next(__i, 2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1374 | for (; __j != __e && *__j == __v; ++__j) |
| 1375 | ; |
| 1376 | erase_after(__i, __j); |
| 1377 | if (__j == __e) |
| 1378 | break; |
| 1379 | __i = __j; |
| 1380 | } |
| 1381 | else |
| 1382 | ++__i; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | template <class _Tp, class _Alloc> |
| 1387 | template <class _Predicate> |
| 1388 | void |
| 1389 | forward_list<_Tp, _Alloc>::remove_if(_Predicate __pred) |
| 1390 | { |
| 1391 | iterator __e = end(); |
| 1392 | for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;) |
| 1393 | { |
| 1394 | if (__pred(__i.__ptr_->__next_->__value_)) |
| 1395 | { |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 1396 | iterator __j = _STD::next(__i, 2); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1397 | for (; __j != __e && __pred(*__j); ++__j) |
| 1398 | ; |
| 1399 | erase_after(__i, __j); |
| 1400 | if (__j == __e) |
| 1401 | break; |
| 1402 | __i = __j; |
| 1403 | } |
| 1404 | else |
| 1405 | ++__i; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | template <class _Tp, class _Alloc> |
| 1410 | template <class _BinaryPredicate> |
| 1411 | void |
| 1412 | forward_list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred) |
| 1413 | { |
| 1414 | for (iterator __i = begin(), __e = end(); __i != __e;) |
| 1415 | { |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 1416 | iterator __j = _STD::next(__i); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1417 | for (; __j != __e && __binary_pred(*__i, *__j); ++__j) |
| 1418 | ; |
| 1419 | if (__i.__ptr_->__next_ != __j.__ptr_) |
| 1420 | erase_after(__i, __j); |
| 1421 | __i = __j; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | template <class _Tp, class _Alloc> |
| 1426 | template <class _Compare> |
| 1427 | void |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1428 | forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1429 | { |
| 1430 | if (this != &__x) |
| 1431 | { |
| 1432 | base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_, |
| 1433 | __x.__before_begin()->__next_, |
| 1434 | __comp); |
| 1435 | __x.__before_begin()->__next_ = nullptr; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | template <class _Tp, class _Alloc> |
| 1440 | template <class _Compare> |
| 1441 | typename forward_list<_Tp, _Alloc>::__node_pointer |
| 1442 | forward_list<_Tp, _Alloc>::__merge(__node_pointer __f1, __node_pointer __f2, |
| 1443 | _Compare& __comp) |
| 1444 | { |
| 1445 | if (__f1 == nullptr) |
| 1446 | return __f2; |
| 1447 | if (__f2 == nullptr) |
| 1448 | return __f1; |
| 1449 | __node_pointer __r; |
| 1450 | if (__comp(__f2->__value_, __f1->__value_)) |
| 1451 | { |
| 1452 | __node_pointer __t = __f2; |
| 1453 | while (__t->__next_ != nullptr && |
| 1454 | __comp(__t->__next_->__value_, __f1->__value_)) |
| 1455 | __t = __t->__next_; |
| 1456 | __r = __f2; |
| 1457 | __f2 = __t->__next_; |
| 1458 | __t->__next_ = __f1; |
| 1459 | } |
| 1460 | else |
| 1461 | __r = __f1; |
| 1462 | __node_pointer __p = __f1; |
| 1463 | __f1 = __f1->__next_; |
| 1464 | while (__f1 != nullptr && __f2 != nullptr) |
| 1465 | { |
| 1466 | if (__comp(__f2->__value_, __f1->__value_)) |
| 1467 | { |
| 1468 | __node_pointer __t = __f2; |
| 1469 | while (__t->__next_ != nullptr && |
| 1470 | __comp(__t->__next_->__value_, __f1->__value_)) |
| 1471 | __t = __t->__next_; |
| 1472 | __p->__next_ = __f2; |
| 1473 | __f2 = __t->__next_; |
| 1474 | __t->__next_ = __f1; |
| 1475 | } |
| 1476 | __p = __f1; |
| 1477 | __f1 = __f1->__next_; |
| 1478 | } |
| 1479 | if (__f2 != nullptr) |
| 1480 | __p->__next_ = __f2; |
| 1481 | return __r; |
| 1482 | } |
| 1483 | |
| 1484 | template <class _Tp, class _Alloc> |
| 1485 | template <class _Compare> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1486 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1487 | void |
| 1488 | forward_list<_Tp, _Alloc>::sort(_Compare __comp) |
| 1489 | { |
| 1490 | base::__before_begin()->__next_ = __sort(base::__before_begin()->__next_, |
| 1491 | _STD::distance(begin(), end()), __comp); |
| 1492 | } |
| 1493 | |
| 1494 | template <class _Tp, class _Alloc> |
| 1495 | template <class _Compare> |
| 1496 | typename forward_list<_Tp, _Alloc>::__node_pointer |
| 1497 | forward_list<_Tp, _Alloc>::__sort(__node_pointer __f1, difference_type __sz, |
| 1498 | _Compare& __comp) |
| 1499 | { |
| 1500 | switch (__sz) |
| 1501 | { |
| 1502 | case 0: |
| 1503 | case 1: |
| 1504 | return __f1; |
| 1505 | case 2: |
| 1506 | if (__comp(__f1->__next_->__value_, __f1->__value_)) |
| 1507 | { |
| 1508 | __node_pointer __t = __f1->__next_; |
| 1509 | __t->__next_ = __f1; |
| 1510 | __f1->__next_ = nullptr; |
| 1511 | __f1 = __t; |
| 1512 | } |
| 1513 | return __f1; |
| 1514 | } |
| 1515 | difference_type __sz1 = __sz / 2; |
| 1516 | difference_type __sz2 = __sz - __sz1; |
Howard Hinnant | 6cf5d8c | 2011-02-14 19:12:38 +0000 | [diff] [blame] | 1517 | __node_pointer __t = _STD::next(iterator(__f1), __sz1 - 1).__ptr_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1518 | __node_pointer __f2 = __t->__next_; |
| 1519 | __t->__next_ = nullptr; |
| 1520 | return __merge(__sort(__f1, __sz1, __comp), |
| 1521 | __sort(__f2, __sz2, __comp), __comp); |
| 1522 | } |
| 1523 | |
| 1524 | template <class _Tp, class _Alloc> |
| 1525 | void |
Howard Hinnant | 8790cab | 2011-06-02 16:44:28 +0000 | [diff] [blame] | 1526 | forward_list<_Tp, _Alloc>::reverse() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1527 | { |
| 1528 | __node_pointer __p = base::__before_begin()->__next_; |
| 1529 | if (__p != nullptr) |
| 1530 | { |
| 1531 | __node_pointer __f = __p->__next_; |
| 1532 | __p->__next_ = nullptr; |
| 1533 | while (__f != nullptr) |
| 1534 | { |
| 1535 | __node_pointer __t = __f->__next_; |
| 1536 | __f->__next_ = __p; |
| 1537 | __p = __f; |
| 1538 | __f = __t; |
| 1539 | } |
| 1540 | base::__before_begin()->__next_ = __p; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | template <class _Tp, class _Alloc> |
| 1545 | bool operator==(const forward_list<_Tp, _Alloc>& __x, |
| 1546 | const forward_list<_Tp, _Alloc>& __y) |
| 1547 | { |
| 1548 | typedef forward_list<_Tp, _Alloc> _C; |
| 1549 | typedef typename _C::const_iterator _I; |
| 1550 | _I __ix = __x.begin(); |
| 1551 | _I __ex = __x.end(); |
| 1552 | _I __iy = __y.begin(); |
| 1553 | _I __ey = __y.end(); |
| 1554 | for (; __ix != __ex && __iy != __ey; ++__ix, ++__iy) |
| 1555 | if (!(*__ix == *__iy)) |
| 1556 | return false; |
| 1557 | return (__ix == __ex) == (__iy == __ey); |
| 1558 | } |
| 1559 | |
| 1560 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1561 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1562 | bool operator!=(const forward_list<_Tp, _Alloc>& __x, |
| 1563 | const forward_list<_Tp, _Alloc>& __y) |
| 1564 | { |
| 1565 | return !(__x == __y); |
| 1566 | } |
| 1567 | |
| 1568 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1569 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1570 | bool operator< (const forward_list<_Tp, _Alloc>& __x, |
| 1571 | const forward_list<_Tp, _Alloc>& __y) |
| 1572 | { |
| 1573 | return _STD::lexicographical_compare(__x.begin(), __x.end(), |
| 1574 | __y.begin(), __y.end()); |
| 1575 | } |
| 1576 | |
| 1577 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1578 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1579 | bool operator> (const forward_list<_Tp, _Alloc>& __x, |
| 1580 | const forward_list<_Tp, _Alloc>& __y) |
| 1581 | { |
| 1582 | return __y < __x; |
| 1583 | } |
| 1584 | |
| 1585 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1586 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1587 | bool operator>=(const forward_list<_Tp, _Alloc>& __x, |
| 1588 | const forward_list<_Tp, _Alloc>& __y) |
| 1589 | { |
| 1590 | return !(__x < __y); |
| 1591 | } |
| 1592 | |
| 1593 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1594 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1595 | bool operator<=(const forward_list<_Tp, _Alloc>& __x, |
| 1596 | const forward_list<_Tp, _Alloc>& __y) |
| 1597 | { |
| 1598 | return !(__y < __x); |
| 1599 | } |
| 1600 | |
| 1601 | template <class _Tp, class _Alloc> |
Howard Hinnant | 42a63a7 | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1602 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1603 | void |
| 1604 | swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y) |
Howard Hinnant | b965fed | 2011-06-03 16:20:53 +0000 | [diff] [blame] | 1605 | _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1606 | { |
| 1607 | __x.swap(__y); |
| 1608 | } |
| 1609 | |
| 1610 | _LIBCPP_END_NAMESPACE_STD |
| 1611 | |
| 1612 | #endif // _LIBCPP_FORWARD_LIST |