diff options
author | stenman <stenman@epfl.ch> | 2003-07-09 10:49:03 +0000 |
---|---|---|
committer | stenman <stenman@epfl.ch> | 2003-07-09 10:49:03 +0000 |
commit | a9d2d118928ceab8f096c57e695f6ec3a1fcd7c1 (patch) | |
tree | f9f9fdcfbf98b1a3545976eda6484da6cf64de27 /sources | |
parent | dd34727fc756c174a3fd701b63dd89452f881859 (diff) | |
download | scala-a9d2d118928ceab8f096c57e695f6ec3a1fcd7c1.tar.gz scala-a9d2d118928ceab8f096c57e695f6ec3a1fcd7c1.tar.bz2 scala-a9d2d118928ceab8f096c57e695f6ec3a1fcd7c1.zip |
Functional Queues, take 2
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/collection/immutable/Queue.scala | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/sources/scala/collection/immutable/Queue.scala b/sources/scala/collection/immutable/Queue.scala index 819afc589c..e1338052d8 100644 --- a/sources/scala/collection/immutable/Queue.scala +++ b/sources/scala/collection/immutable/Queue.scala @@ -21,13 +21,17 @@ object Queue { } class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { - /* val in:List[A]= Nil; - val out:List[A]= Nil; */ def at(n: Int): A = if (n < out.length) out.at(n) else (in.reverse).at(n - out.length); + /** Returns the <code>n</code>-th element of this queue. + * The first element is at position 0. + * @param n index of the element to return + * @return the element at position <code>n</code> in this list. + * @throws java.lang.RuntimeException if the list is too short. + */ def apply(n: Int): A = at(n); /** Returns the elements in the list as an iterator @@ -40,6 +44,8 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { */ def isEmpty: Boolean = (in.isEmpty && out.isEmpty); + /** Returns the lenegth of the queue. + */ def length = in.length + out.length; /** Creates a new queue with element added at the end @@ -47,7 +53,7 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { * * @param elem the element to insert */ - def +(elem: A):Queue[A] = { + def +[B >: A](elem: B):Queue[B] = { new Queue(elem::in,out); } @@ -59,8 +65,8 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { * * @param iter an iterable object */ - def +(iter: Iterable[A]) = { - var q:List[A] = in; + def +[B >: A](iter: Iterable[B]) = { + var q:List[B] = in; iter.elements.foreach(e => q = (e::q)); new Queue(q,out); } @@ -69,14 +75,14 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { * * @param elems the elements to add. */ - def enqueue(elems: A*): Queue[A] = (this + elems); + def enqueue [B >: A](elems: B*): Queue[B] = (this + elems); /** Returns a tuple with the first element in the queue, * and a new queu with this element removed. * * @returns the first element of the queue. */ - def dequeue: Tuple2[A,Queue[A]] = { + def dequeue: Pair[A,Queue[A]] = { var newOut:List[A]=Nil; var newIn:List[A]=Nil; if (out.isEmpty) { @@ -89,7 +95,7 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { if (newOut.isEmpty) error("queue empty"); else { - Tuple2(newOut.head,new Queue(newIn,newOut.tail)); + Pair(newOut.head,new Queue(newIn,newOut.tail)); } } @@ -106,6 +112,27 @@ class Queue[+A](in:List[A],out:List[A]) extends Seq[A] { } else out.head; } + + /** Returns a string representation of this queue. The resulting string + * begins with the string <code>start</code> and is finished by the string + * <code>end</code>. Inside, the string representations of elements (w.r.t. + * the method <code>toString()</code>) are separated by the string + * <code>sep</code>. + * <p> + * Ex: <br> + * <code>Queue(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"</code> + * @param start starting string. + * @param sep separator string. + * @param end ending string. + * @return a string representation of this list. + */ + def mkString(start: String, sep: String, end: String): String = + (out:::(in.reverse)).mkString(start,sep,end); + + /** Returns a string representation of this queue. + */ + override def toString() = (out:::(in.reverse)).toString(); + } |