On 3/2/07, Adam Olsen
On 3/1/07, Jason Orendorff
wrote: I wonder if anyone else finds the heapq and bisect APIs a little dated.
Ooh, me, me!
It seems like these things could be offered in a more intuitive and
featureful way by making them classes. They could go in the collections module:
I agree, at least for heapq. The algorithm involves enough voodoo
that it's not obvious how to extend it, so it might as well be wrapped in a class that hides as much as possible.
Sounds good to me. +1 The same can't be said for bisect though. All it does is replace del
a[bisect_left(a, x)] with a.remove(x). A little cleaner, but no more obvious. And the cost is still O(n) since it involves moving all the pointers after x.
A sortedlist class seems like a useful abstraction to me, since while using an instance of such a class, you could be sure that the list remains sorted. For example, you don't have to worry about it being changed outside of your code and no longer being sorted. Also, wouldn't a sortedlist class also have in insert() method to replace bisect.insort(), as well as different implementations of __contains__ and index?