@@ -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,16 +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(math.min(math.min(len, srcLen), destLen - start), 0 )
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
+ }
277
288
278
289
/** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
279
290
@ inline private [collection] def copyElemsToArray [A , B >: A ](elems : IterableOnce [A ],
@@ -986,58 +997,70 @@ trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOnce[A] =>
986
997
987
998
/** Copies elements to an array, returning the number of elements written.
988
999
*
989
- * 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.
990
1001
*
991
1002
* Copying will stop once either all the elements of this $coll have been copied,
992
1003
* or the end of the array is reached.
993
1004
*
994
- * @param xs the array to fill.
1005
+ * @param dest the array to fill.
995
1006
* @tparam B the type of the elements of the array.
996
1007
* @return the number of elements written to the array
997
1008
*
998
1009
* @note Reuse: $consumesIterator
999
1010
*/
1000
1011
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1001
- 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 )
1002
1014
1003
1015
/** Copies elements to an array, returning the number of elements written.
1004
1016
*
1005
- * 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.
1006
1018
*
1007
1019
* Copying will stop once either all the elements of this $coll have been copied,
1008
1020
* or the end of the array is reached.
1009
1021
*
1010
- * @param xs the array to fill.
1022
+ * @param dest the array to fill.
1011
1023
* @param start the starting index of xs.
1012
1024
* @tparam B the type of the elements of the array.
1013
1025
* @return the number of elements written to the array
1014
1026
*
1015
1027
* @note Reuse: $consumesIterator
1016
1028
*/
1017
1029
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1018
- 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 )
1019
1032
1020
- /** Copy elements to an array, returning the number of elements written.
1033
+ /** Copies elements to an array and returns the number of elements written.
1021
1034
*
1022
- * 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.
1023
1036
*
1024
1037
* Copying will stop once either all the elements of this $coll have been copied,
1025
- * 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.
1026
1039
*
1027
- * @param xs the array to fill.
1040
+ * If `start` is less than zero, it is taken as zero.
1041
+ *
1042
+ * @param dest the array to fill.
1028
1043
* @param start the starting index of xs.
1029
- * @param len the maximal number of elements to copy.
1044
+ * @param n the maximal number of elements to copy.
1030
1045
* @tparam B the type of the elements of the array.
1031
1046
* @return the number of elements written to the array
1032
1047
*
1033
1048
* @note Reuse: $consumesIterator
1034
1049
*/
1035
- 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 = {
1036
1055
val it = iterator
1037
1056
var i = start
1038
- val end = start + math.min(len, xs.length - start)
1057
+ val srclen = knownSize match {
1058
+ case - 1 => dest.length
1059
+ case k => k
1060
+ }
1061
+ val end = start + elemsToCopyToArray(srclen, dest.length, start, n)
1039
1062
while (i < end && it.hasNext) {
1040
- xs (i) = it.next()
1063
+ dest (i) = it.next()
1041
1064
i += 1
1042
1065
}
1043
1066
i - start
0 commit comments