From Jason Turner

[numeric.ops]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpf4qs3l8j/{from.md → to.md} +294 -445
tmp/tmpf4qs3l8j/{from.md → to.md} RENAMED
@@ -1,276 +1,50 @@
1
  ## Generalized numeric operations <a id="numeric.ops">[[numeric.ops]]</a>
2
 
3
- ### Header `<numeric>` synopsis <a id="numeric.ops.overview">[[numeric.ops.overview]]</a>
4
-
5
- ``` cpp
6
- namespace std {
7
- // [accumulate], accumulate
8
- template <class InputIterator, class T>
9
- T accumulate(InputIterator first, InputIterator last, T init);
10
- template <class InputIterator, class T, class BinaryOperation>
11
- T accumulate(InputIterator first, InputIterator last, T init,
12
- BinaryOperation binary_op);
13
-
14
- // [reduce], reduce
15
- template<class InputIterator>
16
- typename iterator_traits<InputIterator>::value_type
17
- reduce(InputIterator first, InputIterator last);
18
- template<class InputIterator, class T>
19
- T reduce(InputIterator first, InputIterator last, T init);
20
- template<class InputIterator, class T, class BinaryOperation>
21
- T reduce(InputIterator first, InputIterator last, T init,
22
- BinaryOperation binary_op);
23
- template<class ExecutionPolicy, class ForwardIterator>
24
- typename iterator_traits<ForwardIterator>::value_type
25
- reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
26
- ForwardIterator first, ForwardIterator last);
27
- template<class ExecutionPolicy, class ForwardIterator, class T>
28
- T reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
29
- ForwardIterator first, ForwardIterator last, T init);
30
- template<class ExecutionPolicy, class ForwardIterator, class T, class BinaryOperation>
31
- T reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
32
- ForwardIterator first, ForwardIterator last, T init,
33
- BinaryOperation binary_op);
34
-
35
- // [inner.product], inner product
36
- template <class InputIterator1, class InputIterator2, class T>
37
- T inner_product(InputIterator1 first1, InputIterator1 last1,
38
- InputIterator2 first2, T init);
39
- template <class InputIterator1, class InputIterator2, class T,
40
- class BinaryOperation1, class BinaryOperation2>
41
- T inner_product(InputIterator1 first1, InputIterator1 last1,
42
- InputIterator2 first2, T init,
43
- BinaryOperation1 binary_op1,
44
- BinaryOperation2 binary_op2);
45
-
46
- // [transform.reduce], transform reduce
47
- template<class InputIterator1, class InputIterator2, class T>
48
- T transform_reduce(InputIterator1 first1, InputIterator1 last1,
49
- InputIterator2 first2,
50
- T init);
51
- template<class InputIterator1, class InputIterator2, class T,
52
- class BinaryOperation1, class BinaryOperation2>
53
- T transform_reduce(InputIterator1 first1, InputIterator1 last1,
54
- InputIterator2 first2,
55
- T init,
56
- BinaryOperation1 binary_op1,
57
- BinaryOperation2 binary_op2);
58
- template<class InputIterator, class T,
59
- class BinaryOperation, class UnaryOperation>
60
- T transform_reduce(InputIterator first, InputIterator last,
61
- T init,
62
- BinaryOperation binary_op, UnaryOperation unary_op);
63
- template<class ExecutionPolicy,
64
- class ForwardIterator1, class ForwardIterator2, class T>
65
- T transform_reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
66
- ForwardIterator1 first1, ForwardIterator1 last1,
67
- ForwardIterator2 first2,
68
- T init);
69
- template<class ExecutionPolicy,
70
- class ForwardIterator1, class ForwardIterator2, class T,
71
- class BinaryOperation1, class BinaryOperation2>
72
- T transform_reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
73
- ForwardIterator1 first1, ForwardIterator1 last1,
74
- ForwardIterator2 first2,
75
- T init,
76
- BinaryOperation1 binary_op1,
77
- BinaryOperation2 binary_op2);
78
- template<class ExecutionPolicy,
79
- class ForwardIterator, class T,
80
- class BinaryOperation, class UnaryOperation>
81
- T transform_reduce(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
82
- ForwardIterator first, ForwardIterator last,
83
- T init,
84
- BinaryOperation binary_op, UnaryOperation unary_op);
85
-
86
- // [partial.sum], partial sum
87
- template <class InputIterator, class OutputIterator>
88
- OutputIterator partial_sum(InputIterator first,
89
- InputIterator last,
90
- OutputIterator result);
91
- template <class InputIterator, class OutputIterator, class BinaryOperation>
92
- OutputIterator partial_sum(InputIterator first,
93
- InputIterator last,
94
- OutputIterator result,
95
- BinaryOperation binary_op);
96
-
97
- // [exclusive.scan], exclusive scan
98
- template<class InputIterator, class OutputIterator, class T>
99
- OutputIterator exclusive_scan(InputIterator first, InputIterator last,
100
- OutputIterator result,
101
- T init);
102
- template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
103
- OutputIterator exclusive_scan(InputIterator first, InputIterator last,
104
- OutputIterator result,
105
- T init, BinaryOperation binary_op);
106
- template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T>
107
- ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
108
- ForwardIterator1 first, ForwardIterator1 last,
109
- ForwardIterator2 result,
110
- T init);
111
- template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T,
112
- class BinaryOperation>
113
- ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
114
- ForwardIterator1 first, ForwardIterator1 last,
115
- ForwardIterator2 result,
116
- T init, BinaryOperation binary_op);
117
-
118
- // [inclusive.scan], inclusive scan
119
- template<class InputIterator, class OutputIterator>
120
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
121
- OutputIterator result);
122
- template<class InputIterator, class OutputIterator, class BinaryOperation>
123
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
124
- OutputIterator result,
125
- BinaryOperation binary_op);
126
- template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
127
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
128
- OutputIterator result,
129
- BinaryOperation binary_op, T init);
130
- template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
131
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
132
- ForwardIterator1 first, ForwardIterator1 last,
133
- ForwardIterator2 result);
134
- template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
135
- class BinaryOperation>
136
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
137
- ForwardIterator1 first, ForwardIterator1 last,
138
- ForwardIterator2 result,
139
- BinaryOperation binary_op);
140
- template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
141
- class BinaryOperation, class T>
142
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
143
- ForwardIterator1 first, ForwardIterator1 last,
144
- ForwardIterator2 result,
145
- BinaryOperation binary_op, T init);
146
-
147
- // [transform.exclusive.scan], transform exclusive scan
148
- template<class InputIterator, class OutputIterator, class T,
149
- class BinaryOperation, class UnaryOperation>
150
- OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
151
- OutputIterator result,
152
- T init,
153
- BinaryOperation binary_op,
154
- UnaryOperation unary_op);
155
- template<class ExecutionPolicy,
156
- class ForwardIterator1, class ForwardIterator2, class T,
157
- class BinaryOperation, class UnaryOperation>
158
- ForwardIterator2 transform_exclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
159
- ForwardIterator1 first, ForwardIterator1 last,
160
- ForwardIterator2 result,
161
- T init,
162
- BinaryOperation binary_op,
163
- UnaryOperation unary_op);
164
-
165
- // [transform.inclusive.scan], transform inclusive scan
166
- template<class InputIterator, class OutputIterator,
167
- class BinaryOperation, class UnaryOperation>
168
- OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
169
- OutputIterator result,
170
- BinaryOperation binary_op,
171
- UnaryOperation unary_op);
172
- template<class InputIterator, class OutputIterator,
173
- class BinaryOperation, class UnaryOperation, class T>
174
- OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
175
- OutputIterator result,
176
- BinaryOperation binary_op,
177
- UnaryOperation unary_op,
178
- T init);
179
- template<class ExecutionPolicy,
180
- class ForwardIterator1, class ForwardIterator2,
181
- class BinaryOperation, class UnaryOperation>
182
- ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
183
- ForwardIterator1 first, ForwardIterator1 last,
184
- ForwardIterator2 result,
185
- BinaryOperation binary_op,
186
- UnaryOperation unary_op);
187
- template<class ExecutionPolicy,
188
- class ForwardIterator1, class ForwardIterator2,
189
- class BinaryOperation, class UnaryOperation, class T>
190
- ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
191
- ForwardIterator1 first, ForwardIterator1 last,
192
- ForwardIterator2 result,
193
- BinaryOperation binary_op,
194
- UnaryOperation unary_op,
195
- T init);
196
-
197
- // [adjacent.difference], adjacent difference
198
- template <class InputIterator, class OutputIterator>
199
- OutputIterator adjacent_difference(InputIterator first,
200
- InputIterator last,
201
- OutputIterator result);
202
- template <class InputIterator, class OutputIterator, class BinaryOperation>
203
- OutputIterator adjacent_difference(InputIterator first,
204
- InputIterator last,
205
- OutputIterator result,
206
- BinaryOperation binary_op);
207
- template <class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
208
- ForwardIterator2 adjacent_difference(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
209
- ForwardIterator1 first,
210
- ForwardIterator1 last,
211
- ForwardIterator2 result);
212
- template <class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
213
- class BinaryOperation>
214
- ForwardIterator2 adjacent_difference(ExecutionPolicy&& exec, // see [algorithms.parallel.overloads]
215
- ForwardIterator1 first,
216
- ForwardIterator1 last,
217
- ForwardIterator2 result,
218
- BinaryOperation binary_op);
219
-
220
- // [numeric.iota], iota
221
- template <class ForwardIterator, class T>
222
- void iota(ForwardIterator first, ForwardIterator last, T value);
223
-
224
- // [numeric.ops.gcd], greatest common divisor
225
- template <class M, class N>
226
- constexpr common_type_t<M,N> gcd(M m, N n);
227
-
228
- // [numeric.ops.lcm], least common multiple
229
- template <class M, class N>
230
- constexpr common_type_t<M,N> lcm(M m, N n);
231
- }
232
- ```
233
-
234
- The requirements on the types of algorithms’ arguments that are
235
- described in the introduction to Clause  [[algorithms]] also apply to
236
- the following algorithms.
237
-
238
- Throughout this subclause, the parameters `UnaryOperation`,
239
- `BinaryOperation`, `BinaryOperation1`, and `BinaryOperation2` are used
240
- whenever an algorithm expects a function object ([[function.objects]]).
241
-
242
  [*Note 1*: The use of closed ranges as well as semi-open ranges to
243
  specify requirements throughout this subclause is
244
  intentional. — *end note*]
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  ### Accumulate <a id="accumulate">[[accumulate]]</a>
247
 
