diffeo / heapq2

extension to Python standard library heapq package

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

heapq2

Python standard library list.sort() takes an comparison function argument, so heapq should too.

These functions have been extended to take a cmp_lt(a,b) function which returns True if a is less than b:

  • heappush(), heappop(), heapreplace(), heappushpop(), heapify(), nlargest(), nsmallest(), merge()

The default cmp_lt functions as before, using a.lt(b) if available, or falling back to (b >= a).

An 'index=0' argument has been added to heappop() and heapreplace() so that an element other than the head of the heap-queue can be popped.

Other guidance on the Python standard library heapq routines suggested wrapping objects in a tuple with the sort key in the first element and the value in the second, or using a wrapper object that defined lt(), but in maintaining a queue of many millions of elements the object overhead gets problematic and replacing all those wrapper objects with one comparison function is efficient.

About

extension to Python standard library heapq package

License:MIT License


Languages

Language:Python 100.0%