Skip to content

Commit 6569d4c

Browse files
authored
Merge 2024-03 LWG Motion 14
P1068R11 Vector API for random number generation
2 parents 08df8c5 + feb3cbe commit 6569d4c

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

source/algorithms.tex

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
\ref{alg.sorting} & Sorting and related operations & \\ \rowsep
2525
\ref{numeric.ops} & Generalized numeric operations & \tcode{<numeric>} \\ \rowsep
2626
\ref{specialized.algorithms} & Specialized \tcode{<memory>} algorithms & \tcode{<memory>} \\ \rowsep
27+
\ref{alg.rand} & Specialized \tcode{<random>} algorithms & \tcode{<random>} \\ \rowsep
2728
\ref{alg.c.library} & C library algorithms & \tcode{<cstdlib>} \\
2829
\end{libsumtab}
2930

@@ -11667,6 +11668,138 @@
1166711668
\end{codeblock}
1166811669
\end{itemdescr}
1166911670

11671+
\rSec1[alg.rand]{Specialized \tcode{<random>} algorithms}
11672+
11673+
\rSec2[alg.rand.general]{General}
11674+
11675+
\pnum
11676+
The contents specified in \ref{alg.rand}
11677+
are declared in the header \libheaderrefx{random}{rand.synopsis}.
11678+
11679+
\rSec2[alg.rand.generate]{\tcode{generate_random}}
11680+
11681+
\begin{itemdecl}
11682+
template<class R, class G>
11683+
requires @\libconcept{output_range}@<R, invoke_result_t<G&>> && @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11684+
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g);
11685+
\end{itemdecl}
11686+
11687+
\begin{itemdescr}
11688+
\pnum
11689+
\effects
11690+
\begin{itemize}
11691+
\item
11692+
Calls \tcode{g.generate_random(std::forward<R>(r))}
11693+
if this expression is well-formed.
11694+
\item
11695+
Otherwise, if \tcode{R} models \tcode{sized_range},
11696+
fills \tcode{r} with \tcode{ranges::size(r)} values of
11697+
type \tcode{invoke_result_t<G\&>} by performing
11698+
an unspecified number of invocations of
11699+
the form \tcode{g()} or \tcode{g.generate_random(s)},
11700+
if such an expression is well-formed for a value \tcode{N} and
11701+
an object \tcode{s} of type \tcode{span<invoke_result_t<G\&>, N>}.
11702+
\begin{note}
11703+
Values of \tcode{N} can differ between invocations.
11704+
\end{note}
11705+
\item
11706+
Otherwise, calls \tcode{ranges::generate(std::forward<R>(r), ref(g))}.
11707+
\end{itemize}
11708+
11709+
\pnum
11710+
\returns
11711+
\tcode{ranges::end(r)}.
11712+
11713+
\pnum
11714+
\remarks
11715+
The effects of \tcode{generate_random(r, g)} shall be equivalent to
11716+
\tcode{ranges::generate(std::for\-ward<R>(r), ref(g))}.
11717+
\begin{note}
11718+
This implies that \tcode{g.generate_random(a)} fills \tcode{a}
11719+
with the same values as produced by invocation of \tcode{g()}.
11720+
\end{note}
11721+
\end{itemdescr}
11722+
11723+
\begin{itemdecl}
11724+
template<class G, @\libconcept{output_iterator}@<invoke_result_t<G&>> O, @\libconcept{sentinel_for}@<O> S>
11725+
requires @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11726+
constexpr O ranges::generate_random(O first, S last, G&& g);
11727+
\end{itemdecl}
11728+
11729+
\begin{itemdescr}
11730+
\pnum
11731+
\effects
11732+
Equivalent to:
11733+
\begin{codeblock}
11734+
return generate_random(subrange<O, S>(std::move(first), last), g);
11735+
\end{codeblock}
11736+
\end{itemdescr}
11737+
11738+
\begin{itemdecl}
11739+
template<class R, class G, class D>
11740+
requires @\libconcept{output_range}@<R, invoke_result_t<D&, G&>> && @\libconcept{invocable}@<D&, G&> &&
11741+
@\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11742+
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g, D&& d);
11743+
\end{itemdecl}
11744+
11745+
\begin{itemdescr}
11746+
\pnum
11747+
\effects
11748+
\begin{itemize}
11749+
\item
11750+
Calls \tcode{d.generate_random(std::forward<R>(r), g)}
11751+
if this expression is well-formed.
11752+
\item
11753+
Otherwise, if \tcode{R} models \tcode{sized_range},
11754+
fills \tcode{r} with \tcode{ranges::size(r)} values of
11755+
type \tcode{invoke_result_t<D\&, G\&>}
11756+
by performing an unspecified number of invocations of
11757+
the form \tcode{invoke(d, g)} or \tcode{d.generate_random(s, g)},
11758+
if such an expression is well-formed
11759+
for a value \tcode{N} and
11760+
an object \tcode{s} of type \tcode{span<invoke_result_t<D\&, G\&>, N>}.
11761+
\begin{note}
11762+
Values of N can differ between invocations.
11763+
\end{note}
11764+
\item
11765+
Otherwise, calls
11766+
\begin{codeblock}
11767+
ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); });
11768+
\end{codeblock}
11769+
\end{itemize}
11770+
11771+
\pnum
11772+
\returns
11773+
\tcode{ranges::end(r)}
11774+
11775+
\pnum
11776+
\remarks
11777+
The effects of \tcode{generate_random(r, g, d)} shall be equivalent to
11778+
\begin{codeblock}
11779+
ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); })}
11780+
\end{codeblock}
11781+
\begin{note}
11782+
This implies that \tcode{d.generate_random(a, g)}
11783+
fills \tcode{a} with the values with the same random distribution
11784+
as produced by invocation of \tcode{invoke(d, g)}.
11785+
\end{note}
11786+
\end{itemdescr}
11787+
11788+
\begin{itemdecl}
11789+
template<class G, class D, @\libconcept{output_iterator}@<invoke_result_t<D&, G&>> O, @\libconcept{sentinel_for}@<O> S>
11790+
requires @\libconcept{invocable}@<D&, G&> && @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11791+
constexpr O ranges::generate_random(O first, S last, G&& g, D&& d);
11792+
\end{itemdecl}
11793+
11794+
\begin{itemdescr}
11795+
\pnum
11796+
\effects
11797+
Equivalent to:
11798+
\begin{codeblock}
11799+
return generate_random(subrange<O, S>(std::move(first), last), g, d);
11800+
\end{codeblock}
11801+
\end{itemdescr}
11802+
1167011803
\rSec1[alg.c.library]{C library algorithms}
1167111804

