Module: heap
Contents
StructsBoundedHeap- A bounded max-heap that keeps the K entries with the smallest “distance”
resq_dsa::heap::BoundedHeap
Struct A bounded max-heap that keeps the K entries with the smallest “distance” values. This data structure is useful for tracking the K-nearest neighbors or K-shortest paths. The root always contains the entry with the maximum distance among the kept items. When the heap is full and a new entry has a smaller distance than the root, the root is evicted and the new entry takes its place.Algorithm
Uses a max-heap where:- The root (index 0) always holds the entry with the largest distance
- When full, only entries with distance less than the current max can be inserted
- Insertion is O(log k) where k is the limit
Use Cases
- K-nearest neighbor search in path planning
- Finding K shortest paths
- Maintaining top-K results in streaming scenarios
Examples
- T
- D
fn new(limit: usize, dist: D) -> Self- Creates a new bounded heap with the given limit and distance function.fn insert(self: & mut Self, entry: T)- Inserts an entry into the heap.fn peek(self: &Self) -> Option<&T>- Returns a reference to the entry with the maximum distance (the root).fn to_sorted(self: &Self) -> Vec<&T>- Returns all entries sorted by distance (ascending, nearest first).fn len(self: &Self) -> usize- Returns the current number of entries in the heap.fn is_empty(self: &Self) -> bool- Returnstrueif the heap contains no entries.