248
  ``` cpp
249
  template<class InputIterator, class T>
250
- T accumulate(InputIterator first, InputIterator last, T init);
251
  template<class InputIterator, class T, class BinaryOperation>
252
- T accumulate(InputIterator first, InputIterator last, T init,
253
  BinaryOperation binary_op);
254
  ```
255
 
256
- *Requires:* `T` shall meet the requirements of `CopyConstructible`
257
- (Table  [[tab:copyconstructible]]) and `CopyAssignable`
258
- (Table  [[tab:copyassignable]]) types. In the range \[`first`, `last`\],
259
- `binary_op` shall neither modify elements nor invalidate iterators or
260
- subranges.[^13]
261
 
262
  *Effects:* Computes its result by initializing the accumulator `acc`
263
- with the initial value `init` and then modifies it with `acc = acc + *i`
264
- or `acc = binary_op(acc, *i)` for every iterator `i` in the range
265
- \[`first`, `last`) in order.[^14]
266
 
267
  ### Reduce <a id="reduce">[[reduce]]</a>
268
 
269
  ``` cpp
270
  template<class InputIterator>
271
- typename iterator_traits<InputIterator>::value_type
272
  reduce(InputIterator first, InputIterator last);
273
  ```
274
 
275
  *Effects:* Equivalent to:
276
 
@@ -293,11 +67,11 @@ return reduce(std::forward<ExecutionPolicy>(exec), first, last,
293
  typename iterator_traits<ForwardIterator>::value_type{});
294
  ```
295
 
296
  ``` cpp
297
  template<class InputIterator, class T>
298
- T reduce(InputIterator first, InputIterator last, T init);
299
  ```
300
 
301
  *Effects:* Equivalent to:
302
 
303
  ``` cpp
@@ -316,26 +90,33 @@ template<class ExecutionPolicy, class ForwardIterator, class T>
316
  return reduce(std::forward<ExecutionPolicy>(exec), first, last, init, plus<>());
317
  ```
318
 
319
  ``` cpp
320
  template<class InputIterator, class T, class BinaryOperation>
321
- T reduce(InputIterator first, InputIterator last, T init,
322
  BinaryOperation binary_op);
323
  template<class ExecutionPolicy, class ForwardIterator, class T, class BinaryOperation>
324
  T reduce(ExecutionPolicy&& exec,
325
  ForwardIterator first, ForwardIterator last, T init,
326
  BinaryOperation binary_op);
327
  ```
328
 
329
- *Requires:*
330
-
331
- - `T` shall be `MoveConstructible` (Table  [[tab:moveconstructible]]).
332
- - All of `binary_op(init, *first)`, `binary_op(*first, init)`,
333
- `binary_op(init, init)`, and `binary_op(*first, *first)` shall be
334
- convertible to `T`.
335
- - `binary_op` shall neither invalidate iterators or subranges, nor
336
- modify elements in the range \[`first`, `last`\].
 
 
 
 
 
 
 
337
 
338
  *Returns:* *GENERALIZED_SUM*(binary_op, init, \*i, ...) for every `i` in
339
  \[`first`, `last`).
340
 
341
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
@@ -347,59 +128,69 @@ nondeterministic result for non-associative or non-commutative
347
 
348
  ### Inner product <a id="inner.product">[[inner.product]]</a>
349
 
350
  ``` cpp
351
  template<class InputIterator1, class InputIterator2, class T>
352
- T inner_product(InputIterator1 first1, InputIterator1 last1,
353
  InputIterator2 first2, T init);
354
  template<class InputIterator1, class InputIterator2, class T,
355
  class BinaryOperation1, class BinaryOperation2>
356
- T inner_product(InputIterator1 first1, InputIterator1 last1,
357
  InputIterator2 first2, T init,
358
  BinaryOperation1 binary_op1,
359
  BinaryOperation2 binary_op2);
360
  ```
361
 
362
- *Requires:* `T` shall meet the requirements of `CopyConstructible`
363
- (Table  [[tab:copyconstructible]]) and `CopyAssignable`
364
- (Table  [[tab:copyassignable]]) types. In the ranges \[`first1`,
365
  `last1`\] and \[`first2`, `first2 + (last1 - first1)`\] `binary_op1` and
366
- `binary_op2` shall neither modify elements nor invalidate iterators or
367
- subranges.[^15]
368
 
369
  *Effects:* Computes its result by initializing the accumulator `acc`
370
  with the initial value `init` and then modifying it with
371
- `acc = acc + (*i1) * (*i2)` or
372
- `acc = binary_op1(acc, binary_op2(*i1, *i2))` for every iterator `i1` in
373
- the range \[`first1`, `last1`) and iterator `i2` in the range
374
- \[`first2`, `first2 + (last1 - first1)`) in order.
375
 
376
  ### Transform reduce <a id="transform.reduce">[[transform.reduce]]</a>
377
 
378
  ``` cpp
379
  template<class InputIterator1, class InputIterator2, class T>
380
- T transform_reduce(InputIterator1 first1, InputIterator1 last1,
381
  InputIterator2 first2,
382
  T init);
383
- template <class ExecutionPolicy,
384
- class ForwardIterator1, class ForwardIterator2, class T>
385
- T transform_reduce(ExecutionPolicy&& exec,
386
- ForwardIterator1 first1, ForwardIterator1 last1,
387
- ForwardIterator2 first2,
388
- T init);
389
  ```
390
 
391
  *Effects:* Equivalent to:
392
 
393
  ``` cpp
394
  return transform_reduce(first1, last1, first2, init, plus<>(), multiplies<>());
395
  ```
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
  ``` cpp
398
  template<class InputIterator1, class InputIterator2, class T,
399
  class BinaryOperation1, class BinaryOperation2>
400
- T transform_reduce(InputIterator1 first1, InputIterator1 last1,
401
  InputIterator2 first2,
402
  T init,
403
  BinaryOperation1 binary_op1,
404
  BinaryOperation2 binary_op2);
