Skip to content

Commit 8c9f695

Browse files
committed
make Heap::as_slice not panic on empty heap
1 parent ae280d5 commit 8c9f695

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/dary/heap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ where
207207
/// assert!(slice.contains(&("z", 99)));
208208
/// ```
209209
pub(crate) fn as_slice(&self) -> &[(N, K)] {
210-
&self.tree[offset::<D>()..]
210+
if self.tree.is_empty() {
211+
&self.tree
212+
} else {
213+
&self.tree[offset::<D>()..]
214+
}
211215
}
212216
}
213217

tests/priority_queue_tests/as_slice.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn test_as_slice<const D: usize>(mut pq: DaryHeap<usize, f64, D>) {
2020
pq.clear();
2121
assert!(pq.is_empty());
2222
let mut vec = vec![];
23+
assert!(pq.as_slice().is_empty());
2324

2425
let mut rng = rand::rng();
2526
for node in 0..N {

0 commit comments

Comments
 (0)