summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/immutable/Queue.scala18
1 files changed, 7 insertions, 11 deletions
diff --git a/src/library/scala/collection/immutable/Queue.scala b/src/library/scala/collection/immutable/Queue.scala
index 2d5b05778b..079c3f3a3d 100644
--- a/src/library/scala/collection/immutable/Queue.scala
+++ b/src/library/scala/collection/immutable/Queue.scala
@@ -40,17 +40,13 @@ class Queue[+A] protected(
* @throws Predef.NoSuchElementException if the queue is too short.
*/
def apply(n: Int): A = {
- @tailrec
- def walk(i: Int, inlist: List[A], outlist: List[A]): A =
- (i == 0, inlist.isEmpty, outlist.isEmpty) match {
- case (_, true, true) => throw new NoSuchElementException("index out of range")
- case (true, _, false) => outlist.head
- case (true, _, true) => inlist.last
- case (false, _, false) => walk(i - 1, inlist, outlist.tail)
- case (false, false, true) => walk(i - 1, Nil, inlist.reverse.tail)
- }
-
- walk(n, in, out)
+ val len = out.length
+ if (n < len) out.apply(n)
+ else {
+ val m = n - len
+ if (m < in.length) in.reverse.apply(m)
+ else throw new NoSuchElementException("index out of range")
+ }
}
/** Returns the elements in the list as an iterator