405
  template<class ExecutionPolicy,
@@ -411,22 +202,25 @@ template <class ExecutionPolicy,
411
  T init,
412
  BinaryOperation1 binary_op1,
413
  BinaryOperation2 binary_op2);
414
  ```
415
 
416
- *Requires:*
417
 
418
- - `T` shall be `MoveConstructible` (Table  [[tab:moveconstructible]]).
419
- - All of
420
  - `binary_op1(init, init)`,
421
  - `binary_op1(init, binary_op2(*first1, *first2))`,
422
  - `binary_op1(binary_op2(*first1, *first2), init)`, and
423
  - `binary_op1(binary_op2(*first1, *first2), binary_op2(*first1, *first2))`
424
 
425
- shall be convertible to `T`.
426
- - Neither `binary_op1` nor `binary_op2` shall invalidate subranges, or
427
- modify elements in the ranges \[`first1`, `last1`\] and \[`first2`,
 
 
 
 
 
428
  `first2 + (last1 - first1)`\].
429
 
430
  *Returns:*
431
 
432
  ``` cpp
@@ -439,32 +233,35 @@ for every iterator `i` in \[`first1`, `last1`).
439
  `binary_op2`.
440
 
441
  ``` cpp
442
  template<class InputIterator, class T,
443
  class BinaryOperation, class UnaryOperation>
444
- T transform_reduce(InputIterator first, InputIterator last, T init,
445
  BinaryOperation binary_op, UnaryOperation unary_op);
446
  template<class ExecutionPolicy,
447
  class ForwardIterator, class T,
448
  class BinaryOperation, class UnaryOperation>
449
  T transform_reduce(ExecutionPolicy&& exec,
450
  ForwardIterator first, ForwardIterator last,
451
  T init, BinaryOperation binary_op, UnaryOperation unary_op);
452
  ```
453
 
454
- *Requires:*
455
 
456
- - `T` shall be `MoveConstructible` (Table  [[tab:moveconstructible]]).
457
- - All of
458
  - `binary_op(init, init)`,
459
  - `binary_op(init, unary_op(*first))`,
460
  - `binary_op(unary_op(*first), init)`, and
461
  - `binary_op(unary_op(*first), unary_op(*first))`
462
 
463
- shall be convertible to `T`.
464
- - Neither `unary_op` nor `binary_op` shall invalidate subranges, or
465
- modify elements in the range \[`first`, `last`\].
 
 
 
 
 
466
 
467
  *Returns:*
468
 
469
  ``` cpp
470
  GENERALIZED_SUM(binary_op, init, unary_op(*i), ...)
@@ -480,34 +277,35 @@ for every iterator `i` in \[`first`, `last`).
480
 
481
  ### Partial sum <a id="partial.sum">[[partial.sum]]</a>
482
 
483
  ``` cpp
484
  template<class InputIterator, class OutputIterator>
485
- OutputIterator partial_sum(
486
- InputIterator first, InputIterator last,
487
  OutputIterator result);
488
  template<class InputIterator, class OutputIterator, class BinaryOperation>
489
- OutputIterator partial_sum(
490
- InputIterator first, InputIterator last,
491
  OutputIterator result, BinaryOperation binary_op);
492
  ```
493
 
494
- *Requires:* `InputIterator`’s value type shall be constructible from the
495
- type of `*first`. The result of the expression `acc + *i` or
496
- `binary_op(acc, *i)` shall be implicitly convertible to
497
- `InputIterator`’s value type. `acc` shall be
498
- writable ([[iterator.requirements.general]]) to the `result` output
499
- iterator. In the ranges \[`first`, `last`\] and \[`result`,
500
- `result + (last - first)`\] `binary_op` shall neither modify elements
501
- nor invalidate iterators or subranges.[^16]
 
502
 
503
  *Effects:* For a non-empty range, the function creates an accumulator
504
  `acc` whose type is `InputIterator`’s value type, initializes it with
505
  `*first`, and assigns the result to `*result`. For every iterator `i` in
506
  \[`first + 1`, `last`) in order, `acc` is then modified by
507
- `acc = acc + *i` or `acc = binary_op(acc, *i)` and the result is
508
- assigned to `*(result + (i - first))`.
509
 
510
  *Returns:* `result + (last - first)`.
511
 
512
  *Complexity:* Exactly `(last - first) - 1` applications of the binary
513
  operation.
@@ -516,27 +314,27 @@ operation.
516
 
517
  ### Exclusive scan <a id="exclusive.scan">[[exclusive.scan]]</a>
518
 
519
  ``` cpp
520
  template<class InputIterator, class OutputIterator, class T>
521
- OutputIterator exclusive_scan(InputIterator first, InputIterator last,
522
- OutputIterator result,
523
- T init);
524
  ```
525
 
526
  *Effects:* Equivalent to:
527
 
528
  ``` cpp
529
  return exclusive_scan(first, last, result, init, plus<>());
530
  ```
531
 
532
  ``` cpp
533
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T>
534
- ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec,
 
535
  ForwardIterator1 first, ForwardIterator1 last,
536
- ForwardIterator2 result,
537
- T init);
538
  ```
539
 
540
  *Effects:* Equivalent to:
541
 
542
  ``` cpp
@@ -544,28 +342,35 @@ return exclusive_scan(std::forward<ExecutionPolicy>(exec),
544
  first, last, result, init, plus<>());
545
  ```
546
 
547
  ``` cpp
548
  template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
549
- OutputIterator exclusive_scan(InputIterator first, InputIterator last,
550
- OutputIterator result,
551
- T init, BinaryOperation binary_op);
552
  template<class ExecutionPolicy,
553
  class ForwardIterator1, class ForwardIterator2, class T, class BinaryOperation>
554
- ForwardIterator2 exclusive_scan(ExecutionPolicy&& exec,
 
555
  ForwardIterator1 first, ForwardIterator1 last,
556
- ForwardIterator2 result,
557
- T init, BinaryOperation binary_op);
558
  ```
559
 
560
- *Requires:*
561
 
562
- - `T` shall be `MoveConstructible` (Table  [[tab:moveconstructible]]).
563
- - All of `binary_op(init, init)`, `binary_op(init, *first)`, and
564
- `binary_op(*first, *first)` shall be convertible to `T`.
565
- - `binary_op` shall neither invalidate iterators or subranges, nor
566
- modify elements in the ranges \[`first`, `last`\] or \[`result`,
 
 
 
 
 
 
 
567
  `result + (last - first)`\].
568
 
569
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
570
  through `result + K` the value of:
571
 
@@ -579,19 +384,20 @@ GENERALIZED_NONCOMMUTATIVE_SUM(
579
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
580
 
581
  *Remarks:* `result` may be equal to `first`.
582
 
583
  [*Note 1*: The difference between `exclusive_scan` and `inclusive_scan`
584
- is that `exclusive_scan` excludes the `i`th input element from the `i`th
585
  sum. If `binary_op` is not mathematically associative, the behavior of
586
  `exclusive_scan` may be nondeterministic. — *end note*]
587
 
588
  ### Inclusive scan <a id="inclusive.scan">[[inclusive.scan]]</a>
589
 
590
  ``` cpp
591
  template<class InputIterator, class OutputIterator>
592
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
 
593
  OutputIterator result);
594
  ```
595
 
596
  *Effects:* Equivalent to:
597
 
@@ -599,11 +405,12 @@ template<class InputIterator, class OutputIterator>
599
  return inclusive_scan(first, last, result, plus<>());
600
  ```
601
 
602
  ``` cpp
603
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
604
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec,
 
605
  ForwardIterator1 first, ForwardIterator1 last,
606
  ForwardIterator2 result);
607
  ```
608
 
609
  *Effects:* Equivalent to:
@@ -612,43 +419,50 @@ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
612
  return inclusive_scan(std::forward<ExecutionPolicy>(exec), first, last, result, plus<>());
613
  ```
614
 
615
  ``` cpp
616
  template<class InputIterator, class OutputIterator, class BinaryOperation>
617
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
618
- OutputIterator result,
619
- BinaryOperation binary_op);
620
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
621
  class BinaryOperation>
622
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec,
 
623
  ForwardIterator1 first, ForwardIterator1 last,
624
- ForwardIterator2 result,
625
- BinaryOperation binary_op);
626
 
