resq_dsa.heap
Bounded heap data structure for top-k queries. This module provides a bounded min-heap implementation for maintaining the k smallest (or largest) elements from a stream of data.Callable
Generic
TypeVar
T
BoundedHeap Objects
_limit- Maximum number of elements to maintain._dist- Function to compute distance/score for elements._data- Internal heap storage.
heap = BoundedHeap(limit=3, dist=lambda x: x) for i in [5, 2, 8, 1, 9]: … heap.insert(i) heap.to_sorted() [1, 2, 5]
BoundedHeap.__init__
limit- Maximum number of elements to maintain.dist- Function to compute the distance/score for ranking.
ValueError- If limit is less than 1.
heap = BoundedHeap(limit=5, dist=lambda x: abs(x - 10))
BoundedHeap.insert
entry- The element to insert.
heap = BoundedHeap(limit=3, dist=lambda x: x) heap.insert(5) heap.insert(2)
BoundedHeap.peek
heap = BoundedHeap(limit=3, dist=lambda x: x) heap.insert(5) heap.peek() 5
BoundedHeap.to_sorted
heap = BoundedHeap(limit=3, dist=lambda x: x) heap.insert(5) heap.insert(2) heap.to_sorted() [2, 5]
BoundedHeap.size
heap = BoundedHeap(limit=3, dist=lambda x: x) heap.insert(1) heap.insert(2) heap.size 2