From a1a6754a63902f4d58774793bd2b89c30ef07a09 Mon Sep 17 00:00:00 2001 From: Andrei Navumenka Date: Sat, 31 Aug 2024 23:18:30 -0400 Subject: [PATCH] Improve mergeable_heap.rs --- rust/structures/mergeable_heap.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/rust/structures/mergeable_heap.rs b/rust/structures/mergeable_heap.rs index c205a40a..1d3256b5 100644 --- a/rust/structures/mergeable_heap.rs +++ b/rust/structures/mergeable_heap.rs @@ -15,23 +15,22 @@ impl Heap { })) } - fn merge(a: Option>>, b: Option>>) -> Option>> { + fn merge(mut a: Option>>, mut b: Option>>) -> Option>> { if a.is_none() { return b; } if b.is_none() { return a; } - let mut ra = a.unwrap(); - let mut rb = b.unwrap(); - if ra.value > rb.value { - swap(&mut ra, &mut rb); + if a.as_ref()?.value > b.as_ref()?.value { + swap(&mut a, &mut b); } + let mut ha = a?; if rand::random() { - swap(&mut ra.left, &mut ra.right); + swap(&mut ha.left, &mut ha.right); } - ra.left = Self::merge(ra.left, Some(rb)); - Some(ra) + ha.left = Self::merge(ha.left, b); + Some(ha) } fn remove_min(heap: Option>>) -> (Option>>, V) {