627
  template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
628
- OutputIterator inclusive_scan(InputIterator first, InputIterator last,
629
- OutputIterator result,
630
- BinaryOperation binary_op, T init);
631
  template<class ExecutionPolicy,
632
  class ForwardIterator1, class ForwardIterator2, class BinaryOperation, class T>
633
- ForwardIterator2 inclusive_scan(ExecutionPolicy&& exec,
 
634
  ForwardIterator1 first, ForwardIterator1 last,
635
- ForwardIterator2 result,
636
- BinaryOperation binary_op, T init);
637
  ```
638
 
639
- *Requires:*
640
-
641
- - If `init` is provided, `T` shall be `MoveConstructible`
642
- (Table  [[tab:moveconstructible]]); otherwise, `ForwardIterator1`’s
643
- value type shall be `MoveConstructible`.
644
- - If `init` is provided, all of `binary_op(init, init)`,
645
- `binary_op(init, *first)`, and `binary_op(*first, *first)` shall be
646
- convertible to `T`; otherwise, `binary_op(*first, *first)` shall be
647
- convertible to `ForwardIterator1`’s value type.
648
- - `binary_op` shall neither invalidate iterators or subranges, nor
649
- modify elements in the ranges \[`first`, `last`\] or \[`result`,
 
 
 
 
 
 
 
650
  `result + (last - first)`\].
651
 
652
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
653
  through `result + K` the value of
654
 
@@ -665,47 +479,48 @@ through `result + K` the value of
665
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
666
 
667
  *Remarks:* `result` may be equal to `first`.
668
 
669
  [*Note 1*: The difference between `exclusive_scan` and `inclusive_scan`
670
- is that `inclusive_scan` includes the `i`th input element in the `i`th
671
- sum. If `binary_op` is not mathematically associative, the behavior of
672
  `inclusive_scan` may be nondeterministic. — *end note*]
673
 
674
  ### Transform exclusive scan <a id="transform.exclusive.scan">[[transform.exclusive.scan]]</a>
675
 
676
  ``` cpp
677
  template<class InputIterator, class OutputIterator, class T,
678
  class BinaryOperation, class UnaryOperation>
679
- OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
680
- OutputIterator result,
681
- T init,
682
- BinaryOperation binary_op,
683
- UnaryOperation unary_op);
684
  template<class ExecutionPolicy,
685
  class ForwardIterator1, class ForwardIterator2, class T,
686
  class BinaryOperation, class UnaryOperation>
687
- ForwardIterator2 transform_exclusive_scan(ExecutionPolicy&& exec,
 
688
  ForwardIterator1 first, ForwardIterator1 last,
689
- ForwardIterator2 result,
690
- T init,
691
- BinaryOperation binary_op,
692
- UnaryOperation unary_op);
693
  ```
694
 
695
- *Requires:*
696
 
697
- - `T` shall be `MoveConstructible` (Table  [[tab:moveconstructible]]).
698
- - All of
699
  - `binary_op(init, init)`,
700
  - `binary_op(init, unary_op(*first))`, and
701
  - `binary_op(unary_op(*first), unary_op(*first))`
702
 
703
- shall be convertible to `T`.
704
- - Neither `unary_op` nor `binary_op` shall invalidate iterators or
705
- subranges, or modify elements in the ranges \[`first`, `last`\] or
706
- \[`result`, `result + (last - first)`\].
 
 
 
 
 
707
 
708
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
709
  through `result + K` the value of:
710
 
711
  ``` cpp
@@ -731,56 +546,59 @@ may be nondeterministic. `transform_exclusive_scan` does not apply
731
  ### Transform inclusive scan <a id="transform.inclusive.scan">[[transform.inclusive.scan]]</a>
732
 
733
  ``` cpp
734
  template<class InputIterator, class OutputIterator,
735
  class BinaryOperation, class UnaryOperation>
736
- OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
 
737
  OutputIterator result,
738
- BinaryOperation binary_op,
739
- UnaryOperation unary_op);
740
  template<class ExecutionPolicy,
741
  class ForwardIterator1, class ForwardIterator2,
742
  class BinaryOperation, class UnaryOperation>
743
- ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec,
 
744
  ForwardIterator1 first, ForwardIterator1 last,
745
  ForwardIterator2 result,
746
- BinaryOperation binary_op,
747
- UnaryOperation unary_op);
748
  template<class InputIterator, class OutputIterator,
749
  class BinaryOperation, class UnaryOperation, class T>
750
- OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
 
751
  OutputIterator result,
752
- BinaryOperation binary_op,
753
- UnaryOperation unary_op,
754
  T init);
755
  template<class ExecutionPolicy,
756
  class ForwardIterator1, class ForwardIterator2,
757
  class BinaryOperation, class UnaryOperation, class T>
758
- ForwardIterator2 transform_inclusive_scan(ExecutionPolicy&& exec,
 
759
  ForwardIterator1 first, ForwardIterator1 last,
760
  ForwardIterator2 result,
761
- BinaryOperation binary_op,
762
- UnaryOperation unary_op,
763
  T init);
764
  ```
765
 
766
- *Requires:*
 
 
767
 
768
- - If `init` is provided, `T` shall be `MoveConstructible`
769
- (Table  [[tab:moveconstructible]]); otherwise, `ForwardIterator1`’s
770
- value type shall be `MoveConstructible`.
771
- - If `init` is provided, all of
772
  - `binary_op(init, init)`,
773
  - `binary_op(init, unary_op(*first))`, and
774
  - `binary_op(unary_op(*first), unary_op(*first))`
775
 
776
- shall be convertible to `T`; otherwise,
777
- `binary_op(unary_op(*first), unary_op(*first))` shall be convertible
778
- to `ForwardIterator1`’s value type.
779
- - Neither `unary_op` nor `binary_op` shall invalidate iterators or
780
- subranges, nor modify elements in the ranges \[`first`, `last`\] or
781
- \[`result`, `result + (last - first)`\].
 
 
 
 
 
782
 
783
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
784
  through `result + K` the value of
785
 
