diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2009-11-24 16:35:09 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2009-11-24 16:35:09 +0000 |
commit | 4ad672b0b279ee5b28d166483928a5acc6d136d7 (patch) | |
tree | e14eb4d555a24c182fb533b0d82d736fb8c04c00 /src | |
parent | d26b2f2b94af88eab8494c04924c86e6efd8e888 (diff) | |
download | scala-4ad672b0b279ee5b28d166483928a5acc6d136d7.tar.gz scala-4ad672b0b279ee5b28d166483928a5acc6d136d7.tar.bz2 scala-4ad672b0b279ee5b28d166483928a5acc6d136d7.zip |
Added reverse capabilities to PriorityQueue.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/mutable/PriorityQueue.scala | 20 |
1 files changed, 19 insertions, 1 deletions
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 + * <code>x</code> and <code>y</code> the original queue's ordering + * had <code>compare</code> 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 + * <code>compare</code> 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) |