From Jason Turner

[forwardlist]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpbecd6lrk/{from.md → to.md} +0 -555
tmp/tmpbecd6lrk/{from.md → to.md} RENAMED
@@ -1,555 +0,0 @@
1
- ### Class template `forward_list` <a id="forwardlist">[[forwardlist]]</a>
2
-
3
- #### Overview <a id="forwardlist.overview">[[forwardlist.overview]]</a>
4
-
5
- A `forward_list` is a container that supports forward iterators and
6
- allows constant time insert and erase operations anywhere within the
7
- sequence, with storage management handled automatically. Fast random
8
- access to list elements is not supported.
9
-
10
- [*Note 1*: It is intended that `forward_list` have zero space or time
11
- overhead relative to a hand-written C-style singly linked list. Features
12
- that would conflict with that goal have been omitted. — *end note*]
13
-
14
- A `forward_list` meets all of the requirements of a container (
15
- [[container.req]]), except that the `size()` member function is not
16
- provided and `operator==` has linear complexity. A `forward_list` also
17
- meets all of the requirements for an allocator-aware container (
18
- [[container.alloc.req]]). In addition, a `forward_list` provides the
19
- `assign` member functions ([[container.seq.req]]) and several of the
20
- optional container requirements ([[container.seq.opt]]). Descriptions
21
- are provided here only for operations on `forward_list` that are not
22
- described in that table or for operations where there is additional
23
- semantic information.
24
-
25
- [*Note 2*: Modifying any list requires access to the element preceding
26
- the first element of interest, but in a `forward_list` there is no
27
- constant-time way to access a preceding element. For this reason, ranges
28
- that are modified, such as those supplied to `erase` and `splice`, must
29
- be open at the beginning. — *end note*]
30
-
31
- ``` cpp
32
- namespace std {
33
- template<class T, class Allocator = allocator<T>>
34
- class forward_list {
35
- public:
36
- // types
37
- using value_type = T;
38
- using allocator_type = Allocator;
39
- using pointer = typename allocator_traits<Allocator>::pointer;
40
- using const_pointer = typename allocator_traits<Allocator>::const_pointer;
41
- using reference = value_type&;
42
- using const_reference = const value_type&;
43
- using size_type = implementation-defined; // see [container.requirements]
44
- using difference_type = implementation-defined; // see [container.requirements]
45
- using iterator = implementation-defined // type of forward_list::iterator; // see [container.requirements]
46
- using const_iterator = implementation-defined // type of forward_list::const_iterator; // see [container.requirements]
47
-
48
- // [forwardlist.cons], construct/copy/destroy
49
- forward_list() : forward_list(Allocator()) { }
50
- explicit forward_list(const Allocator&);
51
- explicit forward_list(size_type n, const Allocator& = Allocator());
52
- forward_list(size_type n, const T& value, const Allocator& = Allocator());
53
- template<class InputIterator>
54
- forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
55
- forward_list(const forward_list& x);
56
- forward_list(forward_list&& x);
57
- forward_list(const forward_list& x, const Allocator&);
58
- forward_list(forward_list&& x, const Allocator&);
59
- forward_list(initializer_list<T>, const Allocator& = Allocator());
60
- ~forward_list();
61
- forward_list& operator=(const forward_list& x);
62
- forward_list& operator=(forward_list&& x)
63
- noexcept(allocator_traits<Allocator>::is_always_equal::value);
64
- forward_list& operator=(initializer_list<T>);
65
- template<class InputIterator>
66
- void assign(InputIterator first, InputIterator last);
67
- void assign(size_type n, const T& t);
68
- void assign(initializer_list<T>);
69
- allocator_type get_allocator() const noexcept;
70
-
71
- // [forwardlist.iter], iterators
72
- iterator before_begin() noexcept;
73
- const_iterator before_begin() const noexcept;
74
- iterator begin() noexcept;
75
- const_iterator begin() const noexcept;
76
- iterator end() noexcept;
77
- const_iterator end() const noexcept;
78
-
79
- const_iterator cbegin() const noexcept;
80
- const_iterator cbefore_begin() const noexcept;
81
- const_iterator cend() const noexcept;
82
-
83
- // capacity
84
- [[nodiscard]] bool empty() const noexcept;
85
- size_type max_size() const noexcept;
86
-
87
- // [forwardlist.access], element access
88
- reference front();
89
- const_reference front() const;
90
-
91
- // [forwardlist.modifiers], modifiers
92
- template<class... Args> reference emplace_front(Args&&... args);
93
- void push_front(const T& x);
94
- void push_front(T&& x);
95
- void pop_front();
96
-
97
- template<class... Args> iterator emplace_after(const_iterator position, Args&&... args);
98
- iterator insert_after(const_iterator position, const T& x);
99
- iterator insert_after(const_iterator position, T&& x);
100
-
101
- iterator insert_after(const_iterator position, size_type n, const T& x);
102
- template<class InputIterator>
103
- iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
104
- iterator insert_after(const_iterator position, initializer_list<T> il);
105
-
106
- iterator erase_after(const_iterator position);
107
- iterator erase_after(const_iterator position, const_iterator last);
108
- void swap(forward_list&)
109
- noexcept(allocator_traits<Allocator>::is_always_equal::value);
110
-
111
- void resize(size_type sz);
112
- void resize(size_type sz, const value_type& c);
113
- void clear() noexcept;
114
-
115
- // [forwardlist.ops], forward_list operations
116
- void splice_after(const_iterator position, forward_list& x);
117
- void splice_after(const_iterator position, forward_list&& x);
118
- void splice_after(const_iterator position, forward_list& x, const_iterator i);
119
- void splice_after(const_iterator position, forward_list&& x, const_iterator i);
120
- void splice_after(const_iterator position, forward_list& x,
121
- const_iterator first, const_iterator last);
122
- void splice_after(const_iterator position, forward_list&& x,
123
- const_iterator first, const_iterator last);
124
-
125
- size_type remove(const T& value);
126
- template<class Predicate> size_type remove_if(Predicate pred);
127
-
128
- size_type unique();
129
- template<class BinaryPredicate> size_type unique(BinaryPredicate binary_pred);
130
-
131
- void merge(forward_list& x);
132
- void merge(forward_list&& x);
133
- template<class Compare> void merge(forward_list& x, Compare comp);
134
- template<class Compare> void merge(forward_list&& x, Compare comp);
135
-
136
- void sort();
137
- template<class Compare> void sort(Compare comp);
138
-
139
- void reverse() noexcept;
140
- };
141
-
142
- template<class InputIterator, class Allocator = allocator<iter-value-type<InputIterator>>>
143
- forward_list(InputIterator, InputIterator, Allocator = Allocator())
144
- -> forward_list<iter-value-type<InputIterator>, Allocator>;
145
-
146
- // swap
147
- template<class T, class Allocator>
148
- void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
149
- noexcept(noexcept(x.swap(y)));
150
- }
151
- ```
152
-
153
- An incomplete type `T` may be used when instantiating `forward_list` if
154
- the allocator meets the allocator completeness requirements
155
- [[allocator.requirements.completeness]]. `T` shall be complete before
156
- any member of the resulting specialization of `forward_list` is
157
- referenced.
158
-
159
- #### Constructors, copy, and assignment <a id="forwardlist.cons">[[forwardlist.cons]]</a>
160
-
161
- ``` cpp
162
- explicit forward_list(const Allocator&);
163
- ```
164
-
165
- *Effects:* Constructs an empty `forward_list` object using the specified
166
- allocator.
167
-
168
- *Complexity:* Constant.
169
-
170
- ``` cpp
171
- explicit forward_list(size_type n, const Allocator& = Allocator());
172
- ```
173
-
174
- *Preconditions:* `T` is *Cpp17DefaultInsertable* into `*this`.
175
-
176
- *Effects:* Constructs a `forward_list` object with `n` default-inserted
177
- elements using the specified allocator.
178
-
179
- *Complexity:* Linear in `n`.
180
-
181
- ``` cpp
182
- forward_list(size_type n, const T& value, const Allocator& = Allocator());
183
- ```
184
-
185
- *Preconditions:* `T` is *Cpp17CopyInsertable* into `*this`.
186
-
187
- *Effects:* Constructs a `forward_list` object with `n` copies of `value`
188
- using the specified allocator.
189
-
190
- *Complexity:* Linear in `n`.
191
-
192
- ``` cpp
193
- template<class InputIterator>
194
- forward_list(InputIterator first, InputIterator last, const Allocator& = Allocator());
195
- ```
196
-
197
- *Effects:* Constructs a `forward_list` object equal to the range
198
- \[`first`, `last`).
199
-
200
- *Complexity:* Linear in `distance(first, last)`.
201
-
202
- #### Iterators <a id="forwardlist.iter">[[forwardlist.iter]]</a>
203
-
204
- ``` cpp
205
- iterator before_begin() noexcept;
206
- const_iterator before_begin() const noexcept;
207
- const_iterator cbefore_begin() const noexcept;
208
- ```
209
-
210
- *Returns:* A non-dereferenceable iterator that, when incremented, is
211
- equal to the iterator returned by `begin()`.
212
-
213
- *Effects:* `cbefore_begin()` is equivalent to
214
- `const_cast<forward_list const&>(*this).before_begin()`.
215
-
216
- *Remarks:* `before_begin() == end()` shall equal `false`.
217
-
218
- #### Element access <a id="forwardlist.access">[[forwardlist.access]]</a>
219
-
220
- ``` cpp
221
- reference front();
222
- const_reference front() const;
223
- ```
224
-
225
- *Returns:* `*begin()`
226
-
227
- #### Modifiers <a id="forwardlist.modifiers">[[forwardlist.modifiers]]</a>
228
-
229
- None of the overloads of `insert_after` shall affect the validity of
230
- iterators and references, and `erase_after` shall invalidate only
231
- iterators and references to the erased elements. If an exception is
232
- thrown during `insert_after` there shall be no effect. Inserting `n`
233
- elements into a `forward_list` is linear in `n`, and the number of calls
234
- to the copy or move constructor of `T` is exactly equal to `n`. Erasing
235
- `n` elements from a `forward_list` is linear in `n` and the number of
236
- calls to the destructor of type `T` is exactly equal to `n`.
237
-
238
- ``` cpp
239
- template<class... Args> reference emplace_front(Args&&... args);
240
- ```
241
-
242
- *Effects:* Inserts an object of type `value_type` constructed with
243
- `value_type(std::forward<Args>(args)...)` at the beginning of the list.
244
-
245
- ``` cpp
246
- void push_front(const T& x);
247
- void push_front(T&& x);
248
- ```
249
-
250
- *Effects:* Inserts a copy of `x` at the beginning of the list.
251
-
252
- ``` cpp
253
- void pop_front();
254
- ```
255
-
256
- *Effects:* As if by `erase_after(before_begin())`.
257
-
258
- ``` cpp
259
- iterator insert_after(const_iterator position, const T& x);
260
- iterator insert_after(const_iterator position, T&& x);
261
- ```
262
-
263
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
264
- iterator in the range \[`begin()`, `end()`).
265
-
266
- *Effects:* Inserts a copy of `x` after `position`.
267
-
268
- *Returns:* An iterator pointing to the copy of `x`.
269
-
270
- ``` cpp
271
- iterator insert_after(const_iterator position, size_type n, const T& x);
272
- ```
273
-
274
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
275
- iterator in the range \[`begin()`, `end()`).
276
-
277
- *Effects:* Inserts `n` copies of `x` after `position`.
278
-
279
- *Returns:* An iterator pointing to the last inserted copy of `x` or
280
- `position` if `n == 0`.
281
-
282
- ``` cpp
283
- template<class InputIterator>
284
- iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
285
- ```
286
-
287
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
288
- iterator in the range \[`begin()`, `end()`). Neither `first` nor `last`
289
- are iterators in `*this`.
290
-
291
- *Effects:* Inserts copies of elements in \[`first`, `last`) after
292
- `position`.
293
-
294
- *Returns:* An iterator pointing to the last inserted element or
295
- `position` if `first == last`.
296
-
297
- ``` cpp
298
- iterator insert_after(const_iterator position, initializer_list<T> il);
299
- ```
300
-
301
- *Effects:* `insert_after(p, il.begin(), il.end())`.
302
-
303
- *Returns:* An iterator pointing to the last inserted element or
304
- `position` if `il` is empty.
305
-
306
- ``` cpp
307
- template<class... Args>
308
- iterator emplace_after(const_iterator position, Args&&... args);
309
- ```
310
-
311
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
312
- iterator in the range \[`begin()`, `end()`).
313
-
314
- *Effects:* Inserts an object of type `value_type` constructed with
315
- `value_type(std::forward<Args>(args)...)` after `position`.
316
-
317
- *Returns:* An iterator pointing to the new object.
318
-
319
- ``` cpp
320
- iterator erase_after(const_iterator position);
321
- ```
322
-
323
- *Preconditions:* The iterator following `position` is dereferenceable.
324
-
325
- *Effects:* Erases the element pointed to by the iterator following
326
- `position`.
327
-
328
- *Returns:* An iterator pointing to the element following the one that
329
- was erased, or `end()` if no such element exists.
330
-
331
- *Throws:* Nothing.
332
-
333
- ``` cpp
334
- iterator erase_after(const_iterator position, const_iterator last);
335
- ```
336
-
337
- *Preconditions:* All iterators in the range (`position`, `last`) are
338
- dereferenceable.
339
-
340
- *Effects:* Erases the elements in the range (`position`, `last`).
341
-
342
- *Returns:* `last`.
343
-
344
- *Throws:* Nothing.
345
-
346
- ``` cpp
347
- void resize(size_type sz);
348
- ```
349
-
350
- *Preconditions:* `T` is *Cpp17DefaultInsertable* into `*this`.
351
-
352
- *Effects:* If `sz < distance(begin(), end())`, erases the last
353
- `distance(begin(), end()) - sz` elements from the list. Otherwise,
354
- inserts `sz - distance(begin(), end())` default-inserted elements at the
355
- end of the list.
356
-
357
- ``` cpp
358
- void resize(size_type sz, const value_type& c);
359
- ```
360
-
361
- *Preconditions:* `T` is *Cpp17CopyInsertable* into `*this`.
362
-
363
- *Effects:* If `sz < distance(begin(), end())`, erases the last
364
- `distance(begin(), end()) - sz` elements from the list. Otherwise,
365
- inserts `sz - distance(begin(), end())` copies of `c` at the end of the
366
- list.
367
-
368
- ``` cpp
369
- void clear() noexcept;
370
- ```
371
-
372
- *Effects:* Erases all elements in the range \[`begin()`, `end()`).
373
-
374
- *Remarks:* Does not invalidate past-the-end iterators.
375
-
376
- #### Operations <a id="forwardlist.ops">[[forwardlist.ops]]</a>
377
-
378
- In this subclause, arguments for a template parameter named `Predicate`
379
- or `BinaryPredicate` shall meet the corresponding requirements in
380
- [[algorithms.requirements]]. For `merge` and `sort`, the definitions and
381
- requirements in [[alg.sorting]] apply.
382
-
383
- ``` cpp
384
- void splice_after(const_iterator position, forward_list& x);
385
- void splice_after(const_iterator position, forward_list&& x);
386
- ```
387
-
388
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
389
- iterator in the range \[`begin()`, `end()`).
390
- `get_allocator() == x.get_allocator()` is `true`. `addressof(x) != this`
391
- is `true`.
392
-
393
- *Effects:* Inserts the contents of `x` after `position`, and `x` becomes
394
- empty. Pointers and references to the moved elements of `x` now refer to
395
- those same elements but as members of `*this`. Iterators referring to
396
- the moved elements will continue to refer to their elements, but they
397
- now behave as iterators into `*this`, not into `x`.
398
-
399
- *Throws:* Nothing.
400
-
401
- *Complexity:* 𝑂(`distance(x.begin(), x.end())`)
402
-
403
- ``` cpp
404
- void splice_after(const_iterator position, forward_list& x, const_iterator i);
405
- void splice_after(const_iterator position, forward_list&& x, const_iterator i);
406
- ```
407
-
408
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
409
- iterator in the range \[`begin()`, `end()`). The iterator following `i`
410
- is a dereferenceable iterator in `x`.
411
- `get_allocator() == x.get_allocator()` is `true`.
412
-
413
- *Effects:* Inserts the element following `i` into `*this`, following
414
- `position`, and removes it from `x`. The result is unchanged if
415
- `position == i` or `position == ++i`. Pointers and references to `*++i`
416
- continue to refer to the same element but as a member of `*this`.
417
- Iterators to `*++i` continue to refer to the same element, but now
418
- behave as iterators into `*this`, not into `x`.
419
-
420
- *Throws:* Nothing.
421
-
422
- *Complexity:* 𝑂(1)
423
-
424
- ``` cpp
425
- void splice_after(const_iterator position, forward_list& x,
426
- const_iterator first, const_iterator last);
427
- void splice_after(const_iterator position, forward_list&& x,
428
- const_iterator first, const_iterator last);
429
- ```
430
-
431
- *Preconditions:* `position` is `before_begin()` or is a dereferenceable
432
- iterator in the range \[`begin()`, `end()`). (`first`, `last`) is a
433
- valid range in `x`, and all iterators in the range (`first`, `last`) are
434
- dereferenceable. `position` is not an iterator in the range (`first`,
435
- `last`). `get_allocator() == x.get_allocator()` is `true`.
436
-
437
- *Effects:* Inserts elements in the range (`first`, `last`) after
438
- `position` and removes the elements from `x`. Pointers and references to
439
- the moved elements of `x` now refer to those same elements but as
440
- members of `*this`. Iterators referring to the moved elements will
441
- continue to refer to their elements, but they now behave as iterators
442
- into `*this`, not into `x`.
443
-
444
- *Complexity:* 𝑂(`distance(first, last)`)
445
-
446
- ``` cpp
447
- size_type remove(const T& value);
448
- template<class Predicate> size_type remove_if(Predicate pred);
449
- ```
450
-
451
- *Effects:* Erases all the elements in the list referred to by a list
452
- iterator `i` for which the following conditions hold: `*i == value` (for
453
- `remove()`), `pred(*i)` is `true` (for `remove_if()`). Invalidates only
454
- the iterators and references to the erased elements.
455
-
456
- *Returns:* The number of elements erased.
457
-
458
- *Throws:* Nothing unless an exception is thrown by the equality
459
- comparison or the predicate.
460
-
461
- *Remarks:* Stable [[algorithm.stable]].
462
-
463
- *Complexity:* Exactly `distance(begin(), end())` applications of the
464
- corresponding predicate.
465
-
466
- ``` cpp
467
- size_type unique();
468
- template<class BinaryPredicate> size_type unique(BinaryPredicate pred);
469
- ```
470
-
471
- *Effects:* Erases all but the first element from every consecutive group
472
- of equal elements referred to by the iterator `i` in the range
473
- \[`first + 1`, `last`) for which `*i == *(i-1)` (for the version with no
474
- arguments) or `pred(*i, *(i - 1))` (for the version with a predicate
475
- argument) holds. Invalidates only the iterators and references to the
476
- erased elements.
477
-
478
- *Returns:* The number of elements erased.
479
-
480
- *Throws:* Nothing unless an exception is thrown by the equality
481
- comparison or the predicate.
482
-
483
- *Complexity:* If the range \[`first`, `last`) is not empty, exactly
484
- `(last - first) - 1` applications of the corresponding predicate,
485
- otherwise no applications of the predicate.
486
-
487
- ``` cpp
488
- void merge(forward_list& x);
489
- void merge(forward_list&& x);
490
- template<class Compare> void merge(forward_list& x, Compare comp);
491
- template<class Compare> void merge(forward_list&& x, Compare comp);
492
- ```
493
-
494
- *Preconditions:* `*this` and `x` are both sorted with respect to the
495
- comparator `operator<` (for the first two overloads) or `comp` (for the
496
- last two overloads), and `get_allocator() == x.get_allocator()` is
497
- `true`.
498
-
499
- *Effects:* Merges the two sorted ranges `[begin(), end())` and
500
- `[x.begin(), x.end())`. `x` is empty after the merge. If an exception is
501
- thrown other than by a comparison there are no effects. Pointers and
502
- references to the moved elements of `x` now refer to those same elements
503
- but as members of `*this`. Iterators referring to the moved elements
504
- will continue to refer to their elements, but they now behave as
505
- iterators into `*this`, not into `x`.
506
-
507
- *Remarks:* Stable [[algorithm.stable]].
508
-
509
- *Complexity:* At most
510
- `distance(begin(), end()) + distance(x.begin(), x.end()) - 1`
511
- comparisons.
512
-
513
- ``` cpp
514
- void sort();
515
- template<class Compare> void sort(Compare comp);
516
- ```
517
-
518
- *Effects:* Sorts the list according to the `operator<` or the `comp`
519
- function object. If an exception is thrown, the order of the elements in
520
- `*this` is unspecified. Does not affect the validity of iterators and
521
- references.
522
-
523
- *Remarks:* Stable [[algorithm.stable]].
524
-
525
- *Complexity:* Approximately N log N comparisons, where N is
526
- `distance(begin(), end())`.
527
-
528
- ``` cpp
529
- void reverse() noexcept;
530
- ```
531
-
532
- *Effects:* Reverses the order of the elements in the list. Does not
533
- affect the validity of iterators and references.
534
-
535
- *Complexity:* Linear time.
536
-
537
- #### Erasure <a id="forward.list.erasure">[[forward.list.erasure]]</a>
538
-
539
- ``` cpp
540
- template<class T, class Allocator, class U>
541
- typename forward_list<T, Allocator>::size_type
542
- erase(forward_list<T, Allocator>& c, const U& value);
543
- ```
544
-
545
- *Effects:* Equivalent to:
546
- `return erase_if(c, [&](auto& elem) { return elem == value; });`
547
-
548
- ``` cpp
549
- template<class T, class Allocator, class Predicate>
550
- typename forward_list<T, Allocator>::size_type
551
- erase_if(forward_list<T, Allocator>& c, Predicate pred);
552
- ```
553
-
554
- *Effects:* Equivalent to: `return c.remove_if(pred);`
555
-