Closed mernst closed 3 years ago
The specification of PriorityQueue states "Elements that are smaller in the specified order are dequeued first." A FIFO queue violates this contract, so it is not a behavioral subtype of a priority queue.
No, it doesn't. Why it is so?
The given test case shows an example: 1 is smaller, but there exists a PriorityQueue such that 1 is not dequeued first.
I think you are very confused about the notion of "specified order". A FIFO queue has only one possible order—insertion order. That's the priority order. The nature of the elements is irrelevant. This is algorithm 101.
As the term is used by computer scientists, a priority queue's order depends on the values, never on the order of insertion. This definition is consistent with the JDK, Wikipedia, and every algorithms and data structures textbook I know of. Do you know of a reputable text that says otherwise?
In a regular queue, the order depends on the order of insertion.
I agree that it is a bit confusing that queues and priority queues are unrelated data structures, but that is the standard terminology.
Perhaps fastutil uses the term "priority queue" differently than the rest of the field. That is fine, but if so, I think it would be helpful for the documentation to highlight that fact. As currently written, the naming is confusing for someone who expects standard terminology.
I think you should read the Wikipedia entry, rather than just quote it 🤷🏻♂️...
The specification of
PriorityQueue
states "Elements that are smaller in the specified order are dequeued first." A FIFO queue violates this contract, so it is not a behavioral subtype of a priority queue.In fastutil,
IntArrayFIFOQueue
implementsPriorityQueue
, which violates behavioral subtyping and leads to buggy behavior in clients. I think that the right fix is to change all FIFO queues (inArrayFIFOQueue.drv
) to not implementPriorityQueue
.This Java program illustrates the problem:
To run it, execute these commands:
The program outputs
Also, I noticed that the tests
IntArrayPriorityQueueTest.java
andIntHeapPriorityQueueTest.java
always enqueue and dequeue values in the same order. This does not test the priority queue functionality. It might be good to expand these tests.