Skip to content

Commit 1b4a833

Browse files
committed
Use BoxesRunTime.equals for ArraySeq comparison
Add BoxesRunTime.arraysEquals, which uses BoxesRunTime.equals to compare array elements. Use BoxesRunTime.arraysEquals for ArraySeq.ofRef[_]#equals.
1 parent ce8db6b commit 1b4a833

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

library/src/scala/collection/immutable/ArraySeq.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import scala.collection.IterableOnce
99
import scala.annotation.unchecked.uncheckedVariance
1010
import scala.util.hashing.MurmurHash3
1111
import scala.reflect.ClassTag
12-
import scala.runtime.ScalaRunTime
12+
import scala.runtime.{BoxesRunTime, ScalaRunTime}
1313
import java.util.Arrays
1414

1515
/**
@@ -229,8 +229,11 @@ object ArraySeq extends StrictOptimizedClassTagSeqFactory[ArraySeq] { self =>
229229
@throws[ArrayIndexOutOfBoundsException]
230230
def apply(i: Int): T = unsafeArray(i)
231231
override def hashCode = MurmurHash3.arrayHash(unsafeArray, MurmurHash3.seqSeed)
232-
override def equals(that: Any) = that match {
233-
case that: ofRef[_] => Arrays.equals(unsafeArray.asInstanceOf[Array[AnyRef]], that.unsafeArray.asInstanceOf[Array[AnyRef]])
232+
override def equals(that: Any): Boolean = that match {
233+
case that: ofRef[_] =>
234+
BoxesRunTime.arraysEquals(
235+
this.unsafeArray.asInstanceOf[Array[AnyRef]],
236+
that.unsafeArray.asInstanceOf[Array[AnyRef]])
234237
case _ => super.equals(that)
235238
}
236239
}

library/src/scala/collection/mutable/ArraySeq.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package mutable
33

44
import java.io.{ObjectInputStream, ObjectOutputStream}
55

6-
import scala.runtime.ScalaRunTime
6+
import scala.runtime.{BoxesRunTime, ScalaRunTime}
77
import scala.reflect.ClassTag
88
import scala.util.hashing.MurmurHash3
99
import java.util.Arrays
@@ -144,7 +144,10 @@ object ArraySeq extends StrictOptimizedClassTagSeqFactory[ArraySeq] { self =>
144144
def update(index: Int, elem: T): Unit = { array(index) = elem }
145145
override def hashCode = MurmurHash3.arraySeqHash(array)
146146
override def equals(that: Any) = that match {
147-
case that: ofRef[_] => Arrays.equals(array.asInstanceOf[Array[AnyRef]], that.array.asInstanceOf[Array[AnyRef]])
147+
case that: ofRef[_] =>
148+
BoxesRunTime.arraysEquals(
149+
this.array.asInstanceOf[Array[AnyRef]],
150+
that.array.asInstanceOf[Array[AnyRef]])
148151
case _ => super.equals(that)
149152
}
150153
}

library/src/scala/runtime/BoxesRunTime.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ public static boolean equalsNumChar(java.lang.Number xn, java.lang.Character yc)
198198
}
199199
}
200200

201+
public static boolean arraysEquals(Object[] xs, Object[] ys) {
202+
if (xs == ys)
203+
return true;
204+
if (xs.length != ys.length)
205+
return false;
206+
207+
int len = xs.length;
208+
for (int i = 0; i < len; i++) {
209+
if (!equals(xs[i], ys[i]))
210+
return false;
211+
}
212+
return true;
213+
}
214+
201215
private static int unboxCharOrInt(Object arg1, int code) {
202216
if (code == CHAR)
203217
return ((java.lang.Character) arg1).charValue();

0 commit comments

Comments
 (0)