package scala.collection
package mutable
import generic._
trait ResizableArray[A] extends IndexedSeq[A]
with GenericTraversableTemplate[A, ResizableArray]
with IndexedSeqOptimized[A, ResizableArray[A]] {
override def companion: GenericCompanion[ResizableArray] = ResizableArray
protected def initialSize: Int = 16
protected var array: Array[AnyRef] = new Array[AnyRef](math.max(initialSize, 1))
protected var size0: Int = 0
def length: Int = size0
def apply(idx: Int) = {
if (idx >= size0) throw new IndexOutOfBoundsException(idx.toString)
array(idx).asInstanceOf[A]
}
def update(idx: Int, elem: A) {
if (idx >= size0) throw new IndexOutOfBoundsException(idx.toString)
array(idx) = elem.asInstanceOf[AnyRef]
}
override def foreach[U](f: A => U) {
var i = 0
val top = size
while (i < top) {
f(array(i).asInstanceOf[A])
i += 1
}
}
override def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) {
val len1 = len min (xs.length - start) min length
Array.copy(array, 0, xs, start, len1)
}
def reduceToSize(sz: Int) {
require(sz <= size0)
while (size0 > sz) {
size0 -= 1
array(size0) = null
}
}
protected def ensureSize(n: Int) {
if (n > array.length) {
var newsize = array.length * 2
while (n > newsize)
newsize = newsize * 2
val newar: Array[AnyRef] = new Array(newsize)
compat.Platform.arraycopy(array, 0, newar, 0, size0)
array = newar
}
}
protected def swap(a: Int, b: Int) {
val h = array(a)
array(a) = array(b)
array(b) = h
}
protected def copy(m: Int, n: Int, len: Int) {
compat.Platform.arraycopy(array, m, array, n, len)
}
}
object ResizableArray extends SeqFactory[ResizableArray] {
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, ResizableArray[A]] = new GenericCanBuildFrom[A]
def newBuilder[A]: Builder[A, ResizableArray[A]] = new ArrayBuffer[A]
}
<iframe src="https://xuwei-k.github.io/scala-library-sxr/scala-library-2.9.1/scala/collection/mutable/ResizableArray.scala.html" width="1280" height="720" frameborder="0"> </iframe>