summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdmund Noble <edmundnoble@gmail.com>2016-12-04 20:15:30 -0500
committerEdmund Noble <Edmund Noble>2017-01-18 17:36:45 -0500
commit409f84d29f1dfae112a024d90aeb187248d48a3c (patch)
tree800936ed6f1a34ac1607cb5e2d6ca0a6a7679684
parent264cc5f20cd9a6b9c6d414dfea696de1b8608dc5 (diff)
downloadscala-409f84d29f1dfae112a024d90aeb187248d48a3c.tar.gz
scala-409f84d29f1dfae112a024d90aeb187248d48a3c.tar.bz2
scala-409f84d29f1dfae112a024d90aeb187248d48a3c.zip
Improve Queue.++ when building another Queue
Use reverse_:::
-rw-r--r--src/library/scala/collection/immutable/Queue.scala9
-rw-r--r--test/files/run/iq.scala8
2 files changed, 16 insertions, 1 deletions
diff --git a/src/library/scala/collection/immutable/Queue.scala b/src/library/scala/collection/immutable/Queue.scala
index 53af3ce158..bb029ebe70 100644
--- a/src/library/scala/collection/immutable/Queue.scala
+++ b/src/library/scala/collection/immutable/Queue.scala
@@ -100,6 +100,15 @@ class Queue[+A] protected(protected val in: List[A], protected val out: List[A])
case _ => super.:+(elem)(bf)
}
+ override def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Queue[A], B, That]): That = {
+ if (bf eq Queue.ReusableCBF) {
+ val thatQueue = that.asInstanceOf[Queue[B]]
+ new Queue[B](thatQueue.in ++ (thatQueue.out reverse_::: this.in), this.out).asInstanceOf[That]
+ } else {
+ super.++(that)(bf)
+ }
+ }
+
/** Creates a new queue with element added at the end
* of the old queue.
*
diff --git a/test/files/run/iq.scala b/test/files/run/iq.scala
index 0ccf67a2e9..9929f0e1a0 100644
--- a/test/files/run/iq.scala
+++ b/test/files/run/iq.scala
@@ -25,12 +25,18 @@ object iq {
assert(q2 == qb)
val qc = 42 +: q :+ 0
assert(q2 == qc)
+ assert(q ++ qa == qa)
+ val qdr = 1 +: 2 +: 3 +: 4 +: q
+ val qcon1 = 1 +: 2 +: q
+ val qcon2 = q :+ 3 :+ 4
+ val qd = qcon1 ++ qcon2
+ assert(qd == qdr)
Console.println("q2: " + q2)
Console.println("qa: " + qa)
Console.println("qb: " + qb)
Console.println("qc: " + qc)
-
+
/* Test is empty and dequeue.
* Expected: Head: 42
*/