Skip to content

Commit b32c9dd

Browse files
committed
Improve doc for sizeHint
1 parent 5220119 commit b32c9dd

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,45 @@ trait Builder[-A, +To] extends Growable[A] { self =>
3030
/** Result collection consisting of all elements appended so far. */
3131
def result(): To
3232

33-
/** Gives a hint how many elements are expected to be added
34-
* when the next `result` is called. Some builder classes
35-
* will optimize their representation based on the hint. However,
36-
* builder implementations are still required to work correctly even if the hint is
37-
* wrong, i.e. a different number of elements is added.
38-
*
39-
* @param size the hint how many elements will be added.
40-
*/
33+
/** Gives a hint how many elements are expected to be added in total
34+
* by the time `result` is called.
35+
*
36+
* Some builder classes will optimize their representation based on the hint.
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.
42+
*
43+
* The default implementation simply ignores the hint.
44+
*
45+
* @param size the hint how many elements will be added.
46+
*/
4147
def sizeHint(size: Int): Unit = ()
4248

43-
/** Gives a hint that one expects the `result` of this builder
44-
* to have the same size as the given collection, plus some delta. This will
45-
* provide a hint only if the collection has a known size
46-
* Some builder classes
47-
* will optimize their representation based on the hint. However,
48-
* builder implementations are still required to work correctly even if the hint is
49-
* wrong, i.e. a different number of elements is added.
50-
*
51-
* @param coll the collection which serves as a hint for the result's size.
52-
* @param delta a correction to add to the `coll.size` to produce the size hint.
53-
*/
54-
final def sizeHint(coll: scala.collection.IterableOnce[_], delta: Int = 0): Unit = {
55-
val s = coll.knownSize
56-
if (s != -1) sizeHint(s + delta)
57-
}
49+
/** Gives a hint that the `result` of this builder is expected
50+
* to have the same size as the given collection, plus some delta.
51+
*
52+
* This method provides a hint only if the collection has a known size,
53+
* as specified by the following pseudocode:
54+
*
55+
* {{{
56+
* if (coll.knownSize != -1)
57+
* sizeHint(coll.knownSize + delta)
58+
* }}}
59+
*
60+
* Some builder classes will optimize their representation based on the hint.
61+
* However, builder implementations are required to work correctly even if the hint is
62+
* wrong, i.e., if a different number of elements is added.
63+
*
64+
* @param coll the collection which serves as a hint for the result's size.
65+
* @param delta a correction to add to the `coll.size` to produce the size hint (zero if omitted).
66+
*/
67+
final def sizeHint(coll: scala.collection.IterableOnce[_], delta: Int = 0): Unit =
68+
coll.knownSize match {
69+
case -1 =>
70+
case sz => sizeHint(sz + delta)
71+
}
5872

5973
/** Gives a hint how many elements are expected to be added
6074
* when the next `result` is called, together with an upper bound

0 commit comments

Comments
 (0)