Assuming I have a custom class for which I would like to have a priority queue (having a fixed size, say 100 objects). But the problem with the standard queue.PriorityQueue
is that having a maximum size blocks the insertion after the queue is full.
I would like the buffer to still allow for insertions by removing the least priority elements (as decided by my __cmp__(self, other)
function implementation) from the queue. Does there exist any built-in implementations of the same?
No, there is no built-in feature for that.
What's more, a priory queue is designed to identify the top priority entry fast, but is not designed to quickly identify which is the item with the least priority. If you would add logic to find it, it would kill the performance of the queue.
I would suggest implementing a so-called min-max heap, which can identify both the least and greatest element in an efficient manner.
On the other hand, if you are only targetting a maximum size of 100 elements, you should probably just work with a list and keep it sorted. For such small lists that will be fast enough, and it is easy to clip the list when it exceeds the maximum size.