1 |
424 |
jeremybenn |
// <forward_list.h> -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
|
4 |
|
|
//
|
5 |
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
6 |
|
|
// software; you can redistribute it and/or modify it under the
|
7 |
|
|
// terms of the GNU General Public License as published by the
|
8 |
|
|
// Free Software Foundation; either version 3, or (at your option)
|
9 |
|
|
// any later version.
|
10 |
|
|
|
11 |
|
|
// This library is distributed in the hope that it will be useful,
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
// GNU General Public License for more details.
|
15 |
|
|
|
16 |
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
17 |
|
|
// permissions described in the GCC Runtime Library Exception, version
|
18 |
|
|
// 3.1, as published by the Free Software Foundation.
|
19 |
|
|
|
20 |
|
|
// You should have received a copy of the GNU General Public License and
|
21 |
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
22 |
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
23 |
|
|
// <http://www.gnu.org/licenses/>.
|
24 |
|
|
|
25 |
|
|
/** @file forward_list.h
|
26 |
|
|
* This is a Standard C++ Library header.
|
27 |
|
|
*/
|
28 |
|
|
|
29 |
|
|
#ifndef _FORWARD_LIST_H
|
30 |
|
|
#define _FORWARD_LIST_H 1
|
31 |
|
|
|
32 |
|
|
#pragma GCC system_header
|
33 |
|
|
|
34 |
|
|
#include <memory>
|
35 |
|
|
#include <initializer_list>
|
36 |
|
|
|
37 |
|
|
_GLIBCXX_BEGIN_NAMESPACE(std)
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* @brief A helper basic node class for %forward_list.
|
41 |
|
|
* This is just a linked list with nothing inside it.
|
42 |
|
|
* There are purely list shuffling utility methods here.
|
43 |
|
|
*/
|
44 |
|
|
struct _Fwd_list_node_base
|
45 |
|
|
{
|
46 |
|
|
_Fwd_list_node_base() : _M_next(0) { }
|
47 |
|
|
|
48 |
|
|
_Fwd_list_node_base* _M_next;
|
49 |
|
|
|
50 |
|
|
static void
|
51 |
|
|
swap(_Fwd_list_node_base& __x, _Fwd_list_node_base& __y)
|
52 |
|
|
{ std::swap(__x._M_next, __y._M_next); }
|
53 |
|
|
|
54 |
|
|
_Fwd_list_node_base*
|
55 |
|
|
_M_transfer_after(_Fwd_list_node_base* __begin)
|
56 |
|
|
{
|
57 |
|
|
_Fwd_list_node_base* __end = __begin;
|
58 |
|
|
while (__end && __end->_M_next)
|
59 |
|
|
__end = __end->_M_next;
|
60 |
|
|
return _M_transfer_after(__begin, __end);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
_Fwd_list_node_base*
|
64 |
|
|
_M_transfer_after(_Fwd_list_node_base* __begin,
|
65 |
|
|
_Fwd_list_node_base* __end)
|
66 |
|
|
{
|
67 |
|
|
_Fwd_list_node_base* __keep = __begin->_M_next;
|
68 |
|
|
if (__end)
|
69 |
|
|
{
|
70 |
|
|
__begin->_M_next = __end->_M_next;
|
71 |
|
|
__end->_M_next = _M_next;
|
72 |
|
|
}
|
73 |
|
|
else
|
74 |
|
|
__begin->_M_next = 0;
|
75 |
|
|
_M_next = __keep;
|
76 |
|
|
return __end;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
void
|
80 |
|
|
_M_reverse_after()
|
81 |
|
|
{
|
82 |
|
|
_Fwd_list_node_base* __tail = _M_next;
|
83 |
|
|
if (!__tail)
|
84 |
|
|
return;
|
85 |
|
|
while (_Fwd_list_node_base* __temp = __tail->_M_next)
|
86 |
|
|
{
|
87 |
|
|
_Fwd_list_node_base* __keep = _M_next;
|
88 |
|
|
_M_next = __temp;
|
89 |
|
|
__tail->_M_next = __temp->_M_next;
|
90 |
|
|
_M_next->_M_next = __keep;
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
|
|
};
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* @brief A helper node class for %forward_list.
|
97 |
|
|
* This is just a linked list with a data value in each node.
|
98 |
|
|
* There is a sorting utility method.
|
99 |
|
|
*/
|
100 |
|
|
template<typename _Tp>
|
101 |
|
|
struct _Fwd_list_node
|
102 |
|
|
: public _Fwd_list_node_base
|
103 |
|
|
{
|
104 |
|
|
template<typename... _Args>
|
105 |
|
|
_Fwd_list_node(_Args&&... __args)
|
106 |
|
|
: _Fwd_list_node_base(),
|
107 |
|
|
_M_value(std::forward<_Args>(__args)...) { }
|
108 |
|
|
|
109 |
|
|
_Tp _M_value;
|
110 |
|
|
};
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* @brief A forward_list::iterator.
|
114 |
|
|
*
|
115 |
|
|
* All the functions are op overloads.
|
116 |
|
|
*/
|
117 |
|
|
template<typename _Tp>
|
118 |
|
|
struct _Fwd_list_iterator
|
119 |
|
|
{
|
120 |
|
|
typedef _Fwd_list_iterator<_Tp> _Self;
|
121 |
|
|
typedef _Fwd_list_node<_Tp> _Node;
|
122 |
|
|
|
123 |
|
|
typedef _Tp value_type;
|
124 |
|
|
typedef _Tp* pointer;
|
125 |
|
|
typedef _Tp& reference;
|
126 |
|
|
typedef ptrdiff_t difference_type;
|
127 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
128 |
|
|
|
129 |
|
|
_Fwd_list_iterator()
|
130 |
|
|
: _M_node() { }
|
131 |
|
|
|
132 |
|
|
explicit
|
133 |
|
|
_Fwd_list_iterator(_Fwd_list_node_base* __n)
|
134 |
|
|
: _M_node(__n) { }
|
135 |
|
|
|
136 |
|
|
reference
|
137 |
|
|
operator*() const
|
138 |
|
|
{ return static_cast<_Node*>(this->_M_node)->_M_value; }
|
139 |
|
|
|
140 |
|
|
pointer
|
141 |
|
|
operator->() const
|
142 |
|
|
{ return &static_cast<_Node*>(this->_M_node)->_M_value; }
|
143 |
|
|
|
144 |
|
|
_Self&
|
145 |
|
|
operator++()
|
146 |
|
|
{
|
147 |
|
|
_M_node = _M_node->_M_next;
|
148 |
|
|
return *this;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
_Self
|
152 |
|
|
operator++(int)
|
153 |
|
|
{
|
154 |
|
|
_Self __tmp(*this);
|
155 |
|
|
_M_node = _M_node->_M_next;
|
156 |
|
|
return __tmp;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
bool
|
160 |
|
|
operator==(const _Self& __x) const
|
161 |
|
|
{ return _M_node == __x._M_node; }
|
162 |
|
|
|
163 |
|
|
bool
|
164 |
|
|
operator!=(const _Self& __x) const
|
165 |
|
|
{ return _M_node != __x._M_node; }
|
166 |
|
|
|
167 |
|
|
_Self
|
168 |
|
|
_M_next() const
|
169 |
|
|
{
|
170 |
|
|
if (_M_node)
|
171 |
|
|
return _Fwd_list_iterator(_M_node->_M_next);
|
172 |
|
|
else
|
173 |
|
|
return _Fwd_list_iterator(0);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
_Fwd_list_node_base* _M_node;
|
177 |
|
|
};
|
178 |
|
|
|
179 |
|
|
/**
|
180 |
|
|
* @brief A forward_list::const_iterator.
|
181 |
|
|
*
|
182 |
|
|
* All the functions are op overloads.
|
183 |
|
|
*/
|
184 |
|
|
template<typename _Tp>
|
185 |
|
|
struct _Fwd_list_const_iterator
|
186 |
|
|
{
|
187 |
|
|
typedef _Fwd_list_const_iterator<_Tp> _Self;
|
188 |
|
|
typedef const _Fwd_list_node<_Tp> _Node;
|
189 |
|
|
typedef _Fwd_list_iterator<_Tp> iterator;
|
190 |
|
|
|
191 |
|
|
typedef _Tp value_type;
|
192 |
|
|
typedef const _Tp* pointer;
|
193 |
|
|
typedef const _Tp& reference;
|
194 |
|
|
typedef ptrdiff_t difference_type;
|
195 |
|
|
typedef std::forward_iterator_tag iterator_category;
|
196 |
|
|
|
197 |
|
|
_Fwd_list_const_iterator()
|
198 |
|
|
: _M_node() { }
|
199 |
|
|
|
200 |
|
|
explicit
|
201 |
|
|
_Fwd_list_const_iterator(const _Fwd_list_node_base* __n)
|
202 |
|
|
: _M_node(__n) { }
|
203 |
|
|
|
204 |
|
|
_Fwd_list_const_iterator(const iterator& __iter)
|
205 |
|
|
: _M_node(__iter._M_node) { }
|
206 |
|
|
|
207 |
|
|
reference
|
208 |
|
|
operator*() const
|
209 |
|
|
{ return static_cast<_Node*>(this->_M_node)->_M_value; }
|
210 |
|
|
|
211 |
|
|
pointer
|
212 |
|
|
operator->() const
|
213 |
|
|
{ return &static_cast<_Node*>(this->_M_node)->_M_value; }
|
214 |
|
|
|
215 |
|
|
_Self&
|
216 |
|
|
operator++()
|
217 |
|
|
{
|
218 |
|
|
_M_node = _M_node->_M_next;
|
219 |
|
|
return *this;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
_Self
|
223 |
|
|
operator++(int)
|
224 |
|
|
{
|
225 |
|
|
_Self __tmp(*this);
|
226 |
|
|
_M_node = _M_node->_M_next;
|
227 |
|
|
return __tmp;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
bool
|
231 |
|
|
operator==(const _Self& __x) const
|
232 |
|
|
{ return _M_node == __x._M_node; }
|
233 |
|
|
|
234 |
|
|
bool
|
235 |
|
|
operator!=(const _Self& __x) const
|
236 |
|
|
{ return _M_node != __x._M_node; }
|
237 |
|
|
|
238 |
|
|
_Self
|
239 |
|
|
_M_next() const
|
240 |
|
|
{
|
241 |
|
|
if (this->_M_node)
|
242 |
|
|
return _Fwd_list_const_iterator(_M_node->_M_next);
|
243 |
|
|
else
|
244 |
|
|
return _Fwd_list_const_iterator(0);
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
const _Fwd_list_node_base* _M_node;
|
248 |
|
|
};
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* @brief Forward list iterator equality comparison.
|
252 |
|
|
*/
|
253 |
|
|
template<typename _Tp>
|
254 |
|
|
inline bool
|
255 |
|
|
operator==(const _Fwd_list_iterator<_Tp>& __x,
|
256 |
|
|
const _Fwd_list_const_iterator<_Tp>& __y)
|
257 |
|
|
{ return __x._M_node == __y._M_node; }
|
258 |
|
|
|
259 |
|
|
/**
|
260 |
|
|
* @brief Forward list iterator inequality comparison.
|
261 |
|
|
*/
|
262 |
|
|
template<typename _Tp>
|
263 |
|
|
inline bool
|
264 |
|
|
operator!=(const _Fwd_list_iterator<_Tp>& __x,
|
265 |
|
|
const _Fwd_list_const_iterator<_Tp>& __y)
|
266 |
|
|
{ return __x._M_node != __y._M_node; }
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
* @brief Base class for %forward_list.
|
270 |
|
|
*/
|
271 |
|
|
template<typename _Tp, typename _Alloc>
|
272 |
|
|
struct _Fwd_list_base
|
273 |
|
|
{
|
274 |
|
|
protected:
|
275 |
|
|
typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
|
276 |
|
|
|
277 |
|
|
typedef typename _Alloc::template
|
278 |
|
|
rebind<_Fwd_list_node<_Tp>>::other _Node_alloc_type;
|
279 |
|
|
|
280 |
|
|
struct _Fwd_list_impl
|
281 |
|
|
: public _Node_alloc_type
|
282 |
|
|
{
|
283 |
|
|
_Fwd_list_node_base _M_head;
|
284 |
|
|
|
285 |
|
|
_Fwd_list_impl()
|
286 |
|
|
: _Node_alloc_type(), _M_head()
|
287 |
|
|
{ }
|
288 |
|
|
|
289 |
|
|
_Fwd_list_impl(const _Node_alloc_type& __a)
|
290 |
|
|
: _Node_alloc_type(__a), _M_head()
|
291 |
|
|
{ }
|
292 |
|
|
};
|
293 |
|
|
|
294 |
|
|
_Fwd_list_impl _M_impl;
|
295 |
|
|
|
296 |
|
|
public:
|
297 |
|
|
typedef _Fwd_list_iterator<_Tp> iterator;
|
298 |
|
|
typedef _Fwd_list_const_iterator<_Tp> const_iterator;
|
299 |
|
|
typedef _Fwd_list_node<_Tp> _Node;
|
300 |
|
|
|
301 |
|
|
_Node_alloc_type&
|
302 |
|
|
_M_get_Node_allocator()
|
303 |
|
|
{ return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
|
304 |
|
|
|
305 |
|
|
const _Node_alloc_type&
|
306 |
|
|
_M_get_Node_allocator() const
|
307 |
|
|
{ return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
|
308 |
|
|
|
309 |
|
|
_Fwd_list_base()
|
310 |
|
|
: _M_impl()
|
311 |
|
|
{ this->_M_impl._M_head._M_next = 0; }
|
312 |
|
|
|
313 |
|
|
_Fwd_list_base(const _Alloc& __a)
|
314 |
|
|
: _M_impl(__a)
|
315 |
|
|
{ this->_M_impl._M_head._M_next = 0; }
|
316 |
|
|
|
317 |
|
|
_Fwd_list_base(const _Fwd_list_base& __lst, const _Alloc& __a);
|
318 |
|
|
|
319 |
|
|
_Fwd_list_base(_Fwd_list_base&& __lst, const _Alloc& __a)
|
320 |
|
|
: _M_impl(__a)
|
321 |
|
|
{ _Fwd_list_node_base::swap(this->_M_impl._M_head,
|
322 |
|
|
__lst._M_impl._M_head); }
|
323 |
|
|
|
324 |
|
|
_Fwd_list_base(_Fwd_list_base&& __lst)
|
325 |
|
|
: _M_impl(__lst._M_get_Node_allocator())
|
326 |
|
|
{ _Fwd_list_node_base::swap(this->_M_impl._M_head,
|
327 |
|
|
__lst._M_impl._M_head); }
|
328 |
|
|
|
329 |
|
|
~_Fwd_list_base()
|
330 |
|
|
{ _M_erase_after(&_M_impl._M_head, 0); }
|
331 |
|
|
|
332 |
|
|
protected:
|
333 |
|
|
|
334 |
|
|
_Node*
|
335 |
|
|
_M_get_node()
|
336 |
|
|
{ return _M_get_Node_allocator().allocate(1); }
|
337 |
|
|
|
338 |
|
|
template<typename... _Args>
|
339 |
|
|
_Node*
|
340 |
|
|
_M_create_node(_Args&&... __args)
|
341 |
|
|
{
|
342 |
|
|
_Node* __node = this->_M_get_node();
|
343 |
|
|
__try
|
344 |
|
|
{
|
345 |
|
|
_M_get_Node_allocator().construct(__node,
|
346 |
|
|
std::forward<_Args>(__args)...);
|
347 |
|
|
__node->_M_next = 0;
|
348 |
|
|
}
|
349 |
|
|
__catch(...)
|
350 |
|
|
{
|
351 |
|
|
this->_M_put_node(__node);
|
352 |
|
|
__throw_exception_again;
|
353 |
|
|
}
|
354 |
|
|
return __node;
|
355 |
|
|
}
|
356 |
|
|
|
357 |
|
|
template<typename... _Args>
|
358 |
|
|
_Fwd_list_node_base*
|
359 |
|
|
_M_insert_after(const_iterator __pos, _Args&&... __args);
|
360 |
|
|
|
361 |
|
|
void
|
362 |
|
|
_M_put_node(_Node* __p)
|
363 |
|
|
{ _M_get_Node_allocator().deallocate(__p, 1); }
|
364 |
|
|
|
365 |
|
|
void
|
366 |
|
|
_M_erase_after(_Fwd_list_node_base* __pos);
|
367 |
|
|
|
368 |
|
|
void
|
369 |
|
|
_M_erase_after(_Fwd_list_node_base* __pos,
|
370 |
|
|
_Fwd_list_node_base* __last);
|
371 |
|
|
};
|
372 |
|
|
|
373 |
|
|
/**
|
374 |
|
|
* @brief A standard container with linear time access to elements,
|
375 |
|
|
* and fixed time insertion/deletion at any point in the sequence.
|
376 |
|
|
*
|
377 |
|
|
* @ingroup sequences
|
378 |
|
|
*
|
379 |
|
|
* Meets the requirements of a <a href="tables.html#65">container</a>, a
|
380 |
|
|
* <a href="tables.html#67">sequence</a>, including the
|
381 |
|
|
* <a href="tables.html#68">optional sequence requirements</a> with the
|
382 |
|
|
* %exception of @c at and @c operator[].
|
383 |
|
|
*
|
384 |
|
|
* This is a @e singly @e linked %list. Traversal up the
|
385 |
|
|
* %list requires linear time, but adding and removing elements (or
|
386 |
|
|
* @e nodes) is done in constant time, regardless of where the
|
387 |
|
|
* change takes place. Unlike std::vector and std::deque,
|
388 |
|
|
* random-access iterators are not provided, so subscripting ( @c
|
389 |
|
|
* [] ) access is not allowed. For algorithms which only need
|
390 |
|
|
* sequential access, this lack makes no difference.
|
391 |
|
|
*
|
392 |
|
|
* Also unlike the other standard containers, std::forward_list provides
|
393 |
|
|
* specialized algorithms %unique to linked lists, such as
|
394 |
|
|
* splicing, sorting, and in-place reversal.
|
395 |
|
|
*
|
396 |
|
|
* A couple points on memory allocation for forward_list<Tp>:
|
397 |
|
|
*
|
398 |
|
|
* First, we never actually allocate a Tp, we allocate
|
399 |
|
|
* Fwd_list_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
|
400 |
|
|
* that after elements from %forward_list<X,Alloc1> are spliced into
|
401 |
|
|
* %forward_list<X,Alloc2>, destroying the memory of the second %list is a
|
402 |
|
|
* valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
|
403 |
|
|
*/
|
404 |
|
|
template<typename _Tp, typename _Alloc = allocator<_Tp> >
|
405 |
|
|
class forward_list : private _Fwd_list_base<_Tp, _Alloc>
|
406 |
|
|
{
|
407 |
|
|
private:
|
408 |
|
|
typedef _Fwd_list_base<_Tp, _Alloc> _Base;
|
409 |
|
|
typedef _Fwd_list_node<_Tp> _Node;
|
410 |
|
|
typedef _Fwd_list_node_base _Node_base;
|
411 |
|
|
typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
|
412 |
|
|
|
413 |
|
|
public:
|
414 |
|
|
// types:
|
415 |
|
|
typedef _Tp value_type;
|
416 |
|
|
typedef typename _Tp_alloc_type::pointer pointer;
|
417 |
|
|
typedef typename _Tp_alloc_type::const_pointer const_pointer;
|
418 |
|
|
typedef typename _Tp_alloc_type::reference reference;
|
419 |
|
|
typedef typename _Tp_alloc_type::const_reference const_reference;
|
420 |
|
|
|
421 |
|
|
typedef _Fwd_list_iterator<_Tp> iterator;
|
422 |
|
|
typedef _Fwd_list_const_iterator<_Tp> const_iterator;
|
423 |
|
|
typedef std::size_t size_type;
|
424 |
|
|
typedef std::ptrdiff_t difference_type;
|
425 |
|
|
typedef _Alloc allocator_type;
|
426 |
|
|
|
427 |
|
|
// 23.2.3.1 construct/copy/destroy:
|
428 |
|
|
|
429 |
|
|
/**
|
430 |
|
|
* @brief Creates a %forward_list with no elements.
|
431 |
|
|
* @param al An allocator object.
|
432 |
|
|
*/
|
433 |
|
|
explicit
|
434 |
|
|
forward_list(const _Alloc& __al = _Alloc())
|
435 |
|
|
: _Base(__al)
|
436 |
|
|
{ }
|
437 |
|
|
|
438 |
|
|
/**
|
439 |
|
|
* @brief Copy constructor with allocator argument.
|
440 |
|
|
* @param list Input list to copy.
|
441 |
|
|
* @param al An allocator object.
|
442 |
|
|
*/
|
443 |
|
|
forward_list(const forward_list& __list, const _Alloc& __al)
|
444 |
|
|
: _Base(__list, __al)
|
445 |
|
|
{ }
|
446 |
|
|
|
447 |
|
|
/**
|
448 |
|
|
* @brief Move constructor with allocator argument.
|
449 |
|
|
* @param list Input list to move.
|
450 |
|
|
* @param al An allocator object.
|
451 |
|
|
*/
|
452 |
|
|
forward_list(forward_list&& __list, const _Alloc& __al)
|
453 |
|
|
: _Base(std::forward<_Base>(__list), __al)
|
454 |
|
|
{ }
|
455 |
|
|
|
456 |
|
|
/**
|
457 |
|
|
* @brief Creates a %forward_list with default constructed elements.
|
458 |
|
|
* @param n The number of elements to initially create.
|
459 |
|
|
*
|
460 |
|
|
* This constructor creates the %forward_list with @a n default
|
461 |
|
|
* constructed elements.
|
462 |
|
|
*/
|
463 |
|
|
explicit
|
464 |
|
|
forward_list(size_type __n);
|
465 |
|
|
|
466 |
|
|
/**
|
467 |
|
|
* @brief Creates a %forward_list with copies of an exemplar element.
|
468 |
|
|
* @param n The number of elements to initially create.
|
469 |
|
|
* @param value An element to copy.
|
470 |
|
|
* @param al An allocator object.
|
471 |
|
|
*
|
472 |
|
|
* This constructor fills the %forward_list with @a n copies of @a
|
473 |
|
|
* value.
|
474 |
|
|
*/
|
475 |
|
|
forward_list(size_type __n, const _Tp& __value,
|
476 |
|
|
const _Alloc& __al = _Alloc())
|
477 |
|
|
: _Base(__al)
|
478 |
|
|
{ _M_fill_initialize(__n, __value); }
|
479 |
|
|
|
480 |
|
|
/**
|
481 |
|
|
* @brief Builds a %forward_list from a range.
|
482 |
|
|
* @param first An input iterator.
|
483 |
|
|
* @param last An input iterator.
|
484 |
|
|
* @param al An allocator object.
|
485 |
|
|
*
|
486 |
|
|
* Create a %forward_list consisting of copies of the elements from
|
487 |
|
|
* [@a first,@a last). This is linear in N (where N is
|
488 |
|
|
* distance(@a first,@a last)).
|
489 |
|
|
*/
|
490 |
|
|
template<typename _InputIterator>
|
491 |
|
|
forward_list(_InputIterator __first, _InputIterator __last,
|
492 |
|
|
const _Alloc& __al = _Alloc())
|
493 |
|
|
: _Base(__al)
|
494 |
|
|
{
|
495 |
|
|
// Check whether it's an integral type. If so, it's not an iterator.
|
496 |
|
|
typedef typename std::__is_integer<_InputIterator>::__type _Integral;
|
497 |
|
|
_M_initialize_dispatch(__first, __last, _Integral());
|
498 |
|
|
}
|
499 |
|
|
|
500 |
|
|
/**
|
501 |
|
|
* @brief The %forward_list copy constructor.
|
502 |
|
|
* @param list A %forward_list of identical element and allocator
|
503 |
|
|
* types.
|
504 |
|
|
*
|
505 |
|
|
* The newly-created %forward_list uses a copy of the allocation
|
506 |
|
|
* object used by @a list.
|
507 |
|
|
*/
|
508 |
|
|
forward_list(const forward_list& __list)
|
509 |
|
|
: _Base(__list._M_get_Node_allocator())
|
510 |
|
|
{ _M_initialize_dispatch(__list.begin(), __list.end(), __false_type()); }
|
511 |
|
|
|
512 |
|
|
/**
|
513 |
|
|
* @brief The %forward_list move constructor.
|
514 |
|
|
* @param list A %forward_list of identical element and allocator
|
515 |
|
|
* types.
|
516 |
|
|
*
|
517 |
|
|
* The newly-created %forward_list contains the exact contents of @a
|
518 |
|
|
* forward_list. The contents of @a list are a valid, but unspecified
|
519 |
|
|
* %forward_list.
|
520 |
|
|
*/
|
521 |
|
|
forward_list(forward_list&& __list)
|
522 |
|
|
: _Base(std::forward<_Base>(__list)) { }
|
523 |
|
|
|
524 |
|
|
/**
|
525 |
|
|
* @brief Builds a %forward_list from an initializer_list
|
526 |
|
|
* @param il An initializer_list of value_type.
|
527 |
|
|
* @param al An allocator object.
|
528 |
|
|
*
|
529 |
|
|
* Create a %forward_list consisting of copies of the elements
|
530 |
|
|
* in the initializer_list @a il. This is linear in il.size().
|
531 |
|
|
*/
|
532 |
|
|
forward_list(std::initializer_list<_Tp> __il,
|
533 |
|
|
const _Alloc& __al = _Alloc())
|
534 |
|
|
: _Base(__al)
|
535 |
|
|
{ _M_initialize_dispatch(__il.begin(), __il.end(), __false_type()); }
|
536 |
|
|
|
537 |
|
|
/**
|
538 |
|
|
* @brief The forward_list dtor.
|
539 |
|
|
*/
|
540 |
|
|
~forward_list()
|
541 |
|
|
{ }
|
542 |
|
|
|
543 |
|
|
/**
|
544 |
|
|
* @brief The %forward_list assignment operator.
|
545 |
|
|
* @param list A %forward_list of identical element and allocator
|
546 |
|
|
* types.
|
547 |
|
|
*
|
548 |
|
|
* All the elements of @a list are copied, but unlike the copy
|
549 |
|
|
* constructor, the allocator object is not copied.
|
550 |
|
|
*/
|
551 |
|
|
forward_list&
|
552 |
|
|
operator=(const forward_list& __list);
|
553 |
|
|
|
554 |
|
|
/**
|
555 |
|
|
* @brief The %forward_list move assignment operator.
|
556 |
|
|
* @param list A %forward_list of identical element and allocator
|
557 |
|
|
* types.
|
558 |
|
|
*
|
559 |
|
|
* The contents of @a list are moved into this %forward_list
|
560 |
|
|
* (without copying). @a list is a valid, but unspecified
|
561 |
|
|
* %forward_list
|
562 |
|
|
*/
|
563 |
|
|
forward_list&
|
564 |
|
|
operator=(forward_list&& __list)
|
565 |
|
|
{
|
566 |
|
|
// NB: DR 1204.
|
567 |
|
|
// NB: DR 675.
|
568 |
|
|
this->clear();
|
569 |
|
|
this->swap(__list);
|
570 |
|
|
return *this;
|
571 |
|
|
}
|
572 |
|
|
|
573 |
|
|
/**
|
574 |
|
|
* @brief The %forward_list initializer list assignment operator.
|
575 |
|
|
* @param il An initializer_list of value_type.
|
576 |
|
|
*
|
577 |
|
|
* Replace the contents of the %forward_list with copies of the
|
578 |
|
|
* elements in the initializer_list @a il. This is linear in
|
579 |
|
|
* il.size().
|
580 |
|
|
*/
|
581 |
|
|
forward_list&
|
582 |
|
|
operator=(std::initializer_list<_Tp> __il)
|
583 |
|
|
{
|
584 |
|
|
assign(__il);
|
585 |
|
|
return *this;
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
/**
|
589 |
|
|
* @brief Assigns a range to a %forward_list.
|
590 |
|
|
* @param first An input iterator.
|
591 |
|
|
* @param last An input iterator.
|
592 |
|
|
*
|
593 |
|
|
* This function fills a %forward_list with copies of the elements
|
594 |
|
|
* in the range [@a first,@a last).
|
595 |
|
|
*
|
596 |
|
|
* Note that the assignment completely changes the %forward_list and
|
597 |
|
|
* that the resulting %forward_list's size is the same as the number
|
598 |
|
|
* of elements assigned. Old data may be lost.
|
599 |
|
|
*/
|
600 |
|
|
template<typename _InputIterator>
|
601 |
|
|
void
|
602 |
|
|
assign(_InputIterator __first, _InputIterator __last)
|
603 |
|
|
{
|
604 |
|
|
clear();
|
605 |
|
|
insert_after(cbefore_begin(), __first, __last);
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
/**
|
609 |
|
|
* @brief Assigns a given value to a %forward_list.
|
610 |
|
|
* @param n Number of elements to be assigned.
|
611 |
|
|
* @param val Value to be assigned.
|
612 |
|
|
*
|
613 |
|
|
* This function fills a %forward_list with @a n copies of the given
|
614 |
|
|
* value. Note that the assignment completely changes the
|
615 |
|
|
* %forward_list and that the resulting %forward_list's size is the
|
616 |
|
|
* same as the number of elements assigned. Old data may be lost.
|
617 |
|
|
*/
|
618 |
|
|
void
|
619 |
|
|
assign(size_type __n, const _Tp& __val)
|
620 |
|
|
{
|
621 |
|
|
clear();
|
622 |
|
|
insert_after(cbefore_begin(), __n, __val);
|
623 |
|
|
}
|
624 |
|
|
|
625 |
|
|
/**
|
626 |
|
|
* @brief Assigns an initializer_list to a %forward_list.
|
627 |
|
|
* @param il An initializer_list of value_type.
|
628 |
|
|
*
|
629 |
|
|
* Replace the contents of the %forward_list with copies of the
|
630 |
|
|
* elements in the initializer_list @a il. This is linear in
|
631 |
|
|
* il.size().
|
632 |
|
|
*/
|
633 |
|
|
void
|
634 |
|
|
assign(std::initializer_list<_Tp> __il)
|
635 |
|
|
{
|
636 |
|
|
clear();
|
637 |
|
|
insert_after(cbefore_begin(), __il);
|
638 |
|
|
}
|
639 |
|
|
|
640 |
|
|
/// Get a copy of the memory allocation object.
|
641 |
|
|
allocator_type
|
642 |
|
|
get_allocator() const
|
643 |
|
|
{ return this->_M_get_Node_allocator(); }
|
644 |
|
|
|
645 |
|
|
// 23.2.3.2 iterators:
|
646 |
|
|
|
647 |
|
|
/**
|
648 |
|
|
* Returns a read/write iterator that points before the first element
|
649 |
|
|
* in the %forward_list. Iteration is done in ordinary element order.
|
650 |
|
|
*/
|
651 |
|
|
iterator
|
652 |
|
|
before_begin()
|
653 |
|
|
{ return iterator(&this->_M_impl._M_head); }
|
654 |
|
|
|
655 |
|
|
/**
|
656 |
|
|
* Returns a read-only (constant) iterator that points before the
|
657 |
|
|
* first element in the %forward_list. Iteration is done in ordinary
|
658 |
|
|
* element order.
|
659 |
|
|
*/
|
660 |
|
|
const_iterator
|
661 |
|
|
before_begin() const
|
662 |
|
|
{ return const_iterator(&this->_M_impl._M_head); }
|
663 |
|
|
|
664 |
|
|
/**
|
665 |
|
|
* Returns a read/write iterator that points to the first element
|
666 |
|
|
* in the %forward_list. Iteration is done in ordinary element order.
|
667 |
|
|
*/
|
668 |
|
|
iterator
|
669 |
|
|
begin()
|
670 |
|
|
{ return iterator(this->_M_impl._M_head._M_next); }
|
671 |
|
|
|
672 |
|
|
/**
|
673 |
|
|
* Returns a read-only (constant) iterator that points to the first
|
674 |
|
|
* element in the %forward_list. Iteration is done in ordinary
|
675 |
|
|
* element order.
|
676 |
|
|
*/
|
677 |
|
|
const_iterator
|
678 |
|
|
begin() const
|
679 |
|
|
{ return const_iterator(this->_M_impl._M_head._M_next); }
|
680 |
|
|
|
681 |
|
|
/**
|
682 |
|
|
* Returns a read/write iterator that points one past the last
|
683 |
|
|
* element in the %forward_list. Iteration is done in ordinary
|
684 |
|
|
* element order.
|
685 |
|
|
*/
|
686 |
|
|
iterator
|
687 |
|
|
end()
|
688 |
|
|
{ return iterator(0); }
|
689 |
|
|
|
690 |
|
|
/**
|
691 |
|
|
* Returns a read-only iterator that points one past the last
|
692 |
|
|
* element in the %forward_list. Iteration is done in ordinary
|
693 |
|
|
* element order.
|
694 |
|
|
*/
|
695 |
|
|
const_iterator
|
696 |
|
|
end() const
|
697 |
|
|
{ return const_iterator(0); }
|
698 |
|
|
|
699 |
|
|
/**
|
700 |
|
|
* Returns a read-only (constant) iterator that points to the
|
701 |
|
|
* first element in the %forward_list. Iteration is done in ordinary
|
702 |
|
|
* element order.
|
703 |
|
|
*/
|
704 |
|
|
const_iterator
|
705 |
|
|
cbegin() const
|
706 |
|
|
{ return const_iterator(this->_M_impl._M_head._M_next); }
|
707 |
|
|
|
708 |
|
|
/**
|
709 |
|
|
* Returns a read-only (constant) iterator that points before the
|
710 |
|
|
* first element in the %forward_list. Iteration is done in ordinary
|
711 |
|
|
* element order.
|
712 |
|
|
*/
|
713 |
|
|
const_iterator
|
714 |
|
|
cbefore_begin() const
|
715 |
|
|
{ return const_iterator(&this->_M_impl._M_head); }
|
716 |
|
|
|
717 |
|
|
/**
|
718 |
|
|
* Returns a read-only (constant) iterator that points one past
|
719 |
|
|
* the last element in the %forward_list. Iteration is done in
|
720 |
|
|
* ordinary element order.
|
721 |
|
|
*/
|
722 |
|
|
const_iterator
|
723 |
|
|
cend() const
|
724 |
|
|
{ return const_iterator(0); }
|
725 |
|
|
|
726 |
|
|
/**
|
727 |
|
|
* Returns true if the %forward_list is empty. (Thus begin() would
|
728 |
|
|
* equal end().)
|
729 |
|
|
*/
|
730 |
|
|
bool
|
731 |
|
|
empty() const
|
732 |
|
|
{ return this->_M_impl._M_head._M_next == 0; }
|
733 |
|
|
|
734 |
|
|
/**
|
735 |
|
|
* Returns the largest possible size of %forward_list.
|
736 |
|
|
*/
|
737 |
|
|
size_type
|
738 |
|
|
max_size() const
|
739 |
|
|
{ return this->_M_get_Node_allocator().max_size(); }
|
740 |
|
|
|
741 |
|
|
// 23.2.3.3 element access:
|
742 |
|
|
|
743 |
|
|
/**
|
744 |
|
|
* Returns a read/write reference to the data at the first
|
745 |
|
|
* element of the %forward_list.
|
746 |
|
|
*/
|
747 |
|
|
reference
|
748 |
|
|
front()
|
749 |
|
|
{
|
750 |
|
|
_Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
|
751 |
|
|
return __front->_M_value;
|
752 |
|
|
}
|
753 |
|
|
|
754 |
|
|
/**
|
755 |
|
|
* Returns a read-only (constant) reference to the data at the first
|
756 |
|
|
* element of the %forward_list.
|
757 |
|
|
*/
|
758 |
|
|
const_reference
|
759 |
|
|
front() const
|
760 |
|
|
{
|
761 |
|
|
_Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
|
762 |
|
|
return __front->_M_value;
|
763 |
|
|
}
|
764 |
|
|
|
765 |
|
|
// 23.2.3.4 modifiers:
|
766 |
|
|
|
767 |
|
|
/**
|
768 |
|
|
* @brief Constructs object in %forward_list at the front of the
|
769 |
|
|
* list.
|
770 |
|
|
* @param args Arguments.
|
771 |
|
|
*
|
772 |
|
|
* This function will insert an object of type Tp constructed
|
773 |
|
|
* with Tp(std::forward<Args>(args)...) at the front of the list
|
774 |
|
|
* Due to the nature of a %forward_list this operation can
|
775 |
|
|
* be done in constant time, and does not invalidate iterators
|
776 |
|
|
* and references.
|
777 |
|
|
*/
|
778 |
|
|
template<typename... _Args>
|
779 |
|
|
void
|
780 |
|
|
emplace_front(_Args&&... __args)
|
781 |
|
|
{ this->_M_insert_after(cbefore_begin(),
|
782 |
|
|
std::forward<_Args>(__args)...); }
|
783 |
|
|
|
784 |
|
|
/**
|
785 |
|
|
* @brief Add data to the front of the %forward_list.
|
786 |
|
|
* @param val Data to be added.
|
787 |
|
|
*
|
788 |
|
|
* This is a typical stack operation. The function creates an
|
789 |
|
|
* element at the front of the %forward_list and assigns the given
|
790 |
|
|
* data to it. Due to the nature of a %forward_list this operation
|
791 |
|
|
* can be done in constant time, and does not invalidate iterators
|
792 |
|
|
* and references.
|
793 |
|
|
*/
|
794 |
|
|
void
|
795 |
|
|
push_front(const _Tp& __val)
|
796 |
|
|
{ this->_M_insert_after(cbefore_begin(), __val); }
|
797 |
|
|
|
798 |
|
|
/**
|
799 |
|
|
*
|
800 |
|
|
*/
|
801 |
|
|
void
|
802 |
|
|
push_front(_Tp&& __val)
|
803 |
|
|
{ this->_M_insert_after(cbefore_begin(), std::move(__val)); }
|
804 |
|
|
|
805 |
|
|
/**
|
806 |
|
|
* @brief Removes first element.
|
807 |
|
|
*
|
808 |
|
|
* This is a typical stack operation. It shrinks the %forward_list
|
809 |
|
|
* by one. Due to the nature of a %forward_list this operation can
|
810 |
|
|
* be done in constant time, and only invalidates iterators/references
|
811 |
|
|
* to the element being removed.
|
812 |
|
|
*
|
813 |
|
|
* Note that no data is returned, and if the first element's data
|
814 |
|
|
* is needed, it should be retrieved before pop_front() is
|
815 |
|
|
* called.
|
816 |
|
|
*/
|
817 |
|
|
void
|
818 |
|
|
pop_front()
|
819 |
|
|
{ this->_M_erase_after(&this->_M_impl._M_head); }
|
820 |
|
|
|
821 |
|
|
/**
|
822 |
|
|
* @brief Constructs object in %forward_list after the specified
|
823 |
|
|
* iterator.
|
824 |
|
|
* @param pos A const_iterator into the %forward_list.
|
825 |
|
|
* @param args Arguments.
|
826 |
|
|
* @return An iterator that points to the inserted data.
|
827 |
|
|
*
|
828 |
|
|
* This function will insert an object of type T constructed
|
829 |
|
|
* with T(std::forward<Args>(args)...) after the specified
|
830 |
|
|
* location. Due to the nature of a %forward_list this operation can
|
831 |
|
|
* be done in constant time, and does not invalidate iterators
|
832 |
|
|
* and references.
|
833 |
|
|
*/
|
834 |
|
|
template<typename... _Args>
|
835 |
|
|
iterator
|
836 |
|
|
emplace_after(const_iterator __pos, _Args&&... __args)
|
837 |
|
|
{ return iterator(this->_M_insert_after(__pos,
|
838 |
|
|
std::forward<_Args>(__args)...)); }
|
839 |
|
|
|
840 |
|
|
/**
|
841 |
|
|
* @brief Inserts given value into %forward_list after specified
|
842 |
|
|
* iterator.
|
843 |
|
|
* @param pos An iterator into the %forward_list.
|
844 |
|
|
* @param val Data to be inserted.
|
845 |
|
|
* @return An iterator that points to the inserted data.
|
846 |
|
|
*
|
847 |
|
|
* This function will insert a copy of the given value after
|
848 |
|
|
* the specified location. Due to the nature of a %forward_list this
|
849 |
|
|
* operation can be done in constant time, and does not
|
850 |
|
|
* invalidate iterators and references.
|
851 |
|
|
*/
|
852 |
|
|
iterator
|
853 |
|
|
insert_after(const_iterator __pos, const _Tp& __val)
|
854 |
|
|
{ return iterator(this->_M_insert_after(__pos, __val)); }
|
855 |
|
|
|
856 |
|
|
/**
|
857 |
|
|
*
|
858 |
|
|
*/
|
859 |
|
|
iterator
|
860 |
|
|
insert_after(const_iterator __pos, _Tp&& __val)
|
861 |
|
|
{ return iterator(this->_M_insert_after(__pos, std::move(__val))); }
|
862 |
|
|
|
863 |
|
|
/**
|
864 |
|
|
* @brief Inserts a number of copies of given data into the
|
865 |
|
|
* %forward_list.
|
866 |
|
|
* @param pos An iterator into the %forward_list.
|
867 |
|
|
* @param n Number of elements to be inserted.
|
868 |
|
|
* @param val Data to be inserted.
|
869 |
|
|
* @return An iterator pointing to the last inserted copy of
|
870 |
|
|
* @a val or @a pos if @a n == 0.
|
871 |
|
|
*
|
872 |
|
|
* This function will insert a specified number of copies of the
|
873 |
|
|
* given data after the location specified by @a pos.
|
874 |
|
|
*
|
875 |
|
|
* This operation is linear in the number of elements inserted and
|
876 |
|
|
* does not invalidate iterators and references.
|
877 |
|
|
*/
|
878 |
|
|
iterator
|
879 |
|
|
insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
|
880 |
|
|
|
881 |
|
|
/**
|
882 |
|
|
* @brief Inserts a range into the %forward_list.
|
883 |
|
|
* @param position An iterator into the %forward_list.
|
884 |
|
|
* @param first An input iterator.
|
885 |
|
|
* @param last An input iterator.
|
886 |
|
|
* @return An iterator pointing to the last inserted element or
|
887 |
|
|
* @a pos if @a first == @a last.
|
888 |
|
|
*
|
889 |
|
|
* This function will insert copies of the data in the range [@a
|
890 |
|
|
* first,@a last) into the %forward_list after the location specified
|
891 |
|
|
* by @a pos.
|
892 |
|
|
*
|
893 |
|
|
* This operation is linear in the number of elements inserted and
|
894 |
|
|
* does not invalidate iterators and references.
|
895 |
|
|
*/
|
896 |
|
|
template<typename _InputIterator>
|
897 |
|
|
iterator
|
898 |
|
|
insert_after(const_iterator __pos,
|
899 |
|
|
_InputIterator __first, _InputIterator __last);
|
900 |
|
|
|
901 |
|
|
/**
|
902 |
|
|
* @brief Inserts the contents of an initializer_list into
|
903 |
|
|
* %forward_list after the specified iterator.
|
904 |
|
|
* @param pos An iterator into the %forward_list.
|
905 |
|
|
* @param il An initializer_list of value_type.
|
906 |
|
|
* @return An iterator pointing to the last inserted element
|
907 |
|
|
* or @a pos if @a il is empty.
|
908 |
|
|
*
|
909 |
|
|
* This function will insert copies of the data in the
|
910 |
|
|
* initializer_list @a il into the %forward_list before the location
|
911 |
|
|
* specified by @a pos.
|
912 |
|
|
*
|
913 |
|
|
* This operation is linear in the number of elements inserted and
|
914 |
|
|
* does not invalidate iterators and references.
|
915 |
|
|
*/
|
916 |
|
|
iterator
|
917 |
|
|
insert_after(const_iterator __pos, std::initializer_list<_Tp> __il);
|
918 |
|
|
|
919 |
|
|
/**
|
920 |
|
|
* @brief Removes the element pointed to by the iterator following
|
921 |
|
|
* @c pos.
|
922 |
|
|
* @param pos Iterator pointing before element to be erased.
|
923 |
|
|
*
|
924 |
|
|
* This function will erase the element at the given position and
|
925 |
|
|
* thus shorten the %forward_list by one.
|
926 |
|
|
*
|
927 |
|
|
* Due to the nature of a %forward_list this operation can be done
|
928 |
|
|
* in constant time, and only invalidates iterators/references to
|
929 |
|
|
* the element being removed. The user is also cautioned that
|
930 |
|
|
* this function only erases the element, and that if the element
|
931 |
|
|
* is itself a pointer, the pointed-to memory is not touched in
|
932 |
|
|
* any way. Managing the pointer is the user's responsibility.
|
933 |
|
|
*/
|
934 |
|
|
void
|
935 |
|
|
erase_after(const_iterator __pos)
|
936 |
|
|
{ this->_M_erase_after(const_cast<_Node_base*>(__pos._M_node)); }
|
937 |
|
|
|
938 |
|
|
/**
|
939 |
|
|
* @brief Remove a range of elements.
|
940 |
|
|
* @param pos Iterator pointing before the first element to be
|
941 |
|
|
* erased.
|
942 |
|
|
* @param last Iterator pointing to one past the last element to be
|
943 |
|
|
* erased.
|
944 |
|
|
*
|
945 |
|
|
* This function will erase the elements in the range @a
|
946 |
|
|
* (pos,last) and shorten the %forward_list accordingly.
|
947 |
|
|
*
|
948 |
|
|
* This operation is linear time in the size of the range and only
|
949 |
|
|
* invalidates iterators/references to the element being removed.
|
950 |
|
|
* The user is also cautioned that this function only erases the
|
951 |
|
|
* elements, and that if the elements themselves are pointers, the
|
952 |
|
|
* pointed-to memory is not touched in any way. Managing the pointer
|
953 |
|
|
* is the user's responsibility.
|
954 |
|
|
*/
|
955 |
|
|
void
|
956 |
|
|
erase_after(const_iterator __pos, const_iterator __last)
|
957 |
|
|
{ this->_M_erase_after(const_cast<_Node_base*>(__pos._M_node),
|
958 |
|
|
const_cast<_Node_base*>(__last._M_node)); }
|
959 |
|
|
|
960 |
|
|
/**
|
961 |
|
|
* @brief Swaps data with another %forward_list.
|
962 |
|
|
* @param list A %forward_list of the same element and allocator
|
963 |
|
|
* types.
|
964 |
|
|
*
|
965 |
|
|
* This exchanges the elements between two lists in constant
|
966 |
|
|
* time. Note that the global std::swap() function is
|
967 |
|
|
* specialized such that std::swap(l1,l2) will feed to this
|
968 |
|
|
* function.
|
969 |
|
|
*/
|
970 |
|
|
void
|
971 |
|
|
swap(forward_list& __list)
|
972 |
|
|
{ _Node_base::swap(this->_M_impl._M_head, __list._M_impl._M_head); }
|
973 |
|
|
|
974 |
|
|
/**
|
975 |
|
|
* @brief Resizes the %forward_list to the specified number of
|
976 |
|
|
* elements.
|
977 |
|
|
* @param sz Number of elements the %forward_list should contain.
|
978 |
|
|
*
|
979 |
|
|
* This function will %resize the %forward_list to the specified
|
980 |
|
|
* number of elements. If the number is smaller than the
|
981 |
|
|
* %forward_list's current size the %forward_list is truncated,
|
982 |
|
|
* otherwise the %forward_list is extended and the new elements
|
983 |
|
|
* are default constructed.
|
984 |
|
|
*/
|
985 |
|
|
void
|
986 |
|
|
resize(size_type __sz);
|
987 |
|
|
|
988 |
|
|
/**
|
989 |
|
|
* @brief Resizes the %forward_list to the specified number of
|
990 |
|
|
* elements.
|
991 |
|
|
* @param sz Number of elements the %forward_list should contain.
|
992 |
|
|
* @param val Data with which new elements should be populated.
|
993 |
|
|
*
|
994 |
|
|
* This function will %resize the %forward_list to the specified
|
995 |
|
|
* number of elements. If the number is smaller than the
|
996 |
|
|
* %forward_list's current size the %forward_list is truncated,
|
997 |
|
|
* otherwise the %forward_list is extended and new elements are
|
998 |
|
|
* populated with given data.
|
999 |
|
|
*/
|
1000 |
|
|
void
|
1001 |
|
|
resize(size_type __sz, value_type __val);
|
1002 |
|
|
|
1003 |
|
|
/**
|
1004 |
|
|
* @brief Erases all the elements.
|
1005 |
|
|
*
|
1006 |
|
|
* Note that this function only erases
|
1007 |
|
|
* the elements, and that if the elements themselves are
|
1008 |
|
|
* pointers, the pointed-to memory is not touched in any way.
|
1009 |
|
|
* Managing the pointer is the user's responsibility.
|
1010 |
|
|
*/
|
1011 |
|
|
void
|
1012 |
|
|
clear()
|
1013 |
|
|
{ this->_M_erase_after(&this->_M_impl._M_head, 0); }
|
1014 |
|
|
|
1015 |
|
|
// 23.2.3.5 forward_list operations:
|
1016 |
|
|
|
1017 |
|
|
/**
|
1018 |
|
|
* @brief Insert contents of another %forward_list.
|
1019 |
|
|
* @param pos Iterator referencing the element to insert after.
|
1020 |
|
|
* @param list Source list.
|
1021 |
|
|
*
|
1022 |
|
|
* The elements of @a list are inserted in constant time after
|
1023 |
|
|
* the element referenced by @a pos. @a list becomes an empty
|
1024 |
|
|
* list.
|
1025 |
|
|
*
|
1026 |
|
|
* Requires this != @a x.
|
1027 |
|
|
*/
|
1028 |
|
|
void
|
1029 |
|
|
splice_after(const_iterator __pos, forward_list&& __list)
|
1030 |
|
|
{
|
1031 |
|
|
if (!__list.empty())
|
1032 |
|
|
_M_splice_after(__pos, std::move(__list));
|
1033 |
|
|
}
|
1034 |
|
|
|
1035 |
|
|
/**
|
1036 |
|
|
* @brief Insert element from another %forward_list.
|
1037 |
|
|
* @param pos Iterator referencing the element to insert after.
|
1038 |
|
|
* @param list Source list.
|
1039 |
|
|
* @param i Iterator referencing the element before the element
|
1040 |
|
|
* to move.
|
1041 |
|
|
*
|
1042 |
|
|
* Removes the element in list @a list referenced by @a i and
|
1043 |
|
|
* inserts it into the current list after @a pos.
|
1044 |
|
|
*/
|
1045 |
|
|
void
|
1046 |
|
|
splice_after(const_iterator __pos, forward_list&& __list,
|
1047 |
|
|
const_iterator __i)
|
1048 |
|
|
{
|
1049 |
|
|
const_iterator __j = __i;
|
1050 |
|
|
++__j;
|
1051 |
|
|
if (__pos == __i || __pos == __j)
|
1052 |
|
|
return;
|
1053 |
|
|
|
1054 |
|
|
splice_after(__pos, std::move(__list), __i, __j);
|
1055 |
|
|
}
|
1056 |
|
|
|
1057 |
|
|
/**
|
1058 |
|
|
* @brief Insert range from another %forward_list.
|
1059 |
|
|
* @param pos Iterator referencing the element to insert after.
|
1060 |
|
|
* @param list Source list.
|
1061 |
|
|
* @param before Iterator referencing before the start of range
|
1062 |
|
|
* in list.
|
1063 |
|
|
* @param last Iterator referencing the end of range in list.
|
1064 |
|
|
*
|
1065 |
|
|
* Removes elements in the range (before,last) and inserts them
|
1066 |
|
|
* after @a pos in constant time.
|
1067 |
|
|
*
|
1068 |
|
|
* Undefined if @a pos is in (before,last).
|
1069 |
|
|
*/
|
1070 |
|
|
void
|
1071 |
|
|
splice_after(const_iterator __pos, forward_list&& __list,
|
1072 |
|
|
const_iterator __before, const_iterator __last);
|
1073 |
|
|
|
1074 |
|
|
/**
|
1075 |
|
|
* @brief Remove all elements equal to value.
|
1076 |
|
|
* @param val The value to remove.
|
1077 |
|
|
*
|
1078 |
|
|
* Removes every element in the list equal to @a value.
|
1079 |
|
|
* Remaining elements stay in list order. Note that this
|
1080 |
|
|
* function only erases the elements, and that if the elements
|
1081 |
|
|
* themselves are pointers, the pointed-to memory is not
|
1082 |
|
|
* touched in any way. Managing the pointer is the user's
|
1083 |
|
|
* responsibility.
|
1084 |
|
|
*/
|
1085 |
|
|
void
|
1086 |
|
|
remove(const _Tp& __val);
|
1087 |
|
|
|
1088 |
|
|
/**
|
1089 |
|
|
* @brief Remove all elements satisfying a predicate.
|
1090 |
|
|
* @param pred Unary predicate function or object.
|
1091 |
|
|
*
|
1092 |
|
|
* Removes every element in the list for which the predicate
|
1093 |
|
|
* returns true. Remaining elements stay in list order. Note
|
1094 |
|
|
* that this function only erases the elements, and that if the
|
1095 |
|
|
* elements themselves are pointers, the pointed-to memory is
|
1096 |
|
|
* not touched in any way. Managing the pointer is the user's
|
1097 |
|
|
* responsibility.
|
1098 |
|
|
*/
|
1099 |
|
|
template<typename _Pred>
|
1100 |
|
|
void
|
1101 |
|
|
remove_if(_Pred __pred);
|
1102 |
|
|
|
1103 |
|
|
/**
|
1104 |
|
|
* @brief Remove consecutive duplicate elements.
|
1105 |
|
|
*
|
1106 |
|
|
* For each consecutive set of elements with the same value,
|
1107 |
|
|
* remove all but the first one. Remaining elements stay in
|
1108 |
|
|
* list order. Note that this function only erases the
|
1109 |
|
|
* elements, and that if the elements themselves are pointers,
|
1110 |
|
|
* the pointed-to memory is not touched in any way. Managing
|
1111 |
|
|
* the pointer is the user's responsibility.
|
1112 |
|
|
*/
|
1113 |
|
|
void
|
1114 |
|
|
unique()
|
1115 |
|
|
{ this->unique(std::equal_to<_Tp>()); }
|
1116 |
|
|
|
1117 |
|
|
/**
|
1118 |
|
|
* @brief Remove consecutive elements satisfying a predicate.
|
1119 |
|
|
* @param binary_pred Binary predicate function or object.
|
1120 |
|
|
*
|
1121 |
|
|
* For each consecutive set of elements [first,last) that
|
1122 |
|
|
* satisfy predicate(first,i) where i is an iterator in
|
1123 |
|
|
* [first,last), remove all but the first one. Remaining
|
1124 |
|
|
* elements stay in list order. Note that this function only
|
1125 |
|
|
* erases the elements, and that if the elements themselves are
|
1126 |
|
|
* pointers, the pointed-to memory is not touched in any way.
|
1127 |
|
|
* Managing the pointer is the user's responsibility.
|
1128 |
|
|
*/
|
1129 |
|
|
template<typename _BinPred>
|
1130 |
|
|
void
|
1131 |
|
|
unique(_BinPred __binary_pred);
|
1132 |
|
|
|
1133 |
|
|
/**
|
1134 |
|
|
* @brief Merge sorted lists.
|
1135 |
|
|
* @param list Sorted list to merge.
|
1136 |
|
|
*
|
1137 |
|
|
* Assumes that both @a list and this list are sorted according to
|
1138 |
|
|
* operator<(). Merges elements of @a list into this list in
|
1139 |
|
|
* sorted order, leaving @a list empty when complete. Elements in
|
1140 |
|
|
* this list precede elements in @a list that are equal.
|
1141 |
|
|
*/
|
1142 |
|
|
void
|
1143 |
|
|
merge(forward_list&& __list)
|
1144 |
|
|
{ this->merge(std::move(__list), std::less<_Tp>()); }
|
1145 |
|
|
|
1146 |
|
|
/**
|
1147 |
|
|
* @brief Merge sorted lists according to comparison function.
|
1148 |
|
|
* @param list Sorted list to merge.
|
1149 |
|
|
* @param comp Comparison function defining sort order.
|
1150 |
|
|
*
|
1151 |
|
|
* Assumes that both @a list and this list are sorted according to
|
1152 |
|
|
* comp. Merges elements of @a list into this list
|
1153 |
|
|
* in sorted order, leaving @a list empty when complete. Elements
|
1154 |
|
|
* in this list precede elements in @a list that are equivalent
|
1155 |
|
|
* according to comp().
|
1156 |
|
|
*/
|
1157 |
|
|
template<typename _Comp>
|
1158 |
|
|
void
|
1159 |
|
|
merge(forward_list&& __list, _Comp __comp);
|
1160 |
|
|
|
1161 |
|
|
/**
|
1162 |
|
|
* @brief Sort the elements of the list.
|
1163 |
|
|
*
|
1164 |
|
|
* Sorts the elements of this list in NlogN time. Equivalent
|
1165 |
|
|
* elements remain in list order.
|
1166 |
|
|
*/
|
1167 |
|
|
void
|
1168 |
|
|
sort()
|
1169 |
|
|
{ this->sort(std::less<_Tp>()); }
|
1170 |
|
|
|
1171 |
|
|
/**
|
1172 |
|
|
* @brief Sort the forward_list using a comparison function.
|
1173 |
|
|
*
|
1174 |
|
|
* Sorts the elements of this list in NlogN time. Equivalent
|
1175 |
|
|
* elements remain in list order.
|
1176 |
|
|
*/
|
1177 |
|
|
template<typename _Comp>
|
1178 |
|
|
void
|
1179 |
|
|
sort(_Comp __comp);
|
1180 |
|
|
|
1181 |
|
|
/**
|
1182 |
|
|
* @brief Reverse the elements in list.
|
1183 |
|
|
*
|
1184 |
|
|
* Reverse the order of elements in the list in linear time.
|
1185 |
|
|
*/
|
1186 |
|
|
void
|
1187 |
|
|
reverse()
|
1188 |
|
|
{ this->_M_impl._M_head._M_reverse_after(); }
|
1189 |
|
|
|
1190 |
|
|
private:
|
1191 |
|
|
template<typename _Integer>
|
1192 |
|
|
void
|
1193 |
|
|
_M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
|
1194 |
|
|
{ _M_fill_initialize(static_cast<size_type>(__n), __x); }
|
1195 |
|
|
|
1196 |
|
|
// Called by the range constructor to implement [23.1.1]/9
|
1197 |
|
|
template<typename _InputIterator>
|
1198 |
|
|
void
|
1199 |
|
|
_M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
|
1200 |
|
|
__false_type);
|
1201 |
|
|
|
1202 |
|
|
// Called by forward_list(n,v,a), and the range constructor when it
|
1203 |
|
|
// turns out to be the same thing.
|
1204 |
|
|
void
|
1205 |
|
|
_M_fill_initialize(size_type __n, const value_type& __value);
|
1206 |
|
|
|
1207 |
|
|
// Called by splice_after and insert_after.
|
1208 |
|
|
iterator
|
1209 |
|
|
_M_splice_after(const_iterator __pos, forward_list&& __list);
|
1210 |
|
|
};
|
1211 |
|
|
|
1212 |
|
|
/**
|
1213 |
|
|
* @brief Forward list equality comparison.
|
1214 |
|
|
* @param lx A %forward_list
|
1215 |
|
|
* @param ly A %forward_list of the same type as @a lx.
|
1216 |
|
|
* @return True iff the size and elements of the forward lists are equal.
|
1217 |
|
|
*
|
1218 |
|
|
* This is an equivalence relation. It is linear in the size of the
|
1219 |
|
|
* forward lists. Deques are considered equivalent if corresponding
|
1220 |
|
|
* elements compare equal.
|
1221 |
|
|
*/
|
1222 |
|
|
template<typename _Tp, typename _Alloc>
|
1223 |
|
|
bool
|
1224 |
|
|
operator==(const forward_list<_Tp, _Alloc>& __lx,
|
1225 |
|
|
const forward_list<_Tp, _Alloc>& __ly);
|
1226 |
|
|
|
1227 |
|
|
/**
|
1228 |
|
|
* @brief Forward list ordering relation.
|
1229 |
|
|
* @param lx A %forward_list.
|
1230 |
|
|
* @param ly A %forward_list of the same type as @a lx.
|
1231 |
|
|
* @return True iff @a lx is lexicographically less than @a ly.
|
1232 |
|
|
*
|
1233 |
|
|
* This is a total ordering relation. It is linear in the size of the
|
1234 |
|
|
* forward lists. The elements must be comparable with @c <.
|
1235 |
|
|
*
|
1236 |
|
|
* See std::lexicographical_compare() for how the determination is made.
|
1237 |
|
|
*/
|
1238 |
|
|
template<typename _Tp, typename _Alloc>
|
1239 |
|
|
inline bool
|
1240 |
|
|
operator<(const forward_list<_Tp, _Alloc>& __lx,
|
1241 |
|
|
const forward_list<_Tp, _Alloc>& __ly)
|
1242 |
|
|
{ return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
|
1243 |
|
|
__ly.cbegin(), __ly.cend()); }
|
1244 |
|
|
|
1245 |
|
|
/// Based on operator==
|
1246 |
|
|
template<typename _Tp, typename _Alloc>
|
1247 |
|
|
inline bool
|
1248 |
|
|
operator!=(const forward_list<_Tp, _Alloc>& __lx,
|
1249 |
|
|
const forward_list<_Tp, _Alloc>& __ly)
|
1250 |
|
|
{ return !(__lx == __ly); }
|
1251 |
|
|
|
1252 |
|
|
/// Based on operator<
|
1253 |
|
|
template<typename _Tp, typename _Alloc>
|
1254 |
|
|
inline bool
|
1255 |
|
|
operator>(const forward_list<_Tp, _Alloc>& __lx,
|
1256 |
|
|
const forward_list<_Tp, _Alloc>& __ly)
|
1257 |
|
|
{ return (__ly < __lx); }
|
1258 |
|
|
|
1259 |
|
|
/// Based on operator<
|
1260 |
|
|
template<typename _Tp, typename _Alloc>
|
1261 |
|
|
inline bool
|
1262 |
|
|
operator>=(const forward_list<_Tp, _Alloc>& __lx,
|
1263 |
|
|
const forward_list<_Tp, _Alloc>& __ly)
|
1264 |
|
|
{ return !(__lx < __ly); }
|
1265 |
|
|
|
1266 |
|
|
/// Based on operator<
|
1267 |
|
|
template<typename _Tp, typename _Alloc>
|
1268 |
|
|
inline bool
|
1269 |
|
|
operator<=(const forward_list<_Tp, _Alloc>& __lx,
|
1270 |
|
|
const forward_list<_Tp, _Alloc>& __ly)
|
1271 |
|
|
{ return !(__ly < __lx); }
|
1272 |
|
|
|
1273 |
|
|
/// See std::forward_list::swap().
|
1274 |
|
|
template<typename _Tp, typename _Alloc>
|
1275 |
|
|
inline void
|
1276 |
|
|
swap(forward_list<_Tp, _Alloc>& __lx,
|
1277 |
|
|
forward_list<_Tp, _Alloc>& __ly)
|
1278 |
|
|
{ __lx.swap(__ly); }
|
1279 |
|
|
|
1280 |
|
|
_GLIBCXX_END_NAMESPACE // namespace std
|
1281 |
|
|
|
1282 |
|
|
#endif // _FORWARD_LIST_H
|