/* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2002-2009, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ // $Id: BoxedArray.scala 17000 2009-01-29 13:05:53Z odersky $ package scalax.runtime import Predef._ import collection.mutable.{Vector, ArrayBuffer} import collection.generic._ /** *
A class representing Array[T]
xs
with the elements of
* this sequence starting at position start
.
*
* @param xs the array to fill.
* @param start starting index.
* @pre the array must be large enough to hold all elements.
*/
override def copyToArray[B](xs: Array[B], start: Int, len: Int): Unit =
copyTo(0, xs, start, len)
final def deepToString() = deepMkString(stringPrefix + "(", ", ", ")")
final def deepMkString(start: String, sep: String, end: String): String = {
def _deepToString(x: Any) = x match {
case a: AnyRef if ScalaRunTime.isArray(a) =>
ScalaRunTime.boxArray(a).deepMkString(start, sep, end)
case _ =>
ScalaRunTime.stringOf(x)
}
val buf = new StringBuilder()
buf.append(start)
val elems = elements
if (elems.hasNext) buf.append(_deepToString(elems.next))
while (elems.hasNext) {
buf.append(sep); buf.append(_deepToString(elems.next))
}
buf.append(end)
buf.toString
}
final def deepMkString(sep: String): String = this.deepMkString("", sep, "")
final def deepEquals(that: Any): Boolean = {
def _deepEquals(x1: Any, x2: Any) = (x1, x2) match {
case (a1: BoxedArray[_], a2: BoxedArray[_]) =>
_sameElements(a1, a2)
case (a1: AnyRef, a2: AnyRef)
if ScalaRunTime.isArray(a1) && ScalaRunTime.isArray(a2) =>
_sameElements(ScalaRunTime.boxArray(a1), ScalaRunTime.boxArray(a2))
case _ =>
x1.equals(x2)
}
def _sameElements(a1: BoxedArray[_], a2: BoxedArray[_]): Boolean = {
val it1 = a1.elements
val it2 = a2.elements
var res = true
while (res && it1.hasNext && it2.hasNext)
res = _deepEquals(it1.next, it2.next)
!it1.hasNext && !it2.hasNext && res
}
that match {
case a: BoxedArray[_] =>
_sameElements(this, a)
case a: AnyRef if ScalaRunTime.isArray(a) =>
_sameElements(this, ScalaRunTime.boxArray(a))
case _ =>
false
}
}
override final def stringPrefix: String = "Array"
protected def countAndMemo(p: A => Boolean): (Int, Array[Boolean]) = {
val len = length
val memo = new Array[Boolean](len)
var count = 0
var i = 0
while (i < len) {
if (p(this(i))) { memo(i) = true; count += 1 }
i += 1
}
(count, memo)
}
}