diff options
-rw-r--r-- | src/library/scala/collection/mutable/PriorityQueue.scala | 14 | ||||
-rw-r--r-- | test/files/run/priorityQueue.scala | 40 |
2 files changed, 44 insertions, 10 deletions
diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index d6b5d12749..54505dbdf6 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -202,6 +202,20 @@ class PriorityQueue[A](implicit ord: Ordering[A]) } } + override def reverse = throw new UnsupportedOperationException("Priority queue cannot be reversed.") + + override def reverseIterator = new Iterator[A] { + val arr = new Array[Any](size) + iterator.copyToArray(arr) + var i = arr.size - 1 + def hasNext: Boolean = i >= 0 + def next(): A = { + val curr = arr(i) + i -= 1 + curr.asInstanceOf[A] + } + } + /** The hashCode method always yields an error, since it is not * safe to use mutable queues as keys in hash tables. * diff --git a/test/files/run/priorityQueue.scala b/test/files/run/priorityQueue.scala index e582448342..54090091ec 100644 --- a/test/files/run/priorityQueue.scala +++ b/test/files/run/priorityQueue.scala @@ -21,7 +21,7 @@ object Test { testDrops testUpdates testEquality - testSeq + testMisc } def testInsertionsAndEqualities { @@ -232,9 +232,10 @@ object Test { assertPriorityDestructive(pq2) } - def testSeq { + def testMisc { val pq = new PriorityQueue[Int] pq ++= (0 until 100) + assert(pq.size == 100) val (p1, p2) = pq.partition(_ < 50) assertPriorityDestructive(p1) @@ -246,14 +247,33 @@ object Test { pq.clear pq ++= (0 until 10) pq += 5 - //val ind = pq.lastIndexWhere(_ == 5) - //println(ind) - //println(pq) - //assert(ind == 5) - //assertPriorityDestructive(pq) - - //pq.clear - //pq ++= (0 until 10) + assert(pq.size == 11) + + val ind = pq.lastIndexWhere(_ == 5) + assert(ind == 5) + assertPriorityDestructive(pq) + + pq.clear + pq ++= (0 until 10) + assert(pq.lastIndexWhere(_ == 9) == 0) + assert(pq.lastIndexOf(8) == 1) + assert(pq.lastIndexOf(7) == 2) + + pq += 5 + pq += 9 + assert(pq.lastIndexOf(9) == 1) + assert(pq.lastIndexWhere(_ % 2 == 1) == 10) + assert(pq.lastIndexOf(5) == 6) + + val lst = pq.reverseIterator.toList + for (i <- 0 until 5) assert(lst(i) == i) + assert(lst(5) == 5) + assert(lst(6) == 5) + assert(lst(7) == 6) + assert(lst(8) == 7) + assert(lst(9) == 8) + assert(lst(10) == 9) + assert(lst(11) == 9) } } |