summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstepancheg <stepancheg@epfl.ch>2008-06-20 09:26:36 +0000
committerstepancheg <stepancheg@epfl.ch>2008-06-20 09:26:36 +0000
commit84f24cad144b2582b2e7e4b6b0359d5ff58fbeda (patch)
tree4c8f755cc841de37ecc4549279bddbccb6740bab
parent7f78d4634789e149b86691f6af8a911b7206beec (diff)
downloadscala-84f24cad144b2582b2e7e4b6b0359d5ff58fbeda.tar.gz
scala-84f24cad144b2582b2e7e4b6b0359d5ff58fbeda.tar.bz2
scala-84f24cad144b2582b2e7e4b6b0359d5ff58fbeda.zip
speed up ArrayBuffer by replacing BoxedAnyArray...
speed up ArrayBuffer by replacing BoxedAnyArray with raw JVM array
-rw-r--r--src/library/scala/collection/mutable/ArrayBuffer.scala14
-rw-r--r--src/library/scala/collection/mutable/PriorityQueue.scala20
-rw-r--r--src/library/scala/collection/mutable/ResizableArray.scala10
-rw-r--r--test/files/run/unittest_collection.scala8
4 files changed, 30 insertions, 22 deletions
diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala
index 6ec54577c3..eb3f8a07ce 100644
--- a/src/library/scala/collection/mutable/ArrayBuffer.scala
+++ b/src/library/scala/collection/mutable/ArrayBuffer.scala
@@ -31,7 +31,7 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
*/
def +=(elem: A) {
ensureSize(size0 + 1)
- array(size0) = elem
+ array(size0) = elem.asInstanceOf[AnyRef]
size0 += 1
}
@@ -73,7 +73,7 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
def +:(elem: A): Buffer[A] = {
ensureSize(size0 + 1)
copy(0, 1, size0)
- array(0) = elem
+ array(0) = elem.asInstanceOf[AnyRef]
size0 += 1
this
}
@@ -88,7 +88,7 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
if ((i < 0) || (i >= size0))
throw new IndexOutOfBoundsException(i.toString())
else
- array(i)
+ array(i).asInstanceOf[A]
}
/** Prepends a number of elements provided by an iterable object
@@ -115,7 +115,7 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
val len = xs.length
ensureSize(size0 + len)
copy(n, n + len, size0 - n)
- xs.copyToArray(array, n)
+ xs.copyToArray(array.asInstanceOf[Array[Any]], n)
size0 += len
}
@@ -130,8 +130,8 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
if ((n < 0) || (n >= size0))
throw new IndexOutOfBoundsException("cannot update element at " + n)
else {
- val res = array(n)
- array(n) = newelem
+ val res = array(n).asInstanceOf[A]
+ array(n) = newelem.asInstanceOf[AnyRef]
res
}
}
@@ -146,7 +146,7 @@ class ArrayBuffer[A] extends RandomAccessSeq.Mutable[A] with Buffer[A] with Resi
def remove(n: Int): A = {
if ((n < 0) || (n >= size0))
throw new IndexOutOfBoundsException("cannot remove element at " + n);
- val res = array(n)
+ val res = array(n).asInstanceOf[A]
copy(n + 1, n, size0 - n - 1)
size0 -= 1
res
diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala
index de615469d8..6b838b0fc4 100644
--- a/src/library/scala/collection/mutable/PriorityQueue.scala
+++ b/src/library/scala/collection/mutable/PriorityQueue.scala
@@ -26,22 +26,22 @@ package scala.collection.mutable
class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCollection {
size0 = size0 + 1 // we do not use array(0)
- protected def fixUp(as: Array[A], m: Int): Unit = {
+ protected def fixUp(as: Array[AnyRef], m: Int): Unit = {
var k: Int = m
- while ((k > 1) && (as(k / 2) < as(k))) {
+ while ((k > 1) && (as(k / 2).asInstanceOf[A] < as(k).asInstanceOf[A])) {
swap(k, k / 2)
k = k / 2
}
}
- protected def fixDown(as: Array[A], m: Int, n: Int): Unit = {
+ protected def fixDown(as: Array[AnyRef], m: Int, n: Int): Unit = {
var k: Int = m
var loop: Boolean = true
while (loop && (n >= 2 * k)) {
var j = 2 * k
- if ((j < n) && (as(j) < as(j + 1)))
+ if ((j < n) && (as(j).asInstanceOf[A] < as(j + 1).asInstanceOf[A]))
j = j + 1;
- if (!(as(k) < as(j)))
+ if (!(as(k).asInstanceOf[A] < as(j).asInstanceOf[A]))
loop = false
else {
val h = as(k)
@@ -64,7 +64,7 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol
*/
def +=(elem: A): Unit = {
ensureSize(size0+1)
- array(size0) = elem
+ array(size0) = elem.asInstanceOf[AnyRef]
fixUp(array, size0)
size0 = size0 + 1
}
@@ -114,7 +114,7 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol
size0 = size0 - 1
swap(1, size0)
fixDown(array, 1, size0 - 1)
- array(size0)
+ array(size0).asInstanceOf[A]
} else
throw new NoSuchElementException("no element to remove from heap")
@@ -123,7 +123,7 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol
*
* @return the element with the highest priority.
*/
- def max: A = if (size0 > 1) array(1) else throw new NoSuchElementException("queue is empty")
+ def max: A = if (size0 > 1) array(1).asInstanceOf[A] else throw new NoSuchElementException("queue is empty")
/** Removes all elements from the queue. After this operation is completed,
* the queue will be empty.
@@ -136,12 +136,12 @@ class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCol
* @return an iterator over all elements sorted in descending order.
*/
override def elements: Iterator[A] = new Iterator[A] {
- val as: Array[A] = new Array[A](size0)
+ val as: Array[AnyRef] = new Array[AnyRef](size0)
Array.copy(array, 0, as, 0, size0)
var i = size0 - 1
def hasNext: Boolean = i > 0
def next(): A = {
- val res = as(1)
+ val res = as(1).asInstanceOf[A]
as(1) = as(i)
i = i - 1
fixDown(as, 1, i)
diff --git a/src/library/scala/collection/mutable/ResizableArray.scala b/src/library/scala/collection/mutable/ResizableArray.scala
index 355fd0152b..2e2e51ecb9 100644
--- a/src/library/scala/collection/mutable/ResizableArray.scala
+++ b/src/library/scala/collection/mutable/ResizableArray.scala
@@ -23,7 +23,7 @@ import Predef._
trait ResizableArray[A] extends RandomAccessSeq[A] {
protected def initialSize: Int = 16
- protected var array: Array[A] = new Array[A](initialSize)
+ protected var array: Array[AnyRef] = new Array[AnyRef](initialSize)
private var size1: Int = 0
protected def size0: Int = size1
protected def size0_=(sz: Int) { size1 = sz }
@@ -35,7 +35,7 @@ trait ResizableArray[A] extends RandomAccessSeq[A] {
*/
def length: Int = size0
- def apply(i: Int) = array(i)
+ def apply(i: Int) = array(i).asInstanceOf[A]
/** remove elements of this array at indices after <code>sz</code>
*/
@@ -58,7 +58,7 @@ trait ResizableArray[A] extends RandomAccessSeq[A] {
* @param The buffer to which elements are copied
*/
override def copyToBuffer[B >: A](dest: Buffer[B]) {
- dest.++=(array.asInstanceOf[Array[B]], 0, size0)
+ dest.++=(runtime.ScalaRunTime.boxArray(array).asInstanceOf[Array[B]], 0, size0)
}
/** Returns a new iterator over all elements of this resizable array.
@@ -66,7 +66,7 @@ trait ResizableArray[A] extends RandomAccessSeq[A] {
override def elements: Iterator[A] = new Iterator[A] {
var i = 0
def hasNext: Boolean = i < size0
- def next(): A = { i = i + 1; array(i - 1) }
+ def next(): A = { i = i + 1; array(i - 1).asInstanceOf[A] }
}
//##########################################################################
@@ -77,7 +77,7 @@ trait ResizableArray[A] extends RandomAccessSeq[A] {
var newsize = array.length * 2
while (n > newsize)
newsize = newsize * 2
- val newar: Array[A] = new Array(newsize)
+ val newar: Array[AnyRef] = new Array(newsize)
Array.copy(array, 0, newar, 0, size0)
array = newar
}
diff --git a/test/files/run/unittest_collection.scala b/test/files/run/unittest_collection.scala
index 6d2a34cfb9..5d7ab97425 100644
--- a/test/files/run/unittest_collection.scala
+++ b/test/files/run/unittest_collection.scala
@@ -38,6 +38,14 @@ object Test {
// clear
x.clear
assertEquals("length F ", x.length, 0)
+
+ // copyToBuffer
+ x += "a"
+ x += "b"
+ val dest = new ArrayBuffer[String]
+ x copyToBuffer dest
+ assertEquals("dest", List("a", "b"), dest.toList)
+ assertEquals("source", List("a", "b"), x.toList)
}
}