786
  - *GENERALIZED_NONCOMMUTATIVE_SUM*(
@@ -810,68 +628,65 @@ may be nondeterministic. `transform_inclusive_scan` does not apply
810
 
811
  ### Adjacent difference <a id="adjacent.difference">[[adjacent.difference]]</a>
812
 
813
  ``` cpp
814
  template<class InputIterator, class OutputIterator>
815
- OutputIterator
816
  adjacent_difference(InputIterator first, InputIterator last,
817
  OutputIterator result);
818
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
819
  ForwardIterator2
820
  adjacent_difference(ExecutionPolicy&& exec,
821
- ForwardIterator1 first, ForwardIterator1 last,
822
- ForwardIterator2 result);
823
 
824
  template<class InputIterator, class OutputIterator, class BinaryOperation>
825
- OutputIterator
826
  adjacent_difference(InputIterator first, InputIterator last,
827
- OutputIterator result,
828
- BinaryOperation binary_op);
829
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
830
  class BinaryOperation>
831
  ForwardIterator2
832
  adjacent_difference(ExecutionPolicy&& exec,
833
  ForwardIterator1 first, ForwardIterator1 last,
834
- ForwardIterator2 result,
835
- BinaryOperation binary_op);
836
  ```
837
 
838
- *Requires:*
839
-
840
- - For the overloads with no `ExecutionPolicy`, `InputIterator`’s value
841
- type shall be `MoveAssignable` (Table  [[tab:moveassignable]]) and
842
- shall be constructible from the type of `*first`. `acc` (defined
843
- below) shall be writable ([[iterator.requirements.general]]) to the
844
- `result` output iterator. The result of the expression `val - acc` or
845
- `binary_op(val, acc)` shall be writable to the `result` output
846
- iterator.
847
- - For the overloads with an `ExecutionPolicy`, the value type of
848
- `ForwardIterator1` shall be `CopyConstructible`
849
- (Table  [[tab:copyconstructible]]), constructible from the expression
850
- `*first - *first` or `binary_op(*first, *first)`, and assignable to
851
- the value type of `ForwardIterator2`.
 
 
 
 
 
852
  - For all overloads, in the ranges \[`first`, `last`\] and \[`result`,
853
- `result + (last - first)`\], `binary_op` shall neither modify elements
854
- nor invalidate iterators or subranges.[^17]
855
 
856
  *Effects:* For the overloads with no `ExecutionPolicy` and a non-empty
857
- range, the function creates an accumulator `acc` whose type is
858
- `InputIterator`’s value type, initializes it with `*first`, and assigns
859
- the result to `*result`. For every iterator `i` in \[`first + 1`,
860
- `last`) in order, creates an object `val` whose type is
861
- `InputIterator`’s value type, initializes it with `*i`, computes
862
- `val - acc` or `binary_op(val, acc)`, assigns the result to
863
  `*(result + (i - first))`, and move assigns from `val` to `acc`.
864
 
865
- For the overloads with an `ExecutionPolicy` and a non-empty range, first
866
- the function creates an object whose type is `ForwardIterator1`’s value
867
- type, initializes it with `*first`, and assigns the result to `*result`.
868
- Then for every `d` in \[`1`, `last - first - 1`\], creates an object
869
- `val` whose type is `ForwardIterator1`’s value type, initializes it with
870
- `*(first + d) - *(first + d - 1)` or
871
- `binary_op(*(first + d), *(first + d - 1))`, and assigns the result to
872
- `*(result + d)`.
873
 
874
  *Returns:* `result + (last - first)`.
875
 
876
  *Complexity:* Exactly `(last - first) - 1` applications of the binary
877
  operation.
@@ -883,15 +698,15 @@ shall not overlap.
883
 
884
  ### Iota <a id="numeric.iota">[[numeric.iota]]</a>
885
 
886
  ``` cpp
887
  template<class ForwardIterator, class T>
888
- void iota(ForwardIterator first, ForwardIterator last, T value);
889
  ```
890
 
891
- *Requires:* `T` shall be convertible to `ForwardIterator`’s value type.
892
- The expression `++val`, where `val` has type `T`, shall be well formed.
893
 
894
  *Effects:* For each element referred to by the iterator `i` in the range
895
  \[`first`, `last`), assigns `*i = value` and increments `value` as if by
896
  `++value`.
897
 
@@ -902,39 +717,73 @@ The expression `++val`, where `val` has type `T`, shall be well formed.
902
  ``` cpp
903
  template<class M, class N>
904
  constexpr common_type_t<M,N> gcd(M m, N n);
905
  ```
906
 
907
- *Requires:* `|m|` and `|n|` shall be representable as a value of
 
 
908
  `common_type_t<M, N>`.
909
 
910
  [*Note 1*: These requirements ensure, for example, that
911
- `gcd(m, m) = |m|` is representable as a value of type
912
  `M`. — *end note*]
913
 
914
- *Remarks:* If either `M` or `N` is not an integer type, or if either is
915
- cv `bool`, the program is ill-formed.
916
-
917
  *Returns:* Zero when `m` and `n` are both zero. Otherwise, returns the
918
- greatest common divisor of `|m|` and `|n|`.
919
 
920
  *Throws:* Nothing.
921
 
922
  ### Least common multiple <a id="numeric.ops.lcm">[[numeric.ops.lcm]]</a>
923
 
924
  ``` cpp
925
  template<class M, class N>
926
  constexpr common_type_t<M,N> lcm(M m, N n);
927
  ```
928
 
929
- *Requires:* `|m|` and `|n|` shall be representable as a value of
930
- `common_type_t<M, N>`. The least common multiple of `|m|` and `|n|`
931
- shall be representable as a value of type `common_type_t<M,N>`.
932
 
933
- *Remarks:* If either `M` or `N` is not an integer type, or if either is
934
- cv `bool` the program is ill-formed.
 
935
 
936
  *Returns:* Zero when either `m` or `n` is zero. Otherwise, returns the
937
- least common multiple of `|m|` and `|n|`.
938
 
939
  *Throws:* Nothing.
940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ## Generalized numeric operations <a id="numeric.ops">[[numeric.ops]]</a>
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  [*Note 1*: The use of closed ranges as well as semi-open ranges to
4
  specify requirements throughout this subclause is
5
  intentional. — *end note*]
6
 
7
+ ### Definitions <a id="numerics.defns">[[numerics.defns]]</a>
8
+
9
+ Define `GENERALIZED_NONCOMMUTATIVE_SUM(op, a1, ..., aN)` as follows:
10
+
11
+ - `a1` when `N` is `1`, otherwise
12
+ - `op(GENERALIZED_NONCOMMUTATIVE_SUM(op, a1, ..., aK),`
13
+ `\phantom{op(}GENERALIZED_NONCOMMUTATIVE_SUM(op, aM, ..., aN))` for
14
+ any `K` where 1 < K+1 = M ≤ N.
15
+
16
+ Define `GENERALIZED_SUM(op, a1, ..., aN)` as
17
+ `GENERALIZED_NONCOMMUTATIVE_SUM(op, b1, ..., bN)`, where `b1, ..., bN`
18
+ may be any permutation of `a1, ..., aN`.
19
+
20
  ### Accumulate <a id="accumulate">[[accumulate]]</a>
21
 
22
  ``` cpp
23
  template<class InputIterator, class T>
24
+ constexpr T accumulate(InputIterator first, InputIterator last, T init);
25
  template<class InputIterator, class T, class BinaryOperation>
26
+ constexpr T accumulate(InputIterator first, InputIterator last, T init,
27
  BinaryOperation binary_op);
28
  ```
29
 
30
+ *Preconditions:* `T` meets the *Cpp17CopyConstructible*
31
+ ([[cpp17.copyconstructible]]) and *Cpp17CopyAssignable*
32
+ ([[cpp17.copyassignable]]) requirements. In the range \[`first`,
33
+ `last`\], `binary_op` neither modifies elements nor invalidates
34
+ iterators or subranges. [^6]
35
 
36
  *Effects:* Computes its result by initializing the accumulator `acc`
37
+ with the initial value `init` and then modifies it with
38
+ `acc = std::move(acc) + *i` or `acc = binary_op(std::move(acc), *i)` for
39
+ every iterator `i` in the range \[`first`, `last`) in order. [^7]
40
 
41
  ### Reduce <a id="reduce">[[reduce]]</a>
42
 
43
  ``` cpp
44
  template<class InputIterator>
45
+ constexpr typename iterator_traits<InputIterator>::value_type
46
  reduce(InputIterator first, InputIterator last);
47
  ```
48
 
49
  *Effects:* Equivalent to:
50
 
 
67
  typename iterator_traits<ForwardIterator>::value_type{});
68
  ```
69
 
70
  ``` cpp
71
  template<class InputIterator, class T>
72
+ constexpr T reduce(InputIterator first, InputIterator last, T init);
73
  ```
74
 
75
  *Effects:* Equivalent to:
76
 
77
  ``` cpp
 
90
  return reduce(std::forward<ExecutionPolicy>(exec), first, last, init, plus<>());
91
  ```
92
 
93
  ``` cpp
94
  template<class InputIterator, class T, class BinaryOperation>
95
+ constexpr T reduce(InputIterator first, InputIterator last, T init,
96
  BinaryOperation binary_op);
97
  template<class ExecutionPolicy, class ForwardIterator, class T, class BinaryOperation>
98
  T reduce(ExecutionPolicy&& exec,
99
  ForwardIterator first, ForwardIterator last, T init,
100
  BinaryOperation binary_op);
101
  ```
102
 
103
+ *Mandates:* All of
104
+
105
+ - `binary_op(init, *first)`,
106
+ - `binary_op(*first, init)`,
107
+ - `binary_op(init, init)`, and
108
+ - `binary_op(*first, *first)`
109
+
110
+ are convertible to `T`.
111
+
112
+ *Preconditions:*
113
+
114
+ - `T` meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]])
115
+ requirements.
116
+ - `binary_op` neither invalidates iterators or subranges, nor modifies
117
+ elements in the range \[`first`, `last`\].
118
 
119
  *Returns:* *GENERALIZED_SUM*(binary_op, init, \*i, ...) for every `i` in
120
  \[`first`, `last`).
121
 
122
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
 
128
 
129
  ### Inner product <a id="inner.product">[[inner.product]]</a>
130
 
131
  ``` cpp
132
  template<class InputIterator1, class InputIterator2, class T>
