Skip to content

Commit 2726320

Browse files
committed
Merge pull request scala#5132 from acdenhartog/patch-1
Avoid function chaining with reverse method
2 parents fbd2e35 + 284571c commit 2726320

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/library/scala/collection/mutable/PriorityQueue.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ sealed class PriorityQueue[A](implicit val ord: Ordering[A])
200200
* @return A reversed priority queue.
201201
*/
202202
def reverse = {
203-
val revq = new PriorityQueue[A]()(new scala.math.Ordering[A] {
204-
def compare(x: A, y: A) = ord.compare(y, x)
205-
})
203+
val revq = new PriorityQueue[A]()(ord.reverse)
206204
for (i <- 1 until resarr.length) revq += resarr(i)
207205
revq
208206
}

test/junit/scala/collection/mutable/PriorityQueueTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class PriorityQueueTest {
1313
val elements = List.fill(1000)(scala.util.Random.nextInt(Int.MaxValue))
1414
priorityQueue.enqueue(elements :_*)
1515

16+
@Test
17+
def orderingReverseReverse() {
18+
val pq = new mutable.PriorityQueue[Nothing]()((_,_)=>42)
19+
assert(pq.ord eq pq.reverse.reverse.ord)
20+
}
21+
1622
@Test
1723
def canSerialize() {
1824
val outputStream = new ByteArrayOutputStream()
@@ -27,6 +33,7 @@ class PriorityQueueTest {
2733

2834
val objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes))
2935
val deserializedPriorityQueue = objectInputStream.readObject().asInstanceOf[PriorityQueue[Int]]
36+
//correct sequencing is also tested here:
3037
assert(deserializedPriorityQueue.dequeueAll == elements.sorted.reverse)
3138
}
3239
}

0 commit comments

Comments
 (0)