@@ -21,6 +21,8 @@ import scala.math.{Numeric, Ordering}
21
21
import scala .reflect .ClassTag
22
22
import scala .runtime .{AbstractFunction1 , AbstractFunction2 }
23
23
24
+ import IterableOnce .elemsToCopyToArray
25
+
24
26
/**
25
27
* A template trait for collections which can be traversed either once only
26
28
* or one or more times.
@@ -264,18 +266,25 @@ object IterableOnce {
264
266
@ inline implicit def iterableOnceExtensionMethods [A ](it : IterableOnce [A ]): IterableOnceExtensionMethods [A ] =
265
267
new IterableOnceExtensionMethods [A ](it)
266
268
267
- /** Computes the number of elements to copy to an array from a source IterableOnce
268
- *
269
- * @param srcLen the length of the source collection
270
- * @param destLen the length of the destination array
271
- * @param start the index in the destination array at which to start copying elements to
272
- * @param len the requested number of elements to copy (we may only be able to copy less than this)
273
- * @return the number of elements that will be copied to the destination array
274
- */
275
- @ inline private [collection] def elemsToCopyToArray (srcLen : Int , destLen : Int , start : Int , len : Int ): Int =
276
- math.max(0 ,
277
- math.min(if (start < 0 ) destLen else destLen - start,
278
- math.min(len, srcLen)))
269
+ /** Computes the number of elements to copy to an array from a source IterableOnce.
270
+ *
271
+ * If `start` is less than zero, it is taken as zero.
272
+ * If any of the length inputs is less than zero, the computed result is zero.
273
+ *
274
+ * The result is the smaller of the remaining capacity in the destination and the requested count.
275
+ *
276
+ * @param srcLen the length of the source collection
277
+ * @param destLen the length of the destination array
278
+ * @param start the index in the destination array at which to start copying elements
279
+ * @param len the requested number of elements to copy (we may only be able to copy less than this)
280
+ * @return the number of elements that will be copied to the destination array
281
+ */
282
+ @ inline private [collection] def elemsToCopyToArray (srcLen : Int , destLen : Int , start : Int , len : Int ): Int = {
283
+ val limit = math.min(len, srcLen)
284
+ val capacity = if (start < 0 ) destLen else destLen - start
285
+ val total = math.min(capacity, limit)
286
+ math.max(0 , total)
287
+ }
279
288
280
289
/** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
281
290
@ inline private [collection] def copyElemsToArray [A , B >: A ](elems : IterableOnce [A ],
@@ -988,62 +997,70 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A] =>
988
997
989
998
/** Copies elements to an array, returning the number of elements written.
990
999
*
991
- * Fills the given array `xs ` starting at index `start` with values of this $coll.
1000
+ * Fills the given array `dest ` starting at index `start` with values of this $coll.
992
1001
*
993
1002
* Copying will stop once either all the elements of this $coll have been copied,
994
1003
* or the end of the array is reached.
995
1004
*
996
- * @param xs the array to fill.
1005
+ * @param dest the array to fill.
997
1006
* @tparam B the type of the elements of the array.
998
1007
* @return the number of elements written to the array
999
1008
*
1000
1009
* @note Reuse: $consumesIterator
1001
1010
*/
1002
1011
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1003
- def copyToArray [B >: A ](xs : Array [B ]): Int = copyToArray(xs, 0 , Int .MaxValue )
1012
+ def copyToArray [B >: A ](@ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ]): Int =
1013
+ copyToArray(dest, start = 0 , n = Int .MaxValue )
1004
1014
1005
1015
/** Copies elements to an array, returning the number of elements written.
1006
1016
*
1007
- * Fills the given array `xs ` starting at index `start` with values of this $coll.
1017
+ * Fills the given array `dest ` starting at index `start` with values of this $coll.
1008
1018
*
1009
1019
* Copying will stop once either all the elements of this $coll have been copied,
1010
1020
* or the end of the array is reached.
1011
1021
*
1012
- * @param xs the array to fill.
1022
+ * @param dest the array to fill.
1013
1023
* @param start the starting index of xs.
1014
1024
* @tparam B the type of the elements of the array.
1015
1025
* @return the number of elements written to the array
1016
1026
*
1017
1027
* @note Reuse: $consumesIterator
1018
1028
*/
1019
1029
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1020
- def copyToArray [B >: A ](xs : Array [B ], start : Int ): Int = copyToArray(xs, start, Int .MaxValue )
1030
+ def copyToArray [B >: A ](@ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ], start : Int ): Int =
1031
+ copyToArray(dest, start = start, n = Int .MaxValue )
1021
1032
1022
- /** Copy elements to an array, returning the number of elements written.
1033
+ /** Copies elements to an array and returns the number of elements written.
1023
1034
*
1024
- * Fills the given array `xs ` starting at index `start` with at most `len ` elements of this $coll.
1035
+ * Fills the given array `dest ` starting at index `start` with at most `n ` elements of this $coll.
1025
1036
*
1026
1037
* Copying will stop once either all the elements of this $coll have been copied,
1027
- * or the end of the array is reached, or `len` elements have been copied.
1038
+ * or the end of the array is reached, or `n` elements have been copied.
1039
+ *
1040
+ * If `start` is less than zero, it is taken as zero.
1028
1041
*
1029
- * @param xs the array to fill.
1042
+ * @param dest the array to fill.
1030
1043
* @param start the starting index of xs.
1031
- * @param len the maximal number of elements to copy.
1044
+ * @param n the maximal number of elements to copy.
1032
1045
* @tparam B the type of the elements of the array.
1033
1046
* @return the number of elements written to the array
1034
1047
*
1035
1048
* @note Reuse: $consumesIterator
1036
1049
*/
1037
- def copyToArray [B >: A ](xs : Array [B ], start : Int , len : Int ): Int = {
1050
+ def copyToArray [B >: A ](
1051
+ @ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ],
1052
+ start : Int ,
1053
+ @ deprecatedName(" len" , since= " 2.13.17" ) n : Int
1054
+ ): Int = {
1038
1055
val it = iterator
1039
1056
var i = start
1040
1057
val srclen = knownSize match {
1041
- case - 1 => xs .length
1058
+ case - 1 => dest .length
1042
1059
case k => k
1043
1060
}
1044
- val end = start + IterableOnce . elemsToCopyToArray(srclen, xs .length, start, len )
1061
+ val end = start + elemsToCopyToArray(srclen, dest .length, start, n )
1045
1062
while (i < end && it.hasNext) {
1046
- xs (i) = it.next()
1063
+ dest (i) = it.next()
1047
1064
i += 1
1048
1065
}
1049
1066
i - start
0 commit comments