133
+ constexpr T inner_product(InputIterator1 first1, InputIterator1 last1,
134
  InputIterator2 first2, T init);
135
  template<class InputIterator1, class InputIterator2, class T,
136
  class BinaryOperation1, class BinaryOperation2>
137
+ constexpr T inner_product(InputIterator1 first1, InputIterator1 last1,
138
  InputIterator2 first2, T init,
139
  BinaryOperation1 binary_op1,
140
  BinaryOperation2 binary_op2);
141
  ```
142
 
143
+ *Preconditions:* `T` meets the *Cpp17CopyConstructible*
144
+ ([[cpp17.copyconstructible]]) and *Cpp17CopyAssignable*
145
+ ([[cpp17.copyassignable]]) requirements. In the ranges \[`first1`,
146
  `last1`\] and \[`first2`, `first2 + (last1 - first1)`\] `binary_op1` and
147
+ `binary_op2` neither modifies elements nor invalidates iterators or
148
+ subranges. [^8]
149
 
150
  *Effects:* Computes its result by initializing the accumulator `acc`
151
  with the initial value `init` and then modifying it with
152
+ `acc = std::move(acc) + (*i1) * (*i2)` or
153
+ `acc = binary_op1(std::move(acc), binary_op2(*i1, *i2))` for every
154
+ iterator `i1` in the range \[`first1`, `last1`) and iterator `i2` in the
155
+ range \[`first2`, `first2 + (last1 - first1)`) in order.
156
 
157
  ### Transform reduce <a id="transform.reduce">[[transform.reduce]]</a>
158
 
159
  ``` cpp
160
  template<class InputIterator1, class InputIterator2, class T>
161
+ constexpr T transform_reduce(InputIterator1 first1, InputIterator1 last1,
162
  InputIterator2 first2,
163
  T init);
 
 
 
 
 
 
164
  ```
165
 
166
  *Effects:* Equivalent to:
167
 
168
  ``` cpp
169
  return transform_reduce(first1, last1, first2, init, plus<>(), multiplies<>());
170
  ```
171
 
172
+ ``` cpp
173
+ template<class ExecutionPolicy,
174
+ class ForwardIterator1, class ForwardIterator2, class T>
175
+ T transform_reduce(ExecutionPolicy&& exec,
176
+ ForwardIterator1 first1, ForwardIterator1 last1,
177
+ ForwardIterator2 first2,
178
+ T init);
179
+ ```
180
+
181
+ *Effects:* Equivalent to:
182
+
183
+ ``` cpp
184
+ return transform_reduce(std::forward<ExecutionPolicy>(exec),
185
+ first1, last1, first2, init, plus<>(), multiplies<>());
186
+ ```
187
+
188
  ``` cpp
189
  template<class InputIterator1, class InputIterator2, class T,
190
  class BinaryOperation1, class BinaryOperation2>
191
+ constexpr T transform_reduce(InputIterator1 first1, InputIterator1 last1,
192
  InputIterator2 first2,
193
  T init,
194
  BinaryOperation1 binary_op1,
195
  BinaryOperation2 binary_op2);
196
  template<class ExecutionPolicy,
 
202
  T init,
203
  BinaryOperation1 binary_op1,
204
  BinaryOperation2 binary_op2);
205
  ```
206
 
207
+ *Mandates:* All of
208
 
 
 
209
  - `binary_op1(init, init)`,
210
  - `binary_op1(init, binary_op2(*first1, *first2))`,
211
  - `binary_op1(binary_op2(*first1, *first2), init)`, and
212
  - `binary_op1(binary_op2(*first1, *first2), binary_op2(*first1, *first2))`
213
 
214
+ are convertible to `T`.
215
+
216
+ *Preconditions:*
217
+
218
+ - `T` meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]])
219
+ requirements.
220
+ - Neither `binary_op1` nor `binary_op2` invalidates subranges, nor
221
+ modifies elements in the ranges \[`first1`, `last1`\] and \[`first2`,
222
  `first2 + (last1 - first1)`\].
223
 
224
  *Returns:*
225
 
226
  ``` cpp
 
233
  `binary_op2`.
234
 
235
  ``` cpp
236
  template<class InputIterator, class T,
237
  class BinaryOperation, class UnaryOperation>
238
+ constexpr T transform_reduce(InputIterator first, InputIterator last, T init,
239
  BinaryOperation binary_op, UnaryOperation unary_op);
240
  template<class ExecutionPolicy,
241
  class ForwardIterator, class T,
242
  class BinaryOperation, class UnaryOperation>
243
  T transform_reduce(ExecutionPolicy&& exec,
244
  ForwardIterator first, ForwardIterator last,
245
  T init, BinaryOperation binary_op, UnaryOperation unary_op);
246
  ```
247
 
248
+ *Mandates:* All of
249
 
 
 
250
  - `binary_op(init, init)`,
251
  - `binary_op(init, unary_op(*first))`,
252
  - `binary_op(unary_op(*first), init)`, and
253
  - `binary_op(unary_op(*first), unary_op(*first))`
254
 
255
+ are convertible to `T`.
256
+
257
+ *Preconditions:*
258
+
259
+ - `T` meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]])
260
+ requirements.
261
+ - Neither `unary_op` nor `binary_op` invalidates subranges, nor modifies
262
+ elements in the range \[`first`, `last`\].
263
 
264
  *Returns:*
265
 
266
  ``` cpp
267
  GENERALIZED_SUM(binary_op, init, unary_op(*i), ...)
 
277
 
278
  ### Partial sum <a id="partial.sum">[[partial.sum]]</a>
279
 
280
  ``` cpp
281
  template<class InputIterator, class OutputIterator>
282
+ constexpr OutputIterator
283
+ partial_sum(InputIterator first, InputIterator last,
284
  OutputIterator result);
285
  template<class InputIterator, class OutputIterator, class BinaryOperation>
286
+ constexpr OutputIterator
287
+ partial_sum(InputIterator first, InputIterator last,
288
  OutputIterator result, BinaryOperation binary_op);
289
  ```
290
 
291
+ *Mandates:* `InputIterator`’s value type is constructible from `*first`.
292
+ The result of the expression `std::move(acc) + *i` or
293
+ `binary_op(std::move(acc), *i)` is implicitly convertible to
294
+ `InputIterator`’s value type. `acc` is
295
+ writable [[iterator.requirements.general]] to `result`.
296
+
297
+ *Preconditions:* In the ranges \[`first`, `last`\] and \[`result`,
298
+ `result + (last - first)`\] `binary_op` neither modifies elements nor
299
+ invalidates iterators or subranges. [^9]
300
 
301
  *Effects:* For a non-empty range, the function creates an accumulator
302
  `acc` whose type is `InputIterator`’s value type, initializes it with
303
  `*first`, and assigns the result to `*result`. For every iterator `i` in
304
  \[`first + 1`, `last`) in order, `acc` is then modified by
305
+ `acc = std::move(acc) + *i` or `acc = binary_op(std::move(acc), *i)` and
306
+ the result is assigned to `*(result + (i - first))`.
307
 
308
  *Returns:* `result + (last - first)`.
309
 
310
  *Complexity:* Exactly `(last - first) - 1` applications of the binary
311
  operation.
 
314
 
315
  ### Exclusive scan <a id="exclusive.scan">[[exclusive.scan]]</a>
316
 
317
  ``` cpp
318
  template<class InputIterator, class OutputIterator, class T>
319
+ constexpr OutputIterator
320
+ exclusive_scan(InputIterator first, InputIterator last,
321
+ OutputIterator result, T init);
322
  ```
323
 
324
  *Effects:* Equivalent to:
325
 
326
  ``` cpp
327
  return exclusive_scan(first, last, result, init, plus<>());
328
  ```
329
 
330
  ``` cpp
331
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T>
332
+ ForwardIterator2
333
+ exclusive_scan(ExecutionPolicy&& exec,
334
  ForwardIterator1 first, ForwardIterator1 last,
335
+ ForwardIterator2 result, T init);
 
336
  ```
337
 
338
  *Effects:* Equivalent to:
339
 
340
  ``` cpp
 
342
  first, last, result, init, plus<>());
343
  ```
344
 
345
  ``` cpp
346
  template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
