Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ impl<K: Radix + Ord + Copy, V> RadixHeapMap<K, V> {
ret
}

/// Return the greatest element from the heap without removing it, or `None` if
/// empty.
///
/// If there is a tie between multiple elements, the last inserted element
/// will be peeked first.
#[inline]
pub fn peek(&mut self) -> Option<&(K, V)> {
if self.buckets[0].is_empty() {
self.constrain();
}
self.buckets[0].last()
}

/// Returns the number of elements in the heap
#[inline]
pub fn len(&self) -> usize {
Expand Down Expand Up @@ -808,4 +821,22 @@ mod tests {
vec.sort();
assert_eq!(vec, vec![(1, 2), (5, 4)]);
}

#[test]
fn peek() {
let mut heap = RadixHeapMap::new();
heap.push(1, 2);
heap.push(5, 4);
heap.push(7, 1);

assert_eq!(Some(&(7, 1)), heap.peek());
assert_eq!(Some(&(7, 1)), heap.peek());
assert_eq!(Some((7, 1)), heap.pop());
assert_eq!(Some(&(5, 4)), heap.peek());
assert_eq!(Some((5, 4)), heap.pop());
assert_eq!(Some(&(1, 2)), heap.peek());
assert_eq!(Some((1, 2)), heap.pop());
assert_eq!(None, heap.peek());
assert_eq!(None, heap.pop());
}
}