Skip to content

Commit 070eb71

Browse files
committed
P2846R6 reserve_hint: Eagerly reserving memory for not-quite-sized lazy ranges
1 parent 805aac8 commit 070eb71

File tree

3 files changed

+325
-9
lines changed

3 files changed

+325
-9
lines changed

source/containers.tex

+47-5
Original file line numberDiff line numberDiff line change
@@ -1403,14 +1403,23 @@
14031403
from \tcode{*ranges::begin(rg)}.
14041404
For \tcode{vector},
14051405
if \tcode{R} models
1406-
neither \tcode{ranges::\libconcept{sized_range}} nor \tcode{ranges::\libconcept{forward_range}},
1406+
\tcode{ranges::\libconcept{approximately_sized_range}}
1407+
but not \tcode{ranges::\libconcept{sized_range}} or models
1408+
\tcode{ranges::\libconcept{input_range}}
1409+
but not \tcode{ranges::\libconcept{forward_range}},
14071410
\tcode{T} is also \oldconcept{MoveInsertable} into \tcode{X}.
14081411

14091412
\pnum
14101413
\effects
14111414
Constructs a sequence container equal to the range \tcode{rg}.
14121415
Each iterator in the range \tcode{rg} is dereferenced exactly once.
14131416

1417+
\pnum
1418+
\recommended
1419+
If \tcode{R} models \tcode{ranges::\libconcept{approximately_sized_range}} and
1420+
\tcode{ranges::distance(\linebreak{}rg) <= ranges::reserve_hint(rg)} is \tcode{true},
1421+
an implementation should not perform any reallocation.
1422+
14141423
\pnum
14151424
\ensures
14161425
\tcode{distance(begin(), end()) == ranges::distance(rg)} is \tcode{true}.
@@ -1761,7 +1770,10 @@
17611770
from \tcode{*ranges::begin(rg)}.
17621771
For \tcode{vector},
17631772
if \tcode{R} models
1764-
neither \tcode{ranges::\libconcept{sized_range}} nor \tcode{ranges::\libconcept{forward_range}},
1773+
\tcode{ranges::\libconcept{approximately_sized_range}}
1774+
but not \tcode{ranges::\libconcept{sized_range}} or models
1775+
\tcode{ranges::\libconcept{input_range}}
1776+
but not \tcode{ranges::\libconcept{forward_range}},
17651777
\tcode{T} is also \oldconcept{MoveInsertable} into \tcode{X}.
17661778
\tcode{rg} and \tcode{a} do not overlap.
17671779

@@ -1773,6 +1785,12 @@
17731785
For \tcode{vector} and \tcode{deque},
17741786
also invalidates the past-the-end iterator.
17751787
Each iterator in the range \tcode{rg} is dereferenced exactly once.
1788+
1789+
\pnum
1790+
\recommended
1791+
If \tcode{R} models \tcode{ranges::\libconcept{approximately_sized_range}} and
1792+
\tcode{ranges::distance(\linebreak{}rg) <= ranges::reserve_hint(rg)} is \tcode{true},
1793+
an implementation should not perform any reallocation.
17761794
\end{itemdescr}
17771795

17781796
\begin{itemdecl}
@@ -8908,9 +8926,18 @@
89088926
Initializes exactly $N$ elements
89098927
from the results of dereferencing successive iterators of \tcode{rg},
89108928
where $N$ is \tcode{ranges::distance(rg)}.
8911-
Performs no reallocations if \tcode{R} models
8912-
\tcode{ranges::\libconcept{forward_range}} or \tcode{ranges::\libconcept{sized_range}};
8913-
otherwise, performs order $\log N$ reallocations and
8929+
8930+
\pnum
8931+
Performs no reallocations if:
8932+
\begin{itemize}
8933+
\item
8934+
\tcode{R} models \tcode{ranges::\libconcept{approximately_sized_range}}, and
8935+
\tcode{ranges::distance(rg) <= ranges::re\-serve_hint(rg)} is \tcode{true}, or
8936+
\item
8937+
\tcode{R} models \tcode{ranges::\libconcept{forward_range}} and
8938+
\tcode{R} does not model \tcode{ranges::approximately_sized_range}.
8939+
\end{itemize}
8940+
Otherwise, performs order $\log N$ reallocations and
89148941
order $N$ calls to the copy or move constructor of \tcode{T}.
89158942
\end{itemdescr}
89168943

@@ -9161,6 +9188,21 @@
91619188
is \tcode{true}, there are no effects.
91629189
Otherwise, if an exception is thrown by the move constructor of a non-\oldconcept{CopyInsertable}
91639190
\tcode{T}, the effects are unspecified.
9191+
9192+
\pnum
9193+
For the declarations taking a range \tcode{R},
9194+
performs at most one reallocation if:
9195+
\begin{itemize}
9196+
\item
9197+
\tcode{R} models \tcode{ranges::\libconcept{approximately_sized_range}} and
9198+
\tcode{ranges::distance(rg) <= ranges::re\-serve_hint(rg)} is \tcode{true}, or
9199+
\item
9200+
\tcode{R} models \tcode{ranges::\libconcept{forward_range}} and
9201+
\tcode{R} does not model \tcode{ranges::\libconcept{approximately_sized_range}}.
9202+
\end{itemize}
9203+
For the declarations taking a pair of \tcode{InputIterator},
9204+
performs at most one reallocation if
9205+
\tcode{InputItera\-tor} models \oldconcept{ForwardIterator}.
91649206
\end{itemdescr}
91659207

91669208
\indexlibrarymember{erase}{vector}%

0 commit comments

Comments
 (0)