Skip to content

Commit 80ee988

Browse files
authored
Merge pull request scala#5250 from dimatkach/fast-array-slice-3
Override `.slice` in ArrayOps to use arraycopy.
2 parents 16fd63b + a84c7b9 commit 80ee988

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/library/scala/collection/mutable/ArrayOps.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ sealed trait ArrayOps[T] extends Any with ArrayLike[T, Array[T]] with CustomPara
4141
if (l > 0) Array.copy(repr, 0, xs, start, l)
4242
}
4343

44+
override def slice(from: Int, until: Int): Array[T] = {
45+
val lo = math.max(from, 0)
46+
val hi = math.min(math.max(until, 0), repr.length)
47+
val size = math.max(hi - lo, 0)
48+
val result = java.lang.reflect.Array.newInstance(elementClass, size)
49+
if (size > 0) {
50+
Array.copy(repr, lo, result, 0, size)
51+
}
52+
result.asInstanceOf[Array[T]]
53+
}
54+
4455
override def toArray[U >: T : ClassTag]: Array[U] = {
4556
val thatElementClass = implicitly[ClassTag[U]].runtimeClass
4657
if (elementClass eq thatElementClass)

0 commit comments

Comments
 (0)