summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstenman <stenman@epfl.ch>2003-08-15 12:29:05 +0000
committerstenman <stenman@epfl.ch>2003-08-15 12:29:05 +0000
commitcce804c34f58e904cdc66889ba9990b3edd827f5 (patch)
tree483cb168876743831320833816557ea6e618fedf
parentc99331efe76cc9a45246a7a9ec3955a4e73f88bf (diff)
downloadscala-cce804c34f58e904cdc66889ba9990b3edd827f5.tar.gz
scala-cce804c34f58e904cdc66889ba9990b3edd827f5.tar.bz2
scala-cce804c34f58e904cdc66889ba9990b3edd827f5.zip
Hiding in/out
-rw-r--r--sources/scala/collection/immutable/Queue.scala68
-rw-r--r--test/files/run/iq.scala5
2 files changed, 45 insertions, 28 deletions
diff --git a/sources/scala/collection/immutable/Queue.scala b/sources/scala/collection/immutable/Queue.scala
index 2e6dce6367..8a525ab19d 100644
--- a/sources/scala/collection/immutable/Queue.scala
+++ b/sources/scala/collection/immutable/Queue.scala
@@ -17,19 +17,32 @@ package scala.collection.immutable;
*/
object Queue {
- val Empty:Queue[All] = new Queue(Nil, Nil);
+ val Empty:Queue[All] = new Queue();
}
-class Queue[+A](in: List[A], out: List[A]) extends Seq[A]
- with StructuralEquality[Queue[A]] {
+class Queue[+A](elem: A*)extends Seq[A]
+ with StructuralEquality[Queue[A]] {
+ protected val in:List[A] = Nil;
+ protected val out:List[A] = itToList(elem.elements);
+
+ protected def itToList[B >: A](elems:Iterator[B]): List[B] =
+ if (elems.hasNext) { val hd = elems.next;
+ hd::itToList(elems)}
+ else Nil;
+
+ protected def mkQueue[A](i:List[A], o:List[A]):Queue[A] = {
+ new Queue[A](){
+ override protected val in = i;
+ override protected val out = o
+ };
+ }
- /** 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.
- */
+ /** 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 =
if (n < out.length) out.apply(n)
else in.reverse.apply(n - out.length);
@@ -44,7 +57,7 @@ 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.
+ /** Returns the length of the queue.
*/
def length = in.length + out.length;
@@ -53,9 +66,10 @@ class Queue[+A](in: List[A], out: List[A]) extends Seq[A]
*
* @param elem the element to insert
*/
- def +[B >: A](elem: B):Queue[B] = new Queue(elem::in,out);
- /** Returns a new que with all all elements provided by
+ def +[B >: A](elem: B) = mkQueue(elem::in,out);
+
+ /** Returns a new queue with all all elements provided by
* an <code>Iterable</code> object added at the end of
* the queue.
* The elements are prepended in the order they
@@ -66,34 +80,34 @@ class Queue[+A](in: List[A], out: List[A]) extends Seq[A]
def +[B >: A](iter: Iterable[B]) = {
var q:List[B] = in;
iter.elements.foreach(e => q = (e::q));
- new Queue(q,out);
+ mkQueue(q,out);
}
/** Returns a new queue with all elements added.
*
* @param elems the elements to add.
*/
- def enqueue [B >: A](elems: B*): Queue[B] = (this + elems);
+ def enqueue [B >: A](elems: B*) = (this + elems);
/** Returns a tuple with the first element in the queue,
- * and a new queu with this element removed.
+ * and a new queue with this element removed.
*
* @returns the first element of the queue.
*/
- def dequeue: Pair[A,Queue[A]] = {
+ def dequeue:Pair[A,Queue[A]] = {
var newOut:List[A]=Nil;
var newIn:List[A]=Nil;
- if (out.isEmpty) {
- newOut = in.reverse;
- newIn = Nil;
- } else {
- newOut = out;
- newIn = in;
- }
- if (newOut.isEmpty)
- error("queue empty");
+ if (out.isEmpty) {
+ newOut = in.reverse;
+ newIn = Nil;
+ } else {
+ newOut = out;
+ newIn = in;
+ }
+ if (newOut.isEmpty)
+ error("queue empty");
else
- Pair(newOut.head,new Queue(newIn,newOut.tail));
+ Pair(newOut.head,mkQueue(newIn,newOut.tail));
}
/** Returns the first element in the queue, or throws an error if there
diff --git a/test/files/run/iq.scala b/test/files/run/iq.scala
index cb18004ad2..a02a79a53b 100644
--- a/test/files/run/iq.scala
+++ b/test/files/run/iq.scala
@@ -43,11 +43,14 @@ object iq {
*/
java.lang.System.out.println("q5[5]: " + q5(5));
+
+
val q5c:Queue[char] = Queue.Empty.enqueue(0: char, 1: char,
2: char, 3: char,
4: char, 5: char,
6: char, 7: char,
8: char, 9: char);
+
/* Testing ==
* Expected: q5 == q9: true
* q9 == q5: true
@@ -62,7 +65,7 @@ object iq {
* Expected: q8: Queue(2,3,4,5,6,7,8,9,10,11)
*/
java.lang.System.out.println("q8: " + q8);
- val q9 = Queue.Empty.enqueue(2,3,4,5,6,7,8,9,10,11);
+ val q9 = new Queue(2,3,4,5,6,7,8,9,10,11);
/* Testing ==
* Expected: q8 == q9: true