Skip to content

Commit 67a42a5

Browse files
committed
P1068R11 Vector API for random number generation
- adjust subclause labels - adjust subclause heading to fit the surroundings
1 parent fa7c7e5 commit 67a42a5

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

@@ -11580,6 +11581,138 @@
1158011581
\end{codeblock}
1158111582
\end{itemdescr}
1158211583

11584+
\rSec1[alg.rand]{Specialized \tcode{<random>} algorithms}
11585+
11586+
\rSec2[alg.rand.general]{General}
11587+
11588+
\pnum
11589+
The contents specified in \ref{alg.rand}
11590+
are declared in the header \libheaderrefx{random}{rand.synopsis}.
11591+
11592+
\rSec2[alg.rand.generate]{\tcode{generate_random}}
11593+
11594+
\begin{itemdecl}
11595+
template<class R, class G>
11596+
requires @\libconcept{output_range}@<R, invoke_result_t<G&>> && @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11597+
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g);
11598+
\end{itemdecl}
11599+
11600+
\begin{itemdescr}
11601+
\pnum
11602+
\effects
11603+
\begin{itemize}
11604+
\item
11605+
Calls \tcode{g.generate_random(std::forward<R>(r))}
11606+
if this expression is well-formed.
11607+
\item
11608+
Otherwise, if \tcode{R} models \tcode{sized_range},
11609+
fills \tcode{r} with \tcode{ranges::size(r)} values of
11610+
type \tcode{invoke_result_t<G\&>} by performing
11611+
an unspecified number of invocations of
11612+
the form \tcode{g()} or \tcode{g.generate_random(s)},
11613+
if such an expression is well-formed for a value \tcode{N} and
11614+
an object \tcode{s} of type \tcode{span<invoke_result_t<G\&>, N>}.
11615+
\begin{note}
11616+
Values of \tcode{N} can differ between invocations.
11617+
\end{note}
11618+
\item
11619+
Otherwise, calls \tcode{ranges::generate(std::forward<R>(r), ref(g))}.
11620+
\end{itemize}
11621+
11622+
\pnum
11623+
\returns
11624+
\tcode{ranges::end(r)}.
11625+
11626+
\pnum
11627+
\remarks
11628+
The effects of \tcode{generate_random(r, g)} shall be equivalent to
11629+
\tcode{ranges::generate(std::for\-ward<R>(r), ref(g))}.
11630+
\begin{note}
11631+
This implies that \tcode{g.generate_random(a)} fills \tcode{a}
11632+
with the same values as produced by invocation of \tcode{g()}.
11633+
\end{note}
11634+
\end{itemdescr}
11635+
11636+
\begin{itemdecl}
11637+
template<class G, @\libconcept{output_iterator}@<invoke_result_t<G&>> O, @\libconcept{sentinel_for}@<O> S>
11638+
requires @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11639+
constexpr O ranges::generate_random(O first, S last, G&& g);
11640+
\end{itemdecl}
11641+
11642+
\begin{itemdescr}
11643+
\pnum
11644+
\effects
11645+
Equivalent to:
11646+
\begin{codeblock}
11647+
return generate_random(subrange<O, S>(std::move(first), last), g);
11648+
\end{codeblock}
11649+
\end{itemdescr}
11650+
11651+
\begin{itemdecl}
11652+
template<class R, class G, class D>
11653+
requires @\libconcept{output_range}@<R, invoke_result_t<D&, G&>> && @\libconcept{invocable}@<D&, G&> &&
11654+
@\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11655+
constexpr borrowed_iterator_t<R> ranges::generate_random(R&& r, G&& g, D&& d);
11656+
\end{itemdecl}
11657+
11658+
\begin{itemdescr}
11659+
\pnum
11660+
\effects
11661+
\begin{itemize}
11662+
\item
11663+
Calls \tcode{d.generate_random(std::forward<R>(r), g)}
11664+
if this expression is well-formed.
11665+
\item
11666+
Otherwise, if \tcode{R} models \tcode{sized_range},
11667+
fills \tcode{r} with \tcode{ranges::size(r)} values of
11668+
type \tcode{invoke_result_t<D\&, G\&>}
11669+
by performing an unspecified number of invocations of
11670+
the form \tcode{invoke(d, g)} or \tcode{d.generate_random(s, g)},
11671+
if such an expression is well-formed
11672+
for a value \tcode{N} and
11673+
an object \tcode{s} of type \tcode{span<invoke_result_t<D\&, G\&>, N>}.
11674+
\begin{note}
11675+
Values of N can differ between invocations.
11676+
\end{note}
11677+
\item
11678+
Otherwise, calls
11679+
\begin{codeblock}
11680+
ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); });
11681+
\end{codeblock}
11682+
\end{itemize}
11683+
11684+
\pnum
11685+
\returns
11686+
\tcode{ranges::end(r)}
11687+
11688+
\pnum
11689+
\remarks
11690+
The effects of \tcode{generate_random(r, g, d)} shall be equivalent to
11691+
\begin{codeblock}
11692+
ranges::generate(std::forward<R>(r), [&d, &g] { return invoke(d, g); })}
11693+
\end{codeblock}
11694+
\begin{note}
11695+
This implies that \tcode{d.generate_random(a, g)}
11696+
fills \tcode{a} with the values with the same random distribution
11697+
as produced by invocation of \tcode{invoke(d, g)}.
11698+
\end{note}
11699+
\end{itemdescr}
11700+
11701+
\begin{itemdecl}
11702+
template<class G, class D, @\libconcept{output_iterator}@<invoke_result_t<D&, G&>> O, @\libconcept{sentinel_for}@<O> S>
11703+
requires @\libconcept{invocable}@<D&, G&> && @\libconcept{uniform_random_bit_generator}@<remove_cvref_t<G>>
11704+
constexpr O ranges::generate_random(O first, S last, G&& g, D&& d);
11705+
\end{itemdecl}
11706+
11707+
\begin{itemdescr}
11708+
\pnum
11709+
\effects
11710+
Equivalent to:
11711+
\begin{codeblock}
11712+
return generate_random(subrange<O, S>(std::move(first), last), g, d);
11713+
\end{codeblock}
11714+
\end{itemdescr}
11715+
1158311716
\rSec1[alg.c.library]{C library algorithms}
1158411717

1158511718
\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
@@ -664,6 +664,7 @@
664664
#define @\defnlibxname{cpp_lib_fstream_native_handle}@ 202306L // also in \libheader{fstream}
665665
#define @\defnlibxname{cpp_lib_function_ref}@ 202306L // also in \libheader{functional}
666666
#define @\defnlibxname{cpp_lib_gcd_lcm}@ 201606L // also in \libheader{numeric}
667+
#define @\defnlibxname{cpp_lib_generate_random}@ 202403 // also in \libheader{random}
667668
#define @\defnlibxname{cpp_lib_generator}@ 202207L // also in \libheader{generator}
668669
#define @\defnlibxname{cpp_lib_generic_associative_lookup}@ 201304L // also in \libheader{map}, \libheader{set}
669670
#define @\defnlibxname{cpp_lib_generic_unordered_lookup}@ 201811L

0 commit comments

Comments
 (0)