From 3fcbbf4f812517f929950eae4a34db92f02d19fd Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 17 Dec 2023 18:23:47 +0100 Subject: [PATCH] =?UTF-8?q?feat(ds):=20implement=20=E2=80=B9From=E2=80=BA?= =?UTF-8?q?=20and=20=E2=80=B9FromIterator=E2=80=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- src/data_structures/min_heap.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/data_structures/min_heap.rs b/src/data_structures/min_heap.rs index 2996a82..9dfe509 100644 --- a/src/data_structures/min_heap.rs +++ b/src/data_structures/min_heap.rs @@ -26,3 +26,25 @@ impl Default for MinHeap { Self::new() } } + +impl From<[T; N]> for MinHeap +where + T: Ord, +{ + fn from(value: [T; N]) -> Self { + Self { + heap: BinaryHeap::from(value.map(|x| Reverse(x))), + } + } +} + +impl FromIterator for MinHeap +where + T: Ord, +{ + fn from_iter>(iter: U) -> Self { + Self { + heap: BinaryHeap::from_iter(iter.into_iter().map(|x| Reverse(x))), + } + } +}