/* __ *\ * * ________ ___//___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL * * * * __\ \/__/__ |//__/__ | http://scala-lang.org/* * */____/\___/_/|_/____/_/| | **
** |/ **
\* */Package Scala Package Collection Package mutable import generic._ Import scala.collecti On.immutable. {List, Nil} import scala.collection.Iterator import scala.annotation.migration/** Factory object for the ' mutable.
Stack ' class. * * $factoryInfo * @define coll mutable stack * @define Coll ' mutable. Stack ' */object stack extends Seqfactory[stack] {class Stackbuilder[a] extends Builder[a, Stack[a]] {val Lbuff = New Listbuffer[a] def + = (elem:a) = { Lbuff + = Elem; This} def clear () = Lbuff.clear () def result = new Stack (Lbuff.result)} implicit def Canbuildfrom[a]: Canbu Ildfrom[coll, A, stack[a]] = Reusablecbf.asinstanceof[genericcanbuildfrom[a] [def Newbuilder[a]: Builder[A, Stack[A]] = New Stackbuilder[a] val empty:stack[nothing] = new Stack (Nil)}/** a stack implements a data structure which allows
to store and retrieve * objects in a last-in-first-out (LIFO) fashion.
* * @tparam A type of the elements contained in this stack. * * @author Matthias Zenger * @author Martin Odersky * @version 2.8 * @since 1 * @see [[Http://docs.scala-la Ng.org/overviews/collections/concrete-mutable-collection-classes.html#stacks "Scala ' s Collection Library Overview"
]] * section on ' Stacks ' for more information. * @define Coll ' stack ' * @define Coll stack * @define orderdependent * @define orderdependentfold * @define Mayno Tterminateinf * @define Willnotterminateinf * * Class Stack[a] PrivatE (var elems:list[a]) extends Abstractseq[a] with Seq[a] with Seqlike[a, Stack[a]] with GENERICTRAVERSABLETEMPLA
Te[a, Stack] with Cloneable[stack[a]] with Serializable {def this () = this (Nil) override def companion = Stack
/** Checks If the stack is empty. * * @return True, iff there is no element on the stack */override def Isempty:boolean = Elems.isempty/** Th E number of elements in the stack */override def length = elems.length/** Retrieve ' n '-th element from Stack, where
Top of stack has index ' 0 '.
* * This is a linear time operation. * * @param index The index of the element to return * @return The element at the specified index *
@throws Indexoutofboundsexception If the index is out of bounds */override def apply (index:int) = Elems (index)
/** Replace element at index ' n ' with the new element ' Newelem '.
* * This is a linear time operation. * * @param n the index of the element to replace.
* @param newelem the new element. * @throws Indexoutofboundsexception If the index is not valid */def update (n:int, newelem:a) = if (N < 0 || n >= length) throw new Indexoutofboundsexception (n.tostring) Else Elems = Elems.take (n) + + (Newelem:: Elems.drop (n
+1))/** Push an element on the stack.
* * @param elem the element to push on the stack.
* @return the stack with the new element on top. */def push (elem:a): This.type = {Elems = Elem:: elems; this}/** push, or more elements onto the stack.
The last element * of the sequence is on top of the new stack.
* * @param elems the element sequence.
* @return the stack with the new elements on top. */def push (Elem1:a, Elem2:a, elems:a*): This.type = This.push (elem1). push (ELEM2). Pushall (Elems)/** push All Elements in the given Traversable object onto the stack. The * last element in the Traversable object'll is on top of the new stack.
* * @param xs the traversable object.
* @return the stack with the new elements on top. */def Pushall (Xs:traversableonce[a]): This.type = {xs foreach push; this}/** Returns the top element of the STA Ck. This method won't remove * The element from the stack.
An error is signaled if there are no * element on the stack. * * @throws java.util.NoSuchElementException * @return The top element */def TOP:A = Elems.head/**
Removes the top element from the stack. * * @throws java.util.NoSuchElementException * @return The top element */def pop (): A = {val res = Elem S.head Elems = elems.tail Res}/** * Removes all elements from the stack.
After this operation completed, * the stack would be empty. */def Clear (): Unit = Elems = Nil/** Returns An iterator over all elements on the stack. This iterator * are stable with respect to state changes in the StacK object;
i.e. * Such changes is not being reflected in the iterator.
The iterator * issues elements in the reversed order they were inserted into the * stack (LIFO order).
* * @return an iterator through all stack elements. */@migration ("' iterator ' traverses in FIFO order.", "2.8.0") override def Iterator:iterator[a] = elems.iterator/
* * Creates a list of all stacks elements in LIFO order.
* * @return the created list. */@migration ("' ToList ' traverses in FIFO order.", "2.8.0") override def tolist:list[a] = Elems @migration ("' Forea Ch ' traverses in FIFO order. "," 2.8.0 ") override Def Foreach[u] (f:a = + U): Unit = Super.foreach (f)/** this Metho
D clones the stack.
* * @return a stack with the same elements.
*/Override Def clone (): stack[a] = new Stack[a] (Elems)}
Some conclusions:
The 1stack element is saved with the list, and the list is immutable, and each new deletion modifies the element to produce a new
The list object. Why not use Listbuffer implementation to improve performance.
The main construction method for 2Stack is private, and only one empty stack can be constructed with new, but
The inherited Genericcompanion's apply method is then used with the stack's associated object with a stack (...) The way it is constructed.
def Apply[a] (elems:a*): cc[a] = {
if (elems.isempty) Empty[a]
else {
Val B = Newbuilder[a]
b ++= Elems
B.result ()
}
}
There is newbuilder in the associated object of the stack, and he uses listbuffer to construct it and then to list.
The 3push method returns the This.type type, indicating that he can chain-write programs
The 4foreach method is implemented by calling iterator, where the template method is used
The 5clone method is a shallow copy, internally referencing the same data