Skip to content

Commit 5817d40

Browse files
committed
Add immutable Queue to stdlib
1 parent a1fc706 commit 5817d40

File tree

1 file changed

+218
-0
lines changed
  • tests/pos-special/stdlib/collection/immutable

1 file changed

+218
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.collection
14+
package immutable
15+
16+
import scala.collection.generic.DefaultSerializable
17+
import scala.collection.mutable.{Builder, ListBuffer}
18+
import language.experimental.captureChecking
19+
20+
/** `Queue` objects implement data structures that allow to
21+
* insert and retrieve elements in a first-in-first-out (FIFO) manner.
22+
*
23+
* `Queue` is implemented as a pair of `List`s, one containing the ''in'' elements and the other the ''out'' elements.
24+
* Elements are added to the ''in'' list and removed from the ''out'' list. When the ''out'' list runs dry, the
25+
* queue is pivoted by replacing the ''out'' list by ''in.reverse'', and ''in'' by ''Nil''.
26+
*
27+
* Adding items to the queue always has cost `O(1)`. Removing items has cost `O(1)`, except in the case
28+
* where a pivot is required, in which case, a cost of `O(n)` is incurred, where `n` is the number of elements in the queue. When this happens,
29+
* `n` remove operations with `O(1)` cost are guaranteed. Removing an item is on average `O(1)`.
30+
*
31+
* @see [[https://docs.scala-lang.org/overviews/collections-2.13/concrete-immutable-collection-classes.html#immutable-queues "Scala's Collection Library overview"]]
32+
* section on `Immutable Queues` for more information.
33+
*
34+
* @define Coll `immutable.Queue`
35+
* @define coll immutable queue
36+
* @define mayNotTerminateInf
37+
* @define willNotTerminateInf
38+
*/
39+
40+
sealed class Queue[+A] protected(protected val in: List[A], protected val out: List[A])
41+
extends AbstractSeq[A]
42+
with LinearSeq[A]
43+
with LinearSeqOps[A, Queue, Queue[A]]
44+
with StrictOptimizedLinearSeqOps[A, Queue, Queue[A]]
45+
with StrictOptimizedSeqOps[A, Queue, Queue[A]]
46+
with IterableFactoryDefaults[A, Queue]
47+
with DefaultSerializable {
48+
49+
override def iterableFactory: SeqFactory[Queue] = Queue
50+
51+
/** Returns the `n`-th element of this queue.
52+
* The first element is at position `0`.
53+
*
54+
* @param n index of the element to return
55+
* @return the element at position `n` in this queue.
56+
* @throws NoSuchElementException if the queue is too short.
57+
*/
58+
override def apply(n: Int): A = {
59+
def indexOutOfRange(): Nothing = throw new IndexOutOfBoundsException(n.toString)
60+
61+
var index = 0
62+
var curr = out
63+
64+
while (index < n && curr.nonEmpty) {
65+
index += 1
66+
curr = curr.tail
67+
}
68+
69+
if (index == n) {
70+
if (curr.nonEmpty) curr.head
71+
else if (in.nonEmpty) in.last
72+
else indexOutOfRange()
73+
} else {
74+
val indexFromBack = n - index
75+
val inLength = in.length
76+
if (indexFromBack >= inLength) indexOutOfRange()
77+
else in(inLength - indexFromBack - 1)
78+
}
79+
}
80+
81+
/** Returns the elements in the list as an iterator
82+
*/
83+
override def iterator: Iterator[A] = out.iterator.concat(in.reverse)
84+
85+
/** Checks if the queue is empty.
86+
*
87+
* @return true, iff there is no element in the queue.
88+
*/
89+
override def isEmpty: Boolean = in.isEmpty && out.isEmpty
90+
91+
override def head: A =
92+
if (out.nonEmpty) out.head
93+
else if (in.nonEmpty) in.last
94+
else throw new NoSuchElementException("head on empty queue")
95+
96+
override def tail: Queue[A] =
97+
if (out.nonEmpty) new Queue(in, out.tail)
98+
else if (in.nonEmpty) new Queue(Nil, in.reverse.tail)
99+
else throw new NoSuchElementException("tail on empty queue")
100+
101+
override def last: A =
102+
if (in.nonEmpty) in.head
103+
else if (out.nonEmpty) out.last
104+
else throw new NoSuchElementException("last on empty queue")
105+
106+
/* This is made to avoid inefficient implementation of iterator. */
107+
override def forall(p: A => Boolean): Boolean =
108+
in.forall(p) && out.forall(p)
109+
110+
/* This is made to avoid inefficient implementation of iterator. */
111+
override def exists(p: A => Boolean): Boolean =
112+
in.exists(p) || out.exists(p)
113+
114+
override protected[this] def className = "Queue"
115+
116+
/** Returns the length of the queue. */
117+
override def length: Int = in.length + out.length
118+
119+
override def prepended[B >: A](elem: B): Queue[B] = new Queue(in, elem :: out)
120+
121+
override def appended[B >: A](elem: B): Queue[B] = enqueue(elem)
122+
123+
override def appendedAll[B >: A](that: scala.collection.IterableOnce[B]^): Queue[B] = {
124+
val newIn = that match {
125+
case that: Queue[B] => that.in ++ (that.out reverse_::: this.in)
126+
case that: List[B] => that reverse_::: this.in
127+
case _ =>
128+
var result: List[B] = this.in
129+
val iter = that.iterator
130+
while (iter.hasNext) {
131+
result = iter.next() :: result
132+
}
133+
result
134+
}
135+
if (newIn eq this.in) this else new Queue[B](newIn, this.out)
136+
}
137+
138+
/** Creates a new queue with element added at the end
139+
* of the old queue.
140+
*
141+
* @param elem the element to insert
142+
*/
143+
def enqueue[B >: A](elem: B): Queue[B] = new Queue(elem :: in, out)
144+
145+
/** Creates a new queue with all elements provided by an `Iterable` object
146+
* added at the end of the old queue.
147+
*
148+
* The elements are appended in the order they are given out by the
149+
* iterator.
150+
*
151+
* @param iter an iterable object
152+
*/
153+
@deprecated("Use `enqueueAll` instead of `enqueue` to enqueue a collection of elements", "2.13.0")
154+
@`inline` final def enqueue[B >: A](iter: scala.collection.Iterable[B]) = enqueueAll(iter)
155+
156+
/** Creates a new queue with all elements provided by an `Iterable` object
157+
* added at the end of the old queue.
158+
*
159+
* The elements are appended in the order they are given out by the
160+
* iterator.
161+
*
162+
* @param iter an iterable object
163+
*/
164+
def enqueueAll[B >: A](iter: scala.collection.Iterable[B]): Queue[B] = appendedAll(iter)
165+
166+
/** Returns a tuple with the first element in the queue,
167+
* and a new queue with this element removed.
168+
*
169+
* @throws NoSuchElementException
170+
* @return the first element of the queue.
171+
*/
172+
def dequeue: (A, Queue[A]) = out match {
173+
case Nil if !in.isEmpty => val rev = in.reverse ; (rev.head, new Queue(Nil, rev.tail))
174+
case x :: xs => (x, new Queue(in, xs))
175+
case _ => throw new NoSuchElementException("dequeue on empty queue")
176+
}
177+
178+
/** Optionally retrieves the first element and a queue of the remaining elements.
179+
*
180+
* @return A tuple of the first element of the queue, and a new queue with this element removed.
181+
* If the queue is empty, `None` is returned.
182+
*/
183+
def dequeueOption: Option[(A, Queue[A])] = if(isEmpty) None else Some(dequeue)
184+
185+
/** Returns the first element in the queue, or throws an error if there
186+
* is no element contained in the queue.
187+
*
188+
* @throws NoSuchElementException
189+
* @return the first element.
190+
*/
191+
def front: A = head
192+
193+
/** Returns a string representation of this queue.
194+
*/
195+
override def toString(): String = mkString("Queue(", ", ", ")")
196+
}
197+
198+
/** $factoryInfo
199+
* @define Coll `immutable.Queue`
200+
* @define coll immutable queue
201+
*/
202+
@SerialVersionUID(3L)
203+
object Queue extends StrictOptimizedSeqFactory[Queue] {
204+
def newBuilder[sealed A]: Builder[A, Queue[A]] = new ListBuffer[A] mapResult (x => new Queue[A](Nil, x))
205+
206+
def from[A](source: IterableOnce[A]^): Queue[A] = source match {
207+
case q: Queue[A] => q
208+
case _ =>
209+
val list = List.from(source)
210+
if (list.isEmpty) empty
211+
else new Queue(Nil, list)
212+
}
213+
214+
def empty[A]: Queue[A] = EmptyQueue
215+
override def apply[A](xs: A*): Queue[A] = new Queue[A](Nil, xs.toList)
216+
217+
private object EmptyQueue extends Queue[Nothing](Nil, Nil) { }
218+
}

0 commit comments

Comments
 (0)