Skip to content

Commit 21f3c1d

Browse files
committed
Add copyToArray to IArray
1 parent b9b1f2a commit 21f3c1d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

library/src/scala/IArray.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ object opaques:
5252
// but we can use `exists` instead, which is how `ArrayOps#contains` itself is implemented:
5353
genericArrayOps(arr).exists(_ == elem)
5454

55+
/** Copy elements of this array to another array. */
56+
extension [T, U >: T](arr: IArray[T]) def copyToArray(xs: Array[U]): Int =
57+
genericArrayOps(arr).copyToArray(xs)
58+
59+
/** Copy elements of this array to another array. */
60+
extension [T, U >: T](arr: IArray[T]) def copyToArray(xs: Array[U], start: Int): Int =
61+
genericArrayOps(arr).copyToArray(xs, start)
62+
63+
/** Copy elements of this array to another array. */
64+
extension [T, U >: T](arr: IArray[T]) def copyToArray(xs: Array[U], start: Int, len: Int): Int =
65+
genericArrayOps(arr).copyToArray(xs, start, len)
66+
5567
/** Counts the number of elements in this array which satisfy a predicate */
5668
extension [T](arr: IArray[T]) def count(p: T => Boolean): Int =
5769
genericArrayOps(arr).count(p)

tests/run/iarrays.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@ object Test extends App {
7070
val cs: Array[Double] = bs.asInstanceOf[Array[Double]]
7171
cs(1) = 3.0
7272
assert(bs(1) == 3.0)
73-
}
73+
74+
// Check copyToArray
75+
val ds: IArray[Int] = IArray(1, 2, 3)
76+
val es: Array[Int] = new Array[Int](10)
77+
ds.copyToArray(es, 5, 2)
78+
assert(es.toList == List(0, 0, 0, 0, 0, 1, 2, 0, 0, 0))
79+
val fs: Array[Any] = new Array[Any](10)
80+
ds.copyToArray(fs, 5, 2)
81+
assert(fs.toList == List(null, null, null, null, null, 1, 2, null, null, null))
82+
}

0 commit comments

Comments
 (0)