Improve binary heap performance

This commit is contained in:
hal8174 2025-02-07 23:41:51 +01:00
parent 66f40b4aa4
commit e6e5001334
5 changed files with 241 additions and 142 deletions

View file

@ -1,4 +1,5 @@
use std::fmt::Debug;
pub mod binary_heap;
pub mod fibonacci_heap;
pub trait PriorityQueue<Item>
@ -16,144 +17,10 @@ where
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item));
}
#[derive(Debug)]
pub struct BinaryHeap<Item> {
nextfree: usize,
data: Vec<BinaryHeapEntry<Item>>,
}
#[derive(Debug)]
struct BinaryHeapEntry<Item> {
id: usize,
item: Item,
}
impl<Item> BinaryHeap<Item>
where
Item: PartialOrd,
{
fn downheap(&mut self, index: usize) {
let left = 2 * index + 1;
let right = 2 * index + 2;
if right < self.data.len() {
let smaller = if self.data[left].item < self.data[right].item {
left
} else {
right
};
if self.data[index].item > self.data[smaller].item {
self.data.swap(index, smaller);
self.downheap(smaller);
}
} else if left < self.data.len() && self.data[index].item > self.data[left].item {
self.data.swap(index, left);
self.downheap(left);
}
}
fn upheap(&mut self, index: usize) {
if index > 0 {
let parent = (index - 1) / 2;
if self.data[parent].item > self.data[index].item {
self.data.swap(parent, index);
self.upheap(parent);
}
}
}
fn search(&self, id: usize) -> Option<usize> {
for (i, d) in self.data.iter().enumerate() {
if d.id == id {
return Some(i);
}
}
None
}
}
impl<Item> PriorityQueue<Item> for BinaryHeap<Item>
where
Item: PartialOrd + Clone,
{
type Handle = usize;
fn insert(&mut self, item: Item) -> Self::Handle {
self.data.push(BinaryHeapEntry {
id: self.nextfree,
item: item.clone(),
});
self.upheap(self.data.len() - 1);
self.nextfree += 1;
self.nextfree - 1
}
fn pop_min(&mut self) -> Option<Item> {
if self.data.is_empty() {
None
} else {
let d = self.data.swap_remove(0);
self.downheap(0);
Some(d.item)
}
}
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut Item)) {
if let Some(index) = self.search(*handle) {
f(&mut self.data[index].item);
self.upheap(index);
}
}
fn new() -> Self {
Self {
data: Vec::new(),
nextfree: 0,
}
}
}
#[derive(Debug)]
pub struct Trace<P> {
inner: P,
}
impl<P, I> PriorityQueue<I> for Trace<P>
where
I: PartialOrd + Clone + Debug,
P: PriorityQueue<I>,
{
type Handle = P::Handle;
fn new() -> Self {
println!("New priority queue.");
Self { inner: P::new() }
}
fn insert(&mut self, item: I) -> Self::Handle {
println!("Insert: {item:?}");
self.inner.insert(item)
}
fn pop_min(&mut self) -> Option<I> {
let min = self.inner.pop_min();
println!("Pop min: {min:?}");
min
}
fn decrease_key(&mut self, handle: &Self::Handle, f: impl Fn(&mut I)) {
self.inner.decrease_key(handle, |i| {
let old_i = i.clone();
f(i);
println!("Decrease key: {old_i:?} -> {i:?}");
})
}
}
#[cfg(test)]
mod tests {
use super::binary_heap::{BinaryHeap, FastBinaryHeap};
use super::fibonacci_heap::FibonacciHeap;
use super::BinaryHeap;
use super::PriorityQueue;
macro_rules! test_generics {
@ -184,7 +51,7 @@ mod tests {
}
mod test_macro {
test_generics!(basic_generic decrease_key_generic; BinaryHeap FibonacciHeap);
test_generics!(basic_generic decrease_key_generic; BinaryHeap FastBinaryHeap FibonacciHeap);
}
fn basic_generic<T: PriorityQueue<usize>>() {