Skip to content

Commit 9fdea42

Browse files
committed
P0220R1: algorithms (section 9).
1 parent c8e3374 commit 9fdea42

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

source/algorithms.tex

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
ForwardIterator2 first2, ForwardIterator2 last2,
246246
BinaryPredicate pred);
247247

248+
// \ref{alg.search}, search:
248249
template<class ForwardIterator1, class ForwardIterator2>
249250
ForwardIterator1 search(
250251
ForwardIterator1 first1, ForwardIterator1 last1,
@@ -284,6 +285,10 @@
284285
Size count, const T& value,
285286
BinaryPredicate pred);
286287

288+
template <class ForwardIterator, class Searcher>
289+
ForwardIterator search(ForwardIterator first, ForwardIterator last,
290+
const Searcher &searcher);
291+
287292
// \ref{alg.modifying.operations}, modifying sequence operations:
288293
// \ref{alg.copy}, copy:
289294
template<class InputIterator, class OutputIterator>
@@ -512,6 +517,13 @@
512517
ForwardIterator first, ForwardIterator middle,
513518
ForwardIterator last, OutputIterator result);
514519

520+
// \ref{alg.random.sample}, sample:
521+
template<class PopulationIterator, class SampleIterator,
522+
class Distance, class UniformRandomNumberGenerator>
523+
SampleIterator sample(PopulationIterator first, PopulationIterator last,
524+
SampleIterator out, Distance n,
525+
UniformRandomNumberGenerator&& g);
526+
515527
// \ref{alg.random.shuffle}, shuffle:
516528
template<class RandomAccessIterator, class UniformRandomNumberGenerator>
517529
void shuffle(RandomAccessIterator first,
@@ -2081,6 +2093,24 @@
20812093
applications of the corresponding predicate.
20822094
\end{itemdescr}
20832095

2096+
\indexlibrary{\idxcode{search}}%
2097+
\begin{itemdecl}
2098+
template<class ForwardIterator, class Searcher>
2099+
ForwardIterator search(ForwardIterator first, ForwardIterator last,
2100+
const Searcher& searcher);
2101+
\end{itemdecl}
2102+
2103+
\begin{itemdescr}
2104+
\pnum
2105+
\effects
2106+
Equivalent to \tcode{return searcher(first, last);}
2107+
2108+
\pnum
2109+
\remarks
2110+
\tcode{Searcher} need not meet the \tcode{CopyConstructible} requirements.
2111+
\end{itemdescr}
2112+
2113+
20842114
\rSec1[alg.modifying.operations]{Mutating sequence operations}
20852115

20862116
\rSec2[alg.copy]{Copy}
@@ -2930,14 +2960,78 @@
29302960
assignments.
29312961
\end{itemdescr}
29322962

2963+
\rSec2[alg.random.sample]{Sample}
2964+
2965+
\indexlibrary{\idxcode{sample}}%
2966+
\begin{itemdecl}
2967+
template<class PopulationIterator, class SampleIterator,
2968+
class Distance, class UniformRandomNumberGenerator>
2969+
SampleIterator sample(PopulationIterator first, PopulationIterator last,
2970+
SampleIterator out, Distance n,
2971+
UniformRandomNumberGenerator&& g);
2972+
\end{itemdecl}
2973+
2974+
\begin{itemdescr}
2975+
\pnum
2976+
\requires
2977+
\begin{itemize}
2978+
\item
2979+
\tcode{PopulationIterator} shall meet the requirements of an \tcode{InputIterator} type.
2980+
\item
2981+
\tcode{SampleIterator} shall meet the requirements of an \tcode{OutputIterator} type.
2982+
\item
2983+
\tcode{SampleIterator} shall meet the additional requirements of a \tcode{RandomAccessIterator} type
2984+
unless \tcode{PopulationIterator} meets the additional requirements of a \tcode{ForwardIterator} type.
2985+
\item
2986+
\tcode{PopulationIterator}'s value type shall be writable to \tcode{out}.
2987+
\item
2988+
\tcode{Distance} shall be an integer type.
2989+
\item
2990+
\tcode{UniformRandomNumberGenerator} shall meet the requirements of a uniform random number generator type (\ref{rand.req.urng})
2991+
whose return type is convertible to \tcode{Distance}.
2992+
\item
2993+
\tcode{out} shall not be in the range \range{first}{last}.
2994+
\end{itemize}
2995+
2996+
\pnum
2997+
\effects
2998+
Copies \tcode{min(last - first, n)} elements (the \defn{sample})
2999+
from \range{first}{last} (the \defn{population}) to \tcode{out}
3000+
such that each possible sample has equal probability of appearance.
3001+
\enternote
3002+
Algorithms that obtain such effects include \term{selection sampling}
3003+
and \term{reservoir sampling}.
3004+
\exitnote
3005+
3006+
\pnum
3007+
\returns
3008+
The end of the resulting sample range.
3009+
3010+
\pnum
3011+
\complexity
3012+
O(\tcode{last - first}).
3013+
3014+
\pnum
3015+
\remarks
3016+
\begin{itemize}
3017+
\item
3018+
Stable if and only if \tcode{PopulationIterator} meets the
3019+
requirements of a \tcode{ForwardIterator} type.
3020+
\item
3021+
To the extent that the implementation of this function makes use of
3022+
random numbers, the object \tcode{g} shall serve as the
3023+
implementation's source of randomness.
3024+
\end{itemize}
3025+
\end{itemdescr}
3026+
29333027
\rSec2[alg.random.shuffle]{Shuffle}
29343028

29353029
\indexlibrary{\idxcode{shuffle}}%
29363030
\begin{itemdecl}
29373031
template<class RandomAccessIterator, class UniformRandomNumberGenerator>
29383032
void shuffle(RandomAccessIterator first,
2939-
RandomAccessIterator last,
2940-
UniformRandomNumberGenerator&& g);
3033+
RandomAccessIterator last,
3034+
UniformRandomNumberGenerator&& g);
29413035
\end{itemdecl}
29423036

29433037
\begin{itemdescr}

0 commit comments

Comments
 (0)