@@ -35,10 +35,7 @@ trait Builder[-A, +To] extends Growable[A] { self =>
35
35
*
36
36
* Some builder classes will optimize their representation based on the hint.
37
37
* 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.
42
39
*
43
40
* The default implementation simply ignores the hint.
44
41
*
@@ -54,9 +51,13 @@ trait Builder[-A, +To] extends Growable[A] { self =>
54
51
*
55
52
* {{{
56
53
* if (coll.knownSize != -1)
57
- * sizeHint(coll.knownSize + delta)
54
+ * if (coll.knownSize + delta <= 0) sizeHint(0)
55
+ * else sizeHint(coll.knownSize + delta)
58
56
* }}}
59
57
*
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
+ *
60
61
* Some builder classes will optimize their representation based on the hint.
61
62
* However, builder implementations are required to work correctly even if the hint is
62
63
* wrong, i.e., if a different number of elements is added.
@@ -67,7 +68,7 @@ trait Builder[-A, +To] extends Growable[A] { self =>
67
68
final def sizeHint (coll : scala.collection.IterableOnce [_], delta : Int = 0 ): Unit =
68
69
coll.knownSize match {
69
70
case - 1 =>
70
- case sz => sizeHint(sz + delta)
71
+ case sz => sizeHint(0 max sz + delta)
71
72
}
72
73
73
74
/** Gives a hint how many elements are expected to be added
0 commit comments