Steve
Steve

Reputation: 23

Scala PriorityQueue conflict resolution?

I'm working on a project that uses a PriorityQueue and A*. After digging around a ton I think part of the problem that I'm encountering while my search tries to solve my problem is in the PriorityQueue. I'm guessing that when it generates nodes of equal scoring (for example one earlier, and one later) it will chose the one from earlier rather than the one that was most recently generated.

Does anyone know if a PriorityQueue prioritizes the newest node if the scores are the same? If not, how can I make it do this?

Thanks!

Upvotes: 2

Views: 94

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167901

PriorityQueue uses a heap to select the next element. Beyond that it makes no guarantees about how the elements are ordered. If it is important to you that nodes are ordered by addition order, you should keep a count of the number of items added and prioritize by the tuple (priority, -order).

If you do anything else, even if it happens to work now, it may break at any arbitrary time since the API makes no guarantees about how it chooses from among equal elements.

Upvotes: 2

Related Questions