package scala.collection
package mutable
import generic._
import immutable.{List, Nil, ::}
@SerialVersionUID(3419063961353022661L)
final class ListBuffer[A]
extends Buffer[A]
with GenericTraversableTemplate[A, ListBuffer]
with BufferLike[A, ListBuffer[A]]
with Builder[A, List[A]]
with SeqForwarder[A]
with Serializable
{
override def companion: GenericCompanion[ListBuffer] = ListBuffer
import scala.collection.Traversable
private var start: List[A] = Nil
private var last0: ::[A] = _
private var exported: Boolean = false
private var len = 0
protected def underlying: immutable.Seq[A] = start
override def length = len
override def apply(n: Int): A =
if (n < 0 || n >= len) throw new IndexOutOfBoundsException(n.toString())
else super.apply(n)
def update(n: Int, x: A) {
try {
if (exported) copy()
if (n == 0) {
val newElem = new :: (x, start.tail);
if (last0 eq start) {
last0 = newElem
}
start = newElem
} else {
var cursor = start
var i = 1
while (i < n) {
cursor = cursor.tail
i += 1
}
val newElem = new :: (x, cursor.tail.tail)
if (last0 eq cursor.tail) {
last0 = newElem
}
cursor.asInstanceOf[::[A]].tl = newElem
}
} catch {
case ex: Exception => throw new IndexOutOfBoundsException(n.toString())
}
}
def += (x: A): this.type = {
if (exported) copy()
if (start.isEmpty) {
last0 = new :: (x, Nil)
start = last0
} else {
val last1 = last0
last0 = new :: (x, Nil)
last1.tl = last0
}
len += 1
this
}
override def ++=(xs: TraversableOnce[A]): this.type =
if (xs eq this) ++= (this take size) else super.++=(xs)
override def ++=:(xs: TraversableOnce[A]): this.type =
if (xs eq this) ++=: (this take size) else super.++=:(xs)
def clear() {
start = Nil
exported = false
len = 0
}
def +=: (x: A): this.type = {
if (exported) copy()
val newElem = new :: (x, start)
if (start.isEmpty) last0 = newElem
start = newElem
len += 1
this
}
def insertAll(n: Int, seq: Traversable[A]) {
try {
if (exported) copy()
var elems = seq.toList.reverse
len += elems.length
if (n == 0) {
while (!elems.isEmpty) {
val newElem = new :: (elems.head, start)
if (start.isEmpty) last0 = newElem
start = newElem
elems = elems.tail
}
} else {
var cursor = start
var i = 1
while (i < n) {
cursor = cursor.tail
i += 1
}
while (!elems.isEmpty) {
val newElem = new :: (elems.head, cursor.tail)
if (cursor.tail.isEmpty) last0 = newElem
cursor.asInstanceOf[::[A]].tl = newElem
elems = elems.tail
}
}
} catch {
case ex: Exception =>
throw new IndexOutOfBoundsException(n.toString())
}
}
override def remove(n: Int, count: Int) {
if (exported) copy()
val n1 = n max 0
val count1 = count min (len - n1)
var old = start.head
if (n1 == 0) {
var c = count1
while (c > 0) {
start = start.tail
c -= 1
}
} else {
var cursor = start
var i = 1
while (i < n1) {
cursor = cursor.tail
i += 1
}
var c = count1
while (c > 0) {
if (last0 eq cursor.tail) last0 = cursor.asInstanceOf[::[A]]
cursor.asInstanceOf[::[A]].tl = cursor.tail.tail
c -= 1
}
}
len -= count1
}
def result: List[A] = toList
override def toList: List[A] = {
exported = !start.isEmpty
start
}
def prependToList(xs: List[A]): List[A] = {
if (start.isEmpty) xs
else {
if (exported) copy()
last0.tl = xs
toList
}
}
def remove(n: Int): A = {
if (n < 0 || n >= len) throw new IndexOutOfBoundsException(n.toString())
if (exported) copy()
var old = start.head
if (n == 0) {
start = start.tail
} else {
var cursor = start
var i = 1
while (i < n) {
cursor = cursor.tail
i += 1
}
old = cursor.tail.head
if (last0 eq cursor.tail) last0 = cursor.asInstanceOf[::[A]]
cursor.asInstanceOf[::[A]].tl = cursor.tail.tail
}
len -= 1
old
}
override def -= (elem: A): this.type = {
if (exported) copy()
if (start.isEmpty) {}
else if (start.head == elem) {
start = start.tail
len -= 1
} else {
var cursor = start
while (!cursor.tail.isEmpty && cursor.tail.head != elem) {
cursor = cursor.tail
}
if (!cursor.tail.isEmpty) {
val z = cursor.asInstanceOf[::[A]]
if (z.tl == last0)
last0 = z
z.tl = cursor.tail.tail
len -= 1
}
}
this
}
override def iterator: Iterator[A] = new Iterator[A] {
var cursor: List[A] = null
var delivered = 0
def hasNext: Boolean = delivered < ListBuffer.this.length
def next(): A =
if (!hasNext)
throw new NoSuchElementException("next on empty Iterator")
else {
if (cursor eq null) cursor = start
else cursor = cursor.tail
delivered += 1
cursor.head
}
}
override def readOnly: List[A] = start
private def copy() {
var cursor = start
val limit = last0.tail
clear
while (cursor ne limit) {
this += cursor.head
cursor = cursor.tail
}
}
override def equals(that: Any): Boolean = that match {
case that: ListBuffer[_] => this.readOnly equals that.readOnly
case _ => super.equals(that)
}
override def clone(): ListBuffer[A] = (new ListBuffer[A]) ++= this
override def stringPrefix: String = "ListBuffer"
}
object ListBuffer extends SeqFactory[ListBuffer] {
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, ListBuffer[A]] = new GenericCanBuildFrom[A]
def newBuilder[A]: Builder[A, ListBuffer[A]] = new GrowingBuilder(new ListBuffer[A])
}
<iframe src="https://xuwei-k.github.io/scala-library-sxr/scala-library-2.9.1/scala/collection/mutable/ListBuffer.scala.html" width="1280" height="720" frameborder="0"> </iframe>