1167211805
\pnum

source/numerics.tex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,27 @@
14321432
template<class RealType, size_t digits, class URBG>
14331433
RealType generate_canonical(URBG& g);
14341434

1435+
namespace ranges {
1436+
// \ref{alg.rand.generate}, \tcode{generate_random}
1437+
template<class R, class G>
1438+
requires @\libconcept{output_range}@<R, invoke_result_t<G&>> &&
1439+
@\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
1440+
constexpr borrowed_iterator_t<R> generate_random(R&& r, G&& g);
1441+
1442+
template<class G, @\libconcept{output_iterator}@<invoke_result_t<G&>> O, @\libconcept{sentinel_for}@<O> S>
1443+
requires @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
1444+
constexpr O generate_random(O first, S last, G&& g);
1445+
1446+
template<class R, class G, class D>
1447+
requires @\libconcept{output_range}@<R, invoke_result_t<D&, G&>> && @\libconcept{invocable}@<D&, G&> &&
1448+
@\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
1449+
constexpr borrowed_iterator_t<R> generate_random(R&& r, G&& g, D&& d);
1450+
1451+
template<class G, class D, @\libconcept{output_iterator}@<invoke_result_t<D&, G&>> O, @\libconcept{sentinel_for}@<O> S>
1452+
requires @\libconcept{invocable}@<D&, G&> && @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
1453+
constexpr O generate_random(O first, S last, G&& g, D&& d);
1454+
}
1455+
14351456
// \ref{rand.dist.uni.int}, class template \tcode{uniform_int_distribution}
14361457
template<class IntType = int>
14371458
class uniform_int_distribution;

source/support.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@
740740
#define @\defnlibxname{cpp_lib_ranges_enumerate}@ 202302L // also in \libheader{ranges}, \libheader{version}
741741
#define @\defnlibxname{cpp_lib_ranges_find_last}@ 202207L // also in \libheader{algorithm}
742742
#define @\defnlibxname{cpp_lib_ranges_fold}@ 202207L // also in \libheader{algorithm}
743+
#define @\defnlibxname{cpp_lib_ranges_generate_random}@ 202403L // also in \libheader{random}
743744
#define @\defnlibxname{cpp_lib_ranges_iota}@ 202202L // also in \libheader{numeric}
744745
#define @\defnlibxname{cpp_lib_ranges_join_with}@ 202202L // freestanding, also in \libheader{ranges}
745746
#define @\defnlibxname{cpp_lib_ranges_repeat}@ 202207L // freestanding, also in \libheader{ranges}

0 commit comments

Comments
 (0)