From 4ad672b0b279ee5b28d166483928a5acc6d136d7 Mon Sep 17 00:00:00 2001 From: Aleksandar Pokopec Date: Tue, 24 Nov 2009 16:35:09 +0000 Subject: Added reverse capabilities to PriorityQueue. --- .../scala/collection/mutable/PriorityQueue.scala | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index 54505dbdf6..5338d358f3 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -202,7 +202,25 @@ class PriorityQueue[A](implicit ord: Ordering[A]) } } - override def reverse = throw new UnsupportedOperationException("Priority queue cannot be reversed.") + /** + * Returns the reverse of this queue. The priority queue that gets + * returned will have an inversed ordering - if for some elements + * x and y the original queue's ordering + * had compare returning an integer w, the new one will return -w, + * assuming the original ordering abides its contract. + * + * Note that the order of the elements will be reversed unless the + * compare method returns 0. In this case, such elements + * will be subsequent, but their corresponding subinterval may be inappropriately + * reversed. However, due to the compare-equals contract, they will also be equal. + */ + override def reverse = { + val revq = new PriorityQueue[A]()(new math.Ordering[A] { + def compare(x: A, y: A) = ord.compare(y, x) + }) + for (i <- 1 until resarr.length) revq += resarr(i) + revq + } override def reverseIterator = new Iterator[A] { val arr = new Array[Any](size) -- cgit v1.2.3