]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/forward_list.tcc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / forward_list.tcc
CommitLineData
3a63c9cd
ESR
1// <forward_list.tcc> -*- C++ -*-
2
a945c346 3// Copyright (C) 2008-2024 Free Software Foundation, Inc.
3a63c9cd
ESR
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
3a63c9cd
ESR
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
748086b7
JJ
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/>.
3a63c9cd 24
f910786b
BK
25/** @file bits/forward_list.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{forward_list}
3a63c9cd
ESR
28 */
29
30#ifndef _FORWARD_LIST_TCC
31#define _FORWARD_LIST_TCC 1
32
12ffa228
BK
33namespace std _GLIBCXX_VISIBILITY(default)
34{
4a15d842 35_GLIBCXX_BEGIN_NAMESPACE_VERSION
12ffa228 36_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
3a63c9cd 37
7aad1ae2
JW
38 template<typename _Tp, typename _Alloc>
39 _Fwd_list_base<_Tp, _Alloc>::
21bdef94
JW
40 _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a)
41 : _M_impl(std::move(__a))
7aad1ae2 42 {
21bdef94 43 if (__lst._M_get_Node_allocator() == _M_get_Node_allocator())
75ce74bb 44 this->_M_impl._M_head = std::move(__lst._M_impl._M_head);
7aad1ae2
JW
45 }
46
2a7ee2f9
PC
47 template<typename _Tp, typename _Alloc>
48 template<typename... _Args>
97ffcedf 49 _Fwd_list_node_base*
2a7ee2f9
PC
50 _Fwd_list_base<_Tp, _Alloc>::
51 _M_insert_after(const_iterator __pos, _Args&&... __args)
52 {
75ce74bb 53 _Fwd_list_node_base* __to
97ffcedf
PC
54 = const_cast<_Fwd_list_node_base*>(__pos._M_node);
55 _Node* __thing = _M_create_node(std::forward<_Args>(__args)...);
75ce74bb
FD
56 __thing->_M_next = __to->_M_next;
57 __to->_M_next = __thing;
58 return __to->_M_next;
2a7ee2f9
PC
59 }
60
3a63c9cd 61 template<typename _Tp, typename _Alloc>
efb7b456 62 _Fwd_list_node_base*
3a63c9cd 63 _Fwd_list_base<_Tp, _Alloc>::
97ffcedf 64 _M_erase_after(_Fwd_list_node_base* __pos)
3a63c9cd 65 {
97ffcedf 66 _Node* __curr = static_cast<_Node*>(__pos->_M_next);
33913cfa 67 __pos->_M_next = __curr->_M_next;
1cc56f07
JW
68 _Node_alloc_traits::destroy(_M_get_Node_allocator(),
69 __curr->_M_valptr());
50799846 70 __curr->~_Node();
33913cfa 71 _M_put_node(__curr);
efb7b456 72 return __pos->_M_next;
3a63c9cd
ESR
73 }
74
75 template<typename _Tp, typename _Alloc>
efb7b456 76 _Fwd_list_node_base*
3a63c9cd 77 _Fwd_list_base<_Tp, _Alloc>::
75ce74bb
FD
78 _M_erase_after(_Fwd_list_node_base* __pos,
79 _Fwd_list_node_base* __last)
3a63c9cd 80 {
97ffcedf 81 _Node* __curr = static_cast<_Node*>(__pos->_M_next);
33913cfa 82 while (__curr != __last)
75ce74bb
FD
83 {
84 _Node* __temp = __curr;
85 __curr = static_cast<_Node*>(__curr->_M_next);
1cc56f07
JW
86 _Node_alloc_traits::destroy(_M_get_Node_allocator(),
87 __temp->_M_valptr());
50799846 88 __temp->~_Node();
75ce74bb
FD
89 _M_put_node(__temp);
90 }
33913cfa 91 __pos->_M_next = __last;
efb7b456 92 return __last;
3a63c9cd 93 }
efb7b456 94
b0b4a253 95 // Called by the range constructor to implement [23.3.4.2]/9
3a63c9cd
ESR
96 template<typename _Tp, typename _Alloc>
97 template<typename _InputIterator>
e73d6fe8 98 void
3a63c9cd 99 forward_list<_Tp, _Alloc>::
2203cb90 100 _M_range_initialize(_InputIterator __first, _InputIterator __last)
3a63c9cd 101 {
75ce74bb
FD
102 _Node_base* __to = &this->_M_impl._M_head;
103 for (; __first != __last; ++__first)
104 {
105 __to->_M_next = this->_M_create_node(*__first);
106 __to = __to->_M_next;
107 }
3a63c9cd
ESR
108 }
109
2203cb90 110 // Called by forward_list(n,v,a).
3a63c9cd 111 template<typename _Tp, typename _Alloc>
e73d6fe8 112 void
3a63c9cd 113 forward_list<_Tp, _Alloc>::
e73d6fe8 114 _M_fill_initialize(size_type __n, const value_type& __value)
3a63c9cd 115 {
97ffcedf 116 _Node_base* __to = &this->_M_impl._M_head;
dc2cf706 117 for (; __n; --__n)
75ce74bb
FD
118 {
119 __to->_M_next = this->_M_create_node(__value);
120 __to = __to->_M_next;
121 }
3a63c9cd
ESR
122 }
123
1e3ca17d 124 template<typename _Tp, typename _Alloc>
dc2cf706 125 void
1e3ca17d 126 forward_list<_Tp, _Alloc>::
dc2cf706 127 _M_default_initialize(size_type __n)
1e3ca17d 128 {
97ffcedf 129 _Node_base* __to = &this->_M_impl._M_head;
dc2cf706 130 for (; __n; --__n)
75ce74bb
FD
131 {
132 __to->_M_next = this->_M_create_node();
133 __to = __to->_M_next;
134 }
1e3ca17d
PC
135 }
136
3a63c9cd
ESR
137 template<typename _Tp, typename _Alloc>
138 forward_list<_Tp, _Alloc>&
139 forward_list<_Tp, _Alloc>::
140 operator=(const forward_list& __list)
141 {
9649e5b6 142 if (std::__addressof(__list) != this)
75ce74bb 143 {
7aad1ae2
JW
144 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
145 {
75ce74bb
FD
146 auto& __this_alloc = this->_M_get_Node_allocator();
147 auto& __that_alloc = __list._M_get_Node_allocator();
148 if (!_Node_alloc_traits::_S_always_equal()
149 && __this_alloc != __that_alloc)
150 {
7aad1ae2
JW
151 // replacement allocator cannot free existing storage
152 clear();
153 }
154 std::__alloc_on_copy(__this_alloc, __that_alloc);
75ce74bb 155 }
362261e7 156 assign(__list.cbegin(), __list.cend());
75ce74bb 157 }
3a63c9cd
ESR
158 return *this;
159 }
160
dc2cf706
PC
161 template<typename _Tp, typename _Alloc>
162 void
163 forward_list<_Tp, _Alloc>::
164 _M_default_insert_after(const_iterator __pos, size_type __n)
165 {
166 const_iterator __saved_pos = __pos;
167 __try
168 {
169 for (; __n; --__n)
170 __pos = emplace_after(__pos);
171 }
172 __catch(...)
173 {
174 erase_after(__saved_pos, ++__pos);
175 __throw_exception_again;
176 }
177 }
178
1e3ca17d
PC
179 template<typename _Tp, typename _Alloc>
180 void
181 forward_list<_Tp, _Alloc>::
182 resize(size_type __sz)
183 {
184 iterator __k = before_begin();
185
186 size_type __len = 0;
187 while (__k._M_next() != end() && __len < __sz)
75ce74bb
FD
188 {
189 ++__k;
190 ++__len;
191 }
1e3ca17d 192 if (__len == __sz)
75ce74bb 193 erase_after(__k, end());
1e3ca17d 194 else
dc2cf706 195 _M_default_insert_after(__k, __sz - __len);
1e3ca17d
PC
196 }
197
3a63c9cd
ESR
198 template<typename _Tp, typename _Alloc>
199 void
200 forward_list<_Tp, _Alloc>::
ac42c960 201 resize(size_type __sz, const value_type& __val)
3a63c9cd
ESR
202 {
203 iterator __k = before_begin();
204
205 size_type __len = 0;
206 while (__k._M_next() != end() && __len < __sz)
75ce74bb
FD
207 {
208 ++__k;
209 ++__len;
210 }
3a63c9cd 211 if (__len == __sz)
75ce74bb 212 erase_after(__k, end());
3a63c9cd 213 else
75ce74bb 214 insert_after(__k, __sz - __len, __val);
3a63c9cd
ESR
215 }
216
2a7ee2f9 217 template<typename _Tp, typename _Alloc>
16920896 218 typename forward_list<_Tp, _Alloc>::iterator
2a7ee2f9 219 forward_list<_Tp, _Alloc>::
78263296
PC
220 _M_splice_after(const_iterator __pos,
221 const_iterator __before, const_iterator __last)
2a7ee2f9 222 {
16920896 223 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
78263296
PC
224 _Node_base* __b = const_cast<_Node_base*>(__before._M_node);
225 _Node_base* __end = __b;
226
227 while (__end && __end->_M_next != __last._M_node)
228 __end = __end->_M_next;
229
230 if (__b != __end)
75ce74bb 231 return iterator(__tmp->_M_transfer_after(__b, __end));
78263296
PC
232 else
233 return iterator(__tmp);
2a7ee2f9
PC
234 }
235
236 template<typename _Tp, typename _Alloc>
237 void
238 forward_list<_Tp, _Alloc>::
16920896 239 splice_after(const_iterator __pos, forward_list&&,
956d1814 240 const_iterator __i) noexcept
2a7ee2f9 241 {
78263296
PC
242 const_iterator __j = __i;
243 ++__j;
244
245 if (__pos == __i || __pos == __j)
246 return;
247
97ffcedf 248 _Node_base* __tmp = const_cast<_Node_base*>(__pos._M_node);
78263296
PC
249 __tmp->_M_transfer_after(const_cast<_Node_base*>(__i._M_node),
250 const_cast<_Node_base*>(__j._M_node));
2a7ee2f9
PC
251 }
252
16920896
PC
253 template<typename _Tp, typename _Alloc>
254 typename forward_list<_Tp, _Alloc>::iterator
255 forward_list<_Tp, _Alloc>::
256 insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
257 {
258 if (__n)
259 {
ff15f019 260 forward_list __tmp(__n, __val, get_allocator());
78263296 261 return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
16920896
PC
262 }
263 else
264 return iterator(const_cast<_Node_base*>(__pos._M_node));
265 }
266
267 template<typename _Tp, typename _Alloc>
2203cb90 268 template<typename _InputIterator, typename>
16920896
PC
269 typename forward_list<_Tp, _Alloc>::iterator
270 forward_list<_Tp, _Alloc>::
271 insert_after(const_iterator __pos,
272 _InputIterator __first, _InputIterator __last)
273 {
ff15f019 274 forward_list __tmp(__first, __last, get_allocator());
16920896 275 if (!__tmp.empty())
78263296 276 return _M_splice_after(__pos, __tmp.before_begin(), __tmp.end());
16920896
PC
277 else
278 return iterator(const_cast<_Node_base*>(__pos._M_node));
279 }
280
ef45724a
JW
281#if __cplusplus > 201703L
282# define _GLIBCXX20_ONLY(__expr) __expr
283#else
284# define _GLIBCXX20_ONLY(__expr)
285#endif
286
3a63c9cd 287 template<typename _Tp, typename _Alloc>
ef45724a 288 auto
3a63c9cd 289 forward_list<_Tp, _Alloc>::
ef45724a 290 remove(const _Tp& __val) -> __remove_return_type
3a63c9cd 291 {
ef45724a 292 size_type __removed __attribute__((__unused__)) = 0;
8b7af071 293 forward_list __to_destroy(get_allocator());
afb767b4 294
8b7af071
FD
295 auto __prev_it = cbefore_begin();
296 while (_Node* __tmp = static_cast<_Node*>(__prev_it._M_node->_M_next))
297 if (*__tmp->_M_valptr() == __val)
298 {
299 __to_destroy.splice_after(__to_destroy.cbefore_begin(),
300 *this, __prev_it);
301 _GLIBCXX20_ONLY( __removed++ );
302 }
303 else
304 ++__prev_it;
afb767b4 305
ef45724a 306 return _GLIBCXX20_ONLY( __removed );
3a63c9cd
ESR
307 }
308
309 template<typename _Tp, typename _Alloc>
310 template<typename _Pred>
ef45724a 311 auto
3a63c9cd 312 forward_list<_Tp, _Alloc>::
ef45724a 313 remove_if(_Pred __pred) -> __remove_return_type
3a63c9cd 314 {
ef45724a 315 size_type __removed __attribute__((__unused__)) = 0;
8b7af071
FD
316 forward_list __to_destroy(get_allocator());
317
318 auto __prev_it = cbefore_begin();
319 while (_Node* __tmp = static_cast<_Node*>(__prev_it._M_node->_M_next))
320 if (__pred(*__tmp->_M_valptr()))
321 {
322 __to_destroy.splice_after(__to_destroy.cbefore_begin(),
323 *this, __prev_it);
324 _GLIBCXX20_ONLY( __removed++ );
325 }
326 else
327 ++__prev_it;
328
ef45724a 329 return _GLIBCXX20_ONLY( __removed );
3a63c9cd
ESR
330 }
331
3a63c9cd
ESR
332 template<typename _Tp, typename _Alloc>
333 template<typename _BinPred>
ef45724a 334 auto
3a63c9cd 335 forward_list<_Tp, _Alloc>::
ef45724a 336 unique(_BinPred __binary_pred) -> __remove_return_type
3a63c9cd 337 {
75ce74bb
FD
338 iterator __first = begin();
339 iterator __last = end();
340 if (__first == __last)
ef45724a 341 return _GLIBCXX20_ONLY(0);
8b7af071
FD
342
343 forward_list __to_destroy(get_allocator());
ef45724a 344 size_type __removed __attribute__((__unused__)) = 0;
75ce74bb
FD
345 iterator __next = __first;
346 while (++__next != __last)
347 {
348 if (__binary_pred(*__first, *__next))
ef45724a 349 {
8b7af071
FD
350 __to_destroy.splice_after(__to_destroy.cbefore_begin(),
351 *this, __first);
ef45724a
JW
352 _GLIBCXX20_ONLY( __removed++ );
353 }
75ce74bb
FD
354 else
355 __first = __next;
356 __next = __first;
357 }
8b7af071
FD
358
359 return _GLIBCXX20_ONLY( __removed );
3a63c9cd
ESR
360 }
361
ef45724a
JW
362#undef _GLIBCXX20_ONLY
363
3a63c9cd
ESR
364 template<typename _Tp, typename _Alloc>
365 template<typename _Comp>
366 void
367 forward_list<_Tp, _Alloc>::
368 merge(forward_list&& __list, _Comp __comp)
369 {
52ebc2be
PK
370 // _GLIBCXX_RESOLVE_LIB_DEFECTS
371 // 3088. forward_list::merge behavior unclear when passed *this
372 if (std::__addressof(__list) == this)
373 return;
374
75ce74bb
FD
375 _Node_base* __node = &this->_M_impl._M_head;
376 while (__node->_M_next && __list._M_impl._M_head._M_next)
377 {
378 if (__comp(*static_cast<_Node*>
379 (__list._M_impl._M_head._M_next)->_M_valptr(),
380 *static_cast<_Node*>
381 (__node->_M_next)->_M_valptr()))
382 __node->_M_transfer_after(&__list._M_impl._M_head,
383 __list._M_impl._M_head._M_next);
384 __node = __node->_M_next;
385 }
386
387 if (__list._M_impl._M_head._M_next)
388 *__node = std::move(__list._M_impl._M_head);
3a63c9cd
ESR
389 }
390
2a7ee2f9
PC
391 template<typename _Tp, typename _Alloc>
392 bool
393 operator==(const forward_list<_Tp, _Alloc>& __lx,
75ce74bb 394 const forward_list<_Tp, _Alloc>& __ly)
2a7ee2f9
PC
395 {
396 // We don't have size() so we need to walk through both lists
397 // making sure both iterators are valid.
919e5c5e
PC
398 auto __ix = __lx.cbegin();
399 auto __iy = __ly.cbegin();
2a7ee2f9 400 while (__ix != __lx.cend() && __iy != __ly.cend())
75ce74bb 401 {
8c710065 402 if (!(*__ix == *__iy))
75ce74bb
FD
403 return false;
404 ++__ix;
405 ++__iy;
406 }
2a7ee2f9 407 if (__ix == __lx.cend() && __iy == __ly.cend())
75ce74bb 408 return true;
2a7ee2f9 409 else
75ce74bb 410 return false;
2a7ee2f9
PC
411 }
412
fc52f99d
PC
413 template<typename _Tp, class _Alloc>
414 template<typename _Comp>
415 void
416 forward_list<_Tp, _Alloc>::
417 sort(_Comp __comp)
418 {
75ce74bb
FD
419 // If `next' is nullptr, return immediately.
420 _Node* __list = static_cast<_Node*>(this->_M_impl._M_head._M_next);
421 if (!__list)
422 return;
423
424 unsigned long __insize = 1;
425
426 while (1)
427 {
428 _Node* __p = __list;
429 __list = nullptr;
430 _Node* __tail = nullptr;
431
432 // Count number of merges we do in this pass.
433 unsigned long __nmerges = 0;
434
435 while (__p)
436 {
437 ++__nmerges;
438 // There exists a merge to be done.
439 // Step `insize' places along from p.
440 _Node* __q = __p;
441 unsigned long __psize = 0;
442 for (unsigned long __i = 0; __i < __insize; ++__i)
443 {
444 ++__psize;
445 __q = static_cast<_Node*>(__q->_M_next);
446 if (!__q)
447 break;
448 }
449
450 // If q hasn't fallen off end, we have two lists to merge.
451 unsigned long __qsize = __insize;
452
453 // Now we have two lists; merge them.
454 while (__psize > 0 || (__qsize > 0 && __q))
455 {
456 // Decide whether next node of merge comes from p or q.
457 _Node* __e;
458 if (__psize == 0)
459 {
460 // p is empty; e must come from q.
461 __e = __q;
462 __q = static_cast<_Node*>(__q->_M_next);
463 --__qsize;
464 }
465 else if (__qsize == 0 || !__q)
466 {
467 // q is empty; e must come from p.
468 __e = __p;
469 __p = static_cast<_Node*>(__p->_M_next);
470 --__psize;
471 }
8c710065 472 else if (!__comp(*__q->_M_valptr(), *__p->_M_valptr()))
75ce74bb 473 {
8c710065 474 // First node of q is not lower; e must come from p.
75ce74bb
FD
475 __e = __p;
476 __p = static_cast<_Node*>(__p->_M_next);
477 --__psize;
478 }
479 else
480 {
481 // First node of q is lower; e must come from q.
482 __e = __q;
483 __q = static_cast<_Node*>(__q->_M_next);
484 --__qsize;
485 }
486
487 // Add the next node to the merged list.
488 if (__tail)
489 __tail->_M_next = __e;
490 else
491 __list = __e;
492 __tail = __e;
493 }
494
495 // Now p has stepped `insize' places along, and q has too.
496 __p = __q;
497 }
498 __tail->_M_next = nullptr;
499
500 // If we have done only one merge, we're finished.
501 // Allow for nmerges == 0, the empty list case.
502 if (__nmerges <= 1)
503 {
504 this->_M_impl._M_head._M_next = __list;
505 return;
506 }
507
508 // Otherwise repeat, merging lists twice the size.
509 __insize *= 2;
510 }
fc52f99d 511 }
75ce74bb 512
12ffa228 513_GLIBCXX_END_NAMESPACE_CONTAINER
4a15d842 514_GLIBCXX_END_NAMESPACE_VERSION
12ffa228 515} // namespace std
3a63c9cd
ESR
516
517#endif /* _FORWARD_LIST_TCC */