347
+ constexpr OutputIterator
348
+ exclusive_scan(InputIterator first, InputIterator last,
349
+ OutputIterator result, T init, BinaryOperation binary_op);
350
  template<class ExecutionPolicy,
351
  class ForwardIterator1, class ForwardIterator2, class T, class BinaryOperation>
352
+ ForwardIterator2
353
+ exclusive_scan(ExecutionPolicy&& exec,
354
  ForwardIterator1 first, ForwardIterator1 last,
355
+ ForwardIterator2 result, T init, BinaryOperation binary_op);
 
356
  ```
357
 
358
+ *Mandates:* All of
359
 
360
+ - `binary_op(init, init)`,
361
+ - `binary_op(init, *first)`, and
362
+ - `binary_op(*first, *first)`
363
+
364
+ are convertible to `T`.
365
+
366
+ *Preconditions:*
367
+
368
+ - `T` meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]])
369
+ requirements.
370
+ - `binary_op` neither invalidates iterators or subranges, nor modifies
371
+ elements in the ranges \[`first`, `last`\] or \[`result`,
372
  `result + (last - first)`\].
373
 
374
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
375
  through `result + K` the value of:
376
 
 
384
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
385
 
386
  *Remarks:* `result` may be equal to `first`.
387
 
388
  [*Note 1*: The difference between `exclusive_scan` and `inclusive_scan`
389
+ is that `exclusive_scan` excludes the iᵗʰ input element from the iᵗʰ
390
  sum. If `binary_op` is not mathematically associative, the behavior of
391
  `exclusive_scan` may be nondeterministic. — *end note*]
392
 
393
  ### Inclusive scan <a id="inclusive.scan">[[inclusive.scan]]</a>
394
 
395
  ``` cpp
396
  template<class InputIterator, class OutputIterator>
397
+ constexpr OutputIterator
398
+ inclusive_scan(InputIterator first, InputIterator last,
399
  OutputIterator result);
400
  ```
401
 
402
  *Effects:* Equivalent to:
403
 
 
405
  return inclusive_scan(first, last, result, plus<>());
406
  ```
407
 
408
  ``` cpp
409
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
410
+ ForwardIterator2
411
+ inclusive_scan(ExecutionPolicy&& exec,
412
  ForwardIterator1 first, ForwardIterator1 last,
413
  ForwardIterator2 result);
414
  ```
415
 
416
  *Effects:* Equivalent to:
 
419
  return inclusive_scan(std::forward<ExecutionPolicy>(exec), first, last, result, plus<>());
420
  ```
421
 
422
  ``` cpp
423
  template<class InputIterator, class OutputIterator, class BinaryOperation>
424
+ constexpr OutputIterator
425
+ inclusive_scan(InputIterator first, InputIterator last,
426
+ OutputIterator result, BinaryOperation binary_op);
427
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
428
  class BinaryOperation>
429
+ ForwardIterator2
430
+ inclusive_scan(ExecutionPolicy&& exec,
431
  ForwardIterator1 first, ForwardIterator1 last,
432
+ ForwardIterator2 result, BinaryOperation binary_op);
 
433
 
434
  template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
435
+ constexpr OutputIterator
436
+ inclusive_scan(InputIterator first, InputIterator last,
437
+ OutputIterator result, BinaryOperation binary_op, T init);
438
  template<class ExecutionPolicy,
439
  class ForwardIterator1, class ForwardIterator2, class BinaryOperation, class T>
440
+ ForwardIterator2
441
+ inclusive_scan(ExecutionPolicy&& exec,
442
  ForwardIterator1 first, ForwardIterator1 last,
443
+ ForwardIterator2 result, BinaryOperation binary_op, T init);
 
444
  ```
445
 
446
+ Let `U` be the value type of `decltype(first)`.
447
+
448
+ *Mandates:* If `init` is provided, all of
449
+
450
+ - `binary_op(init, init)`,
451
+ - `binary_op(init, *first)`, and
452
+ - `binary_op(*first, *first)`
453
+
454
+ are convertible to `T`; otherwise, `binary_op(*first, *first)` is
455
+ convertible to `U`.
456
+
457
+ *Preconditions:*
458
+
459
+ - If `init` is provided, `T` meets the *Cpp17MoveConstructible*
460
+ ([[cpp17.moveconstructible]]) requirements; otherwise, `U` meets the
461
+ *Cpp17MoveConstructible* requirements.
462
+ - `binary_op` neither invalidates iterators or subranges, nor modifies
463
+ elements in the ranges \[`first`, `last`\] or \[`result`,
464
  `result + (last - first)`\].
465
 
466
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
467
  through `result + K` the value of
468
 
 
479
  *Complexity:* 𝑂(`last - first`) applications of `binary_op`.
480
 
481
  *Remarks:* `result` may be equal to `first`.
482
 
483
  [*Note 1*: The difference between `exclusive_scan` and `inclusive_scan`
484
+ is that `inclusive_scan` includes the iᵗʰ input element in the iᵗʰ sum.
485
+ If `binary_op` is not mathematically associative, the behavior of
486
  `inclusive_scan` may be nondeterministic. — *end note*]
487
 
488
  ### Transform exclusive scan <a id="transform.exclusive.scan">[[transform.exclusive.scan]]</a>
489
 
490
  ``` cpp
491
  template<class InputIterator, class OutputIterator, class T,
492
  class BinaryOperation, class UnaryOperation>
493
+ constexpr OutputIterator
494
+ transform_exclusive_scan(InputIterator first, InputIterator last,
495
+ OutputIterator result, T init,
496
+ BinaryOperation binary_op, UnaryOperation unary_op);
 
497
  template<class ExecutionPolicy,
498
  class ForwardIterator1, class ForwardIterator2, class T,
499
  class BinaryOperation, class UnaryOperation>
500
+ ForwardIterator2
501
+ transform_exclusive_scan(ExecutionPolicy&& exec,
502
  ForwardIterator1 first, ForwardIterator1 last,
503
+ ForwardIterator2 result, T init,
504
+ BinaryOperation binary_op, UnaryOperation unary_op);
 
 
505
  ```
506
 
507
+ *Mandates:* All of
508
 
 
 
509
  - `binary_op(init, init)`,
510
  - `binary_op(init, unary_op(*first))`, and
511
  - `binary_op(unary_op(*first), unary_op(*first))`
512
 
513
+ are convertible to `T`.
514
+
515
+ *Preconditions:*
516
+
517
+ - `T` meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]])
518
+ requirements.
519
+ - Neither `unary_op` nor `binary_op` invalidates iterators or subranges,
520
+ nor modifies elements in the ranges \[`first`, `last`\] or \[`result`,
521
+ `result + (last - first)`\].
522
 
523
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
524
  through `result + K` the value of:
525
 
526
  ``` cpp
 
546
  ### Transform inclusive scan <a id="transform.inclusive.scan">[[transform.inclusive.scan]]</a>
547
 
548
  ``` cpp
549
  template<class InputIterator, class OutputIterator,
550
  class BinaryOperation, class UnaryOperation>
551
+ constexpr OutputIterator
552
+ transform_inclusive_scan(InputIterator first, InputIterator last,
553
  OutputIterator result,
554
+ BinaryOperation binary_op, UnaryOperation unary_op);
 
555
  template<class ExecutionPolicy,
556
  class ForwardIterator1, class ForwardIterator2,
557
  class BinaryOperation, class UnaryOperation>
558
+ ForwardIterator2
559
+ transform_inclusive_scan(ExecutionPolicy&& exec,
560
  ForwardIterator1 first, ForwardIterator1 last,
561
  ForwardIterator2 result,
562
+ BinaryOperation binary_op, UnaryOperation unary_op);
 
563
  template<class InputIterator, class OutputIterator,
564
  class BinaryOperation, class UnaryOperation, class T>
565
+ constexpr OutputIterator
566
+ transform_inclusive_scan(InputIterator first, InputIterator last,
567
  OutputIterator result,
568
+ BinaryOperation binary_op, UnaryOperation unary_op,
 
569
  T init);
570
  template<class ExecutionPolicy,
571
  class ForwardIterator1, class ForwardIterator2,
572
  class BinaryOperation, class UnaryOperation, class T>
573
+ ForwardIterator2
574
+ transform_inclusive_scan(ExecutionPolicy&& exec,
575
  ForwardIterator1 first, ForwardIterator1 last,
576
  ForwardIterator2 result,
577
+ BinaryOperation binary_op, UnaryOperation unary_op,
 
578
  T init);
579
  ```
