m3sh.heap.Heap.

push#

Heap.push(obj, key)#

Add item.

Add data object with associated key. Re-adding an object replaces the corresponding key instead of adding a duplicate with different key. Maintains the heap property.

Parameters:
objobject

Data object. Has to be hashable.

keyobject

Key object, typically a numeric type. Custom classes have to implement __lt__().

Raises:
TypeError

If obj is not derived from a hashable data type.

Notes

The (obj, key) 2-tuple is called a heap item. Keys are compared using the < operator. See the comment on sort stability in the heapq documentation (add a counter as tie-breaker):

>>> heap.push(obj, (key, cnt))