1
0
Fork 0

feat(ds): implement ‹From› and ‹FromIterator›

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-12-17 18:23:47 +01:00
parent 4c07bd4f2e
commit 3fcbbf4f81
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -26,3 +26,25 @@ impl<T: Ord> Default for MinHeap<T> {
Self::new() Self::new()
} }
} }
impl<T, const N: usize> From<[T; N]> for MinHeap<T>
where
T: Ord,
{
fn from(value: [T; N]) -> Self {
Self {
heap: BinaryHeap::from(value.map(|x| Reverse(x))),
}
}
}
impl<T: Ord> FromIterator<T> for MinHeap<T>
where
T: Ord,
{
fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self {
Self {
heap: BinaryHeap::from_iter(iter.into_iter().map(|x| Reverse(x))),
}
}
}