580
 
581
+ Let `U` be the value type of `decltype(first)`.
582
+
583
+ *Mandates:* If `init` is provided, all of
584
 
 
 
 
 
585
  - `binary_op(init, init)`,
586
  - `binary_op(init, unary_op(*first))`, and
587
  - `binary_op(unary_op(*first), unary_op(*first))`
588
 
589
+ are convertible to `T`; otherwise,
590
+ `binary_op(unary_op(*first), unary_op(*first))` is convertible to `U`.
591
+
592
+ *Preconditions:*
593
+
594
+ - If `init` is provided, `T` meets the *Cpp17MoveConstructible*
595
+ ([[cpp17.moveconstructible]]) requirements; otherwise, `U` meets the
596
+ *Cpp17MoveConstructible* requirements.
597
+ - Neither `unary_op` nor `binary_op` invalidates iterators or subranges,
598
+ nor modifies elements in the ranges \[`first`, `last`\] or \[`result`,
599
+ `result + (last - first)`\].
600
 
601
  *Effects:* For each integer `K` in \[`0`, `last - first`) assigns
602
  through `result + K` the value of
603
 
604
  - *GENERALIZED_NONCOMMUTATIVE_SUM*(
 
628
 
629
  ### Adjacent difference <a id="adjacent.difference">[[adjacent.difference]]</a>
630
 
631
  ``` cpp
632
  template<class InputIterator, class OutputIterator>
633
+ constexpr OutputIterator
634
  adjacent_difference(InputIterator first, InputIterator last,
635
  OutputIterator result);
636
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
637
  ForwardIterator2
638
  adjacent_difference(ExecutionPolicy&& exec,
639
+ ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result);
 
640
 
641
  template<class InputIterator, class OutputIterator, class BinaryOperation>
642
+ constexpr OutputIterator
643
  adjacent_difference(InputIterator first, InputIterator last,
644
+ OutputIterator result, BinaryOperation binary_op);
 
645
  template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
646
  class BinaryOperation>
647
  ForwardIterator2
648
  adjacent_difference(ExecutionPolicy&& exec,
649
  ForwardIterator1 first, ForwardIterator1 last,
650
+ ForwardIterator2 result, BinaryOperation binary_op);
 
651
  ```
652
 
653
+ Let `T` be the value type of `decltype(first)`. For the overloads that
654
+ do not take an argument `binary_op`, let `binary_op` be an lvalue that
655
+ denotes an object of type `minus<>`.
656
+
657
+ *Mandates:*
658
+
659
+ - For the overloads with no `ExecutionPolicy`, `T` is constructible from
660
+ `*first`. `acc` (defined below) is
661
+ writable [[iterator.requirements.general]] to the `result` output
662
+ iterator. The result of the expression
663
+ `binary_op(val, std::move(acc))` is writable to `result`.
664
+ - For the overloads with an `ExecutionPolicy`, the result of the
665
+ expressions `binary_op(*first, *first)` and `*first` are writable to
666
+ `result`.
667
+
668
+ *Preconditions:*
669
+
670
+ - For the overloads with no `ExecutionPolicy`, `T` meets the
671
+ *Cpp17MoveAssignable* ([[cpp17.moveassignable]]) requirements.
672
  - For all overloads, in the ranges \[`first`, `last`\] and \[`result`,
673
+ `result + (last - first)`\], `binary_op` neither modifies elements nor
674
+ invalidate iterators or subranges. [^10]
675
 
676
  *Effects:* For the overloads with no `ExecutionPolicy` and a non-empty
677
+ range, the function creates an accumulator `acc` of type `T`,
678
+ initializes it with `*first`, and assigns the result to `*result`. For
679
+ every iterator `i` in \[`first + 1`, `last`) in order, creates an object
680
+ `val` whose type is `T`, initializes it with `*i`, computes
681
+ `binary_op(val, std::move(acc))`, assigns the result to
 
682
  `*(result + (i - first))`, and move assigns from `val` to `acc`.
683
 
684
+ For the overloads with an `ExecutionPolicy` and a non-empty range,
685
+ performs `*result = *first`. Then, for every `d` in
686
+ `[1, last - first - 1]`, performs
687
+ `*(result + d) = binary_op(*(first + d), *(first + (d - 1)))`.
 
 
 
 
688
 
689
  *Returns:* `result + (last - first)`.
690
 
691
  *Complexity:* Exactly `(last - first) - 1` applications of the binary
692
  operation.
 
698
 
699
  ### Iota <a id="numeric.iota">[[numeric.iota]]</a>
700
 
701
  ``` cpp
702
  template<class ForwardIterator, class T>
703
+ constexpr void iota(ForwardIterator first, ForwardIterator last, T value);
704
  ```
705
 
706
+ *Mandates:* `T` is convertible to `ForwardIterator`’s value type. The
707
+ expression `++val`, where `val` has type `T`, is well-formed.
708
 
709
  *Effects:* For each element referred to by the iterator `i` in the range
710
  \[`first`, `last`), assigns `*i = value` and increments `value` as if by
711
  `++value`.
712
 
 
717
  ``` cpp
718
  template<class M, class N>
719
  constexpr common_type_t<M,N> gcd(M m, N n);
720
  ```
721
 
722
+ *Mandates:* `M` and `N` both are integer types and not cv `bool`.
723
+
724
+ *Preconditions:* |`m`| and |`n`| are representable as a value of
725
  `common_type_t<M, N>`.
726
 
727
  [*Note 1*: These requirements ensure, for example, that
728
+ `gcd(m, m)` = |`m`| is representable as a value of type
729
  `M`. — *end note*]
730
 
 
 
 
731
  *Returns:* Zero when `m` and `n` are both zero. Otherwise, returns the
732
+ greatest common divisor of |`m`| and |`n`|.
733
 
734
  *Throws:* Nothing.
735
 
736
  ### Least common multiple <a id="numeric.ops.lcm">[[numeric.ops.lcm]]</a>
737
 
738
  ``` cpp
739
  template<class M, class N>
740
  constexpr common_type_t<M,N> lcm(M m, N n);
741
  ```
742
 
743
+ *Mandates:* `M` and `N` both are integer types and not cv `bool`.
 
 
744
 
745
+ *Preconditions:* |`m`| and |`n`| are representable as a value of
746
+ `common_type_t<M, N>`. The least common multiple of |`m`| and |`n`| is
747
+ representable as a value of type `common_type_t<M,N>`.
748
 
749
  *Returns:* Zero when either `m` or `n` is zero. Otherwise, returns the
750
+ least common multiple of |`m`| and |`n`|.
751
 
752
  *Throws:* Nothing.
753
 
754
+ ### Midpoint <a id="numeric.ops.midpoint">[[numeric.ops.midpoint]]</a>
755
+
756
+ ``` cpp
757
+ template<class T>
758
+ constexpr T midpoint(T a, T b) noexcept;
759
+ ```
760
+
761
+ *Constraints:* `T` is an arithmetic type other than `bool`.
762
+
763
+ *Returns:* Half the sum of `a` and `b`. If `T` is an integer type and
764
+ the sum is odd, the result is rounded towards `a`.
765
+
766
+ *Remarks:* No overflow occurs. If `T` is a floating-point type, at most
767
+ one inexact operation occurs.
768
+
769
+ ``` cpp
770
+ template<class T>
771
+ constexpr T* midpoint(T* a, T* b);
772
+ ```
773
+
774
+ *Constraints:* `T` is an object type.
775
+
776
+ *Mandates:* `T` is a complete type.
777
+
778
+ *Preconditions:* `a` and `b` point to, respectively, elements i and j of
779
+ the same array object `x`.
780
+
781
+ [*Note 1*: As specified in [[basic.compound]], an object that is not an
782
+ array element is considered to belong to a single-element array for this
783
+ purpose and a pointer past the last element of an array of n elements is
784
+ considered to be equivalent to a pointer to a hypothetical array element
785
+ n for this purpose. — *end note*]
786
+
787
+ *Returns:* A pointer to array element $i+\frac{j-i}{2}$ of `x`, where
788
+ the result of the division is truncated towards zero.
789
+