Just a bit more on challenges implementing Heap
/ SortedList
and similar objects properly.
Have a look at implementation of sortedcontainers.SortedList
.
Some complexities would not apply to Heap
equivalent (SortedList
is built on bisect
), however some would.
E.g. If there is implementation of such in standard library it should be properly performant and for that it would need to keep 2 lists - one with keys, another with values.
So my POV is that sensible order to improve things would be:
1-2. `KeyCompared` utility class
1-2. `key` argument for `heapq` module (for consistency with `bisect`)
3. `bisect.SortedList` class.
If there was a class for priority Q, I think it would make much more sense to have SortedList
as it is much more functional.
There is a set of cases where heapq
is enough and it will always be - e.g. Dijkstra’s algorithm.
However, there are many cases when there is always a risk that there will arise a need to know not only smallest item, but N smallest items or even be able to look up item in i’th place. In other words, to have continuously sorted list.
Although lst.sort()
maintains heap invariance, but calling lst.sort()
after each heapq.pushitem
would neither be preferable from performance nor practical perspectives.
Although there is an argument to just use sortedcontainers.SortedList
, but having such in standard library would be beneficial as it would both improve performance and also something implemented in standard library would bring it to the next level of simplicity, robustness, etc.
heapq.Heap
class is just not worth it, IMO.