|
Scala example source code file (MutableList.scala)
The Scala MutableList.scala source code/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.collection
package mutable
import generic._
import immutable.{List, Nil}
// !!! todo: convert to LinkedListBuffer?
/**
* This class is used internally to represent mutable lists. It is the
* basis for the implementation of the class `Queue`.
*
* @author Matthias Zenger
* @author Martin Odersky
* @version 2.8
* @since 1
*/
@SerialVersionUID(5938451523372603072L)
class MutableList[A]
extends LinearSeq[A]
with LinearSeqOptimized[A, MutableList[A]]
with GenericTraversableTemplate[A, MutableList]
with Builder[A, MutableList[A]]
with Serializable
{
override def companion: GenericCompanion[MutableList] = MutableList
override protected[this] def newBuilder: Builder[A, MutableList[A]] = new MutableList[A]
protected var first0: LinkedList[A] = new LinkedList[A]
protected var last0: LinkedList[A] = first0
protected var len: Int = 0
def toQueue = new Queue(first0, last0, len)
/** Is the list empty?
*/
override def isEmpty = len == 0
/** Returns the first element in this list
*/
override def head: A = if (nonEmpty) first0.head else throw new NoSuchElementException
/** Returns the rest of this list
*/
override def tail: MutableList[A] = {
require(nonEmpty, "tail of empty list")
val tl = new MutableList[A]
tl.first0 = first0.tail
tl.last0 = last0
tl.len = len - 1
tl
}
/** Prepends a single element to this list. This operation takes constant
* time.
* @param elem the element to prepend.
* @return this $coll.
*/
def +=: (elem: A): this.type = { prependElem(elem); this }
/** Returns the length of this list.
*/
override def length: Int = len
/** Returns the <code>nth element of this list.
* @throws IndexOutofBoundsException if index does not exist.
*/
override def apply(n: Int): A = first0.apply(n)
/** Updates the <code>nth element of this list to a new value.
* @throws IndexOutofBoundsException if index does not exist.
*/
def update(n: Int, x: A): Unit = first0.update(n, x)
/** Returns the <code>nth element of this list or
Other Scala examples (source code examples)Here is a short list of links related to this Scala MutableList.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.