Skip to content

Commit afc5afb

Browse files
committed
Size hint with delta is not negative
1 parent 50995d9 commit afc5afb

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

library/src/scala/collection/mutable/Builder.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ trait Builder[-A, +To] extends Growable[A] { self =>
3535
*
3636
* Some builder classes will optimize their representation based on the hint.
3737
* However, builder implementations are required to work correctly even if the hint is
38-
* wrong, i.e., if a different number of elements is added.
39-
*
40-
* The semantics of supplying a hint out of range, such as a size that is negative
41-
* or unreasonably large, is not specified but is implementation-dependent.
38+
* wrong, e.g., a different number of elements is added, or the hint is out of range.
4239
*
4340
* The default implementation simply ignores the hint.
4441
*
@@ -54,9 +51,13 @@ trait Builder[-A, +To] extends Growable[A] { self =>
5451
*
5552
* {{{
5653
* if (coll.knownSize != -1)
57-
* sizeHint(coll.knownSize + delta)
54+
* if (coll.knownSize + delta <= 0) sizeHint(0)
55+
* else sizeHint(coll.knownSize + delta)
5856
* }}}
5957
*
58+
* If the delta is negative and the result size is known to be negative,
59+
* then the size hint is issued at zero.
60+
*
6061
* Some builder classes will optimize their representation based on the hint.
6162
* However, builder implementations are required to work correctly even if the hint is
6263
* wrong, i.e., if a different number of elements is added.
@@ -67,7 +68,7 @@ trait Builder[-A, +To] extends Growable[A] { self =>
6768
final def sizeHint(coll: scala.collection.IterableOnce[_], delta: Int = 0): Unit =
6869
coll.knownSize match {
6970
case -1 =>
70-
case sz => sizeHint(sz + delta)
71+
case sz => sizeHint(0 max sz + delta)
7172
}
7273

7374
/** Gives a hint how many elements are expected to be added

0 commit comments

Comments
 (0)