@@ -21,6 +21,8 @@ import scala.math.{Numeric, Ordering}
2121import scala .reflect .ClassTag
2222import scala .runtime .{AbstractFunction1 , AbstractFunction2 }
2323
24+ import IterableOnce .elemsToCopyToArray
25+
2426/**
2527 * A template trait for collections which can be traversed either once only
2628 * or one or more times.
@@ -264,18 +266,25 @@ object IterableOnce {
264266 @ inline implicit def iterableOnceExtensionMethods [A ](it : IterableOnce [A ]): IterableOnceExtensionMethods [A ] =
265267 new IterableOnceExtensionMethods [A ](it)
266268
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+ }
279288
280289 /** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
281290 @ 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] =>
988997
989998 /** Copies elements to an array, returning the number of elements written.
990999 *
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.
9921001 *
9931002 * Copying will stop once either all the elements of this $coll have been copied,
9941003 * or the end of the array is reached.
9951004 *
996- * @param xs the array to fill.
1005+ * @param dest the array to fill.
9971006 * @tparam B the type of the elements of the array.
9981007 * @return the number of elements written to the array
9991008 *
10001009 * @note Reuse: $consumesIterator
10011010 */
10021011 @ 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 )
10041014
10051015 /** Copies elements to an array, returning the number of elements written.
10061016 *
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.
10081018 *
10091019 * Copying will stop once either all the elements of this $coll have been copied,
10101020 * or the end of the array is reached.
10111021 *
1012- * @param xs the array to fill.
1022+ * @param dest the array to fill.
10131023 * @param start the starting index of xs.
10141024 * @tparam B the type of the elements of the array.
10151025 * @return the number of elements written to the array
10161026 *
10171027 * @note Reuse: $consumesIterator
10181028 */
10191029 @ 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 )
10211032
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.
10231034 *
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.
10251036 *
10261037 * 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.
10281041 *
1029- * @param xs the array to fill.
1042+ * @param dest the array to fill.
10301043 * @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.
10321045 * @tparam B the type of the elements of the array.
10331046 * @return the number of elements written to the array
10341047 *
10351048 * @note Reuse: $consumesIterator
10361049 */
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 = {
10381055 val it = iterator
10391056 var i = start
10401057 val srclen = knownSize match {
1041- case - 1 => xs .length
1058+ case - 1 => dest .length
10421059 case k => k
10431060 }
1044- val end = start + IterableOnce . elemsToCopyToArray(srclen, xs .length, start, len )
1061+ val end = start + elemsToCopyToArray(srclen, dest .length, start, n )
10451062 while (i < end && it.hasNext) {
1046- xs (i) = it.next()
1063+ dest (i) = it.next()
10471064 i += 1
10481065 }
10491066 i - start
0 commit comments