@@ -24,6 +24,8 @@ import scala.math.{Numeric, Ordering}
24
24
import scala .reflect .ClassTag
25
25
import scala .runtime .{AbstractFunction1 , AbstractFunction2 }
26
26
27
+ import IterableOnce .elemsToCopyToArray
28
+
27
29
/**
28
30
* A template trait for collections which can be traversed either once only
29
31
* or one or more times.
@@ -267,16 +269,25 @@ object IterableOnce {
267
269
@ inline implicit def iterableOnceExtensionMethods [A ](it : IterableOnce [A ]): IterableOnceExtensionMethods [A ] =
268
270
new IterableOnceExtensionMethods [A ](it)
269
271
270
- /** Computes the number of elements to copy to an array from a source IterableOnce
271
- *
272
- * @param srcLen the length of the source collection
273
- * @param destLen the length of the destination array
274
- * @param start the index in the destination array at which to start copying elements to
275
- * @param len the requested number of elements to copy (we may only be able to copy less than this)
276
- * @return the number of elements that will be copied to the destination array
277
- */
278
- @ inline private [collection] def elemsToCopyToArray (srcLen : Int , destLen : Int , start : Int , len : Int ): Int =
279
- math.max(math.min(math.min(len, srcLen), destLen - start), 0 )
272
+ /** Computes the number of elements to copy to an array from a source IterableOnce.
273
+ *
274
+ * If `start` is less than zero, it is taken as zero.
275
+ * If any of the length inputs is less than zero, the computed result is zero.
276
+ *
277
+ * The result is the smaller of the remaining capacity in the destination and the requested count.
278
+ *
279
+ * @param srcLen the length of the source collection
280
+ * @param destLen the length of the destination array
281
+ * @param start the index in the destination array at which to start copying elements
282
+ * @param len the requested number of elements to copy (we may only be able to copy less than this)
283
+ * @return the number of elements that will be copied to the destination array
284
+ */
285
+ @ inline private [collection] def elemsToCopyToArray (srcLen : Int , destLen : Int , start : Int , len : Int ): Int = {
286
+ val limit = math.min(len, srcLen)
287
+ val capacity = if (start < 0 ) destLen else destLen - start
288
+ val total = math.min(capacity, limit)
289
+ math.max(0 , total)
290
+ }
280
291
281
292
/** Calls `copyToArray` on the given collection, regardless of whether or not it is an `Iterable`. */
282
293
@ inline private [collection] def copyElemsToArray [A , B >: A ](elems : IterableOnce [A ]^ ,
@@ -438,6 +449,8 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
438
449
* @return a $coll containing the elements greater than or equal to
439
450
* index `from` extending up to (but not including) index `until`
440
451
* of this $coll.
452
+ * @example
453
+ * `List('a', 'b', 'c', 'd', 'e').slice(1, 3) == List('b', 'c')`
441
454
*/
442
455
def slice (from : Int , until : Int ): C ^ {this }
443
456
@@ -987,58 +1000,70 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
987
1000
988
1001
/** Copies elements to an array, returning the number of elements written.
989
1002
*
990
- * Fills the given array `xs ` starting at index `start` with values of this $coll.
1003
+ * Fills the given array `dest ` starting at index `start` with values of this $coll.
991
1004
*
992
1005
* Copying will stop once either all the elements of this $coll have been copied,
993
1006
* or the end of the array is reached.
994
1007
*
995
- * @param xs the array to fill.
1008
+ * @param dest the array to fill.
996
1009
* @tparam B the type of the elements of the array.
997
1010
* @return the number of elements written to the array
998
1011
*
999
1012
* @note Reuse: $consumesIterator
1000
1013
*/
1001
1014
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1002
- def copyToArray [B >: A ](xs : Array [B ]): Int = copyToArray(xs, 0 , Int .MaxValue )
1015
+ def copyToArray [B >: A ](@ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ]): Int =
1016
+ copyToArray(dest, start = 0 , n = Int .MaxValue )
1003
1017
1004
1018
/** Copies elements to an array, returning the number of elements written.
1005
1019
*
1006
- * Fills the given array `xs ` starting at index `start` with values of this $coll.
1020
+ * Fills the given array `dest ` starting at index `start` with values of this $coll.
1007
1021
*
1008
1022
* Copying will stop once either all the elements of this $coll have been copied,
1009
1023
* or the end of the array is reached.
1010
1024
*
1011
- * @param xs the array to fill.
1025
+ * @param dest the array to fill.
1012
1026
* @param start the starting index of xs.
1013
1027
* @tparam B the type of the elements of the array.
1014
1028
* @return the number of elements written to the array
1015
1029
*
1016
1030
* @note Reuse: $consumesIterator
1017
1031
*/
1018
1032
@ deprecatedOverriding(" This should always forward to the 3-arg version of this method" , since = " 2.13.4" )
1019
- def copyToArray [B >: A ](xs : Array [B ], start : Int ): Int = copyToArray(xs, start, Int .MaxValue )
1033
+ def copyToArray [B >: A ](@ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ], start : Int ): Int =
1034
+ copyToArray(dest, start = start, n = Int .MaxValue )
1020
1035
1021
- /** Copy elements to an array, returning the number of elements written.
1036
+ /** Copies elements to an array and returns the number of elements written.
1022
1037
*
1023
- * Fills the given array `xs ` starting at index `start` with at most `len ` elements of this $coll.
1038
+ * Fills the given array `dest ` starting at index `start` with at most `n ` elements of this $coll.
1024
1039
*
1025
1040
* Copying will stop once either all the elements of this $coll have been copied,
1026
- * or the end of the array is reached, or `len ` elements have been copied.
1041
+ * or the end of the array is reached, or `n ` elements have been copied.
1027
1042
*
1028
- * @param xs the array to fill.
1043
+ * If `start` is less than zero, it is taken as zero.
1044
+ *
1045
+ * @param dest the array to fill.
1029
1046
* @param start the starting index of xs.
1030
- * @param len the maximal number of elements to copy.
1047
+ * @param n the maximal number of elements to copy.
1031
1048
* @tparam B the type of the elements of the array.
1032
1049
* @return the number of elements written to the array
1033
1050
*
1034
1051
* @note Reuse: $consumesIterator
1035
1052
*/
1036
- def copyToArray [B >: A ](xs : Array [B ], start : Int , len : Int ): Int = {
1053
+ def copyToArray [B >: A ](
1054
+ @ deprecatedName(" xs" , since= " 2.13.17" ) dest : Array [B ],
1055
+ start : Int ,
1056
+ @ deprecatedName(" len" , since= " 2.13.17" ) n : Int
1057
+ ): Int = {
1037
1058
val it = iterator
1038
1059
var i = start
1039
- val end = start + math.min(len, xs.length - start)
1060
+ val srclen = knownSize match {
1061
+ case - 1 => dest.length
1062
+ case k => k
1063
+ }
1064
+ val end = start + elemsToCopyToArray(srclen, dest.length, start, n)
1040
1065
while (i < end && it.hasNext) {
1041
- xs (i) = it.next()
1066
+ dest (i) = it.next()
1042
1067
i += 1
1043
1068
}
1044
1069
i - start
@@ -1244,7 +1269,7 @@ transparent trait IterableOnceOps[+A, +CC[_], +C] extends Any { this: IterableOn
1244
1269
* @param pf the partial function
1245
1270
* @return an option value containing pf applied to the first
1246
1271
* value for which it is defined, or `None` if none exists.
1247
- * @example `Seq("a", 1, 5L).collectFirst( { case x: Int => x*10 }) = Some(10)`
1272
+ * @example `Seq("a", 1, 5L).collectFirst { case x: Int => x*10 } = Some(10)`
1248
1273
*/
1249
1274
def collectFirst [B ](pf : PartialFunction [A , B ]^ ): Option [B ] = {
1250
1275
// Presumably the fastest way to get in and out of a partial function is for a sentinel function to return itself
0 commit comments