Get basic incomplete Python queue API working
This commit is contained in:
parent
661e1d220a
commit
0995e6db90
8 changed files with 69 additions and 45 deletions
|
@ -4,7 +4,7 @@ use super::PureBacking;
|
|||
|
||||
/// A binary min-heap backed by an array
|
||||
#[derive(Debug)]
|
||||
pub struct BinaryHeap<T: Ord + Copy> {
|
||||
pub struct BinaryHeap<T: Ord + Clone + Send + Sync> {
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
|
@ -33,18 +33,23 @@ impl fmt::Display for SiftError {
|
|||
/// Whether a sift operation succeeded
|
||||
type SiftResult = Result<(), SiftError>;
|
||||
|
||||
impl<T: Ord + Copy> BinaryHeap<T> {
|
||||
impl<T: Ord + Clone + Send + Sync> BinaryHeap<T> {
|
||||
/// Instantiates a new (empty) binary heap
|
||||
pub fn new() -> Self {
|
||||
Self { data: vec![] }
|
||||
}
|
||||
|
||||
/// Fix an index representing a node with valid children but that may violate the heap property compared to its immediate parent
|
||||
fn sift_up(&mut self, i: usize) -> SiftResult {
|
||||
if i == 0 {
|
||||
// Base case, at root so nothing to do
|
||||
Ok(())
|
||||
} else if let Some(child) = self.data.get(i).copied() {
|
||||
} else if let Some(child) = self.data.get(i).cloned() {
|
||||
let parent_index = (i - 1) / 2;
|
||||
// Check if the heap property is violated
|
||||
if child < self.data[parent_index] {
|
||||
// Swap child with parent
|
||||
self.data[i] = self.data[parent_index];
|
||||
self.data[i] = self.data[parent_index].clone();
|
||||
self.data[parent_index] = child;
|
||||
|
||||
// Repeat process with parent
|
||||
|
@ -64,12 +69,12 @@ impl<T: Ord + Copy> BinaryHeap<T> {
|
|||
// Tried to sift a non-existent index
|
||||
Err(SiftError::new(i, self.data.len()))
|
||||
} else {
|
||||
if let Some(first_child) = self.data.get(i * 2 + 1).copied() {
|
||||
if let Some(first_child) = self.data.get(i * 2 + 1).cloned() {
|
||||
let smaller_child_index;
|
||||
let smaller_child;
|
||||
|
||||
// Find the smallest child and its index
|
||||
if let Some(second_child) = self.data.get(i * 2 + 2).copied() {
|
||||
if let Some(second_child) = self.data.get(i * 2 + 2).cloned() {
|
||||
// Both children, use the smaller one
|
||||
if first_child < second_child {
|
||||
smaller_child = first_child;
|
||||
|
@ -86,7 +91,7 @@ impl<T: Ord + Copy> BinaryHeap<T> {
|
|||
|
||||
if smaller_child < self.data[i] {
|
||||
// Swap parent with child
|
||||
self.data[smaller_child_index] = self.data[i];
|
||||
self.data[smaller_child_index] = self.data[i].clone();
|
||||
self.data[i] = smaller_child;
|
||||
|
||||
// Repeat process with child
|
||||
|
@ -103,23 +108,19 @@ impl<T: Ord + Copy> BinaryHeap<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Ord + Copy> FromIterator<T> for BinaryHeap<T> {
|
||||
impl<T: Ord + Clone + Send + Sync> FromIterator<T> for BinaryHeap<T> {
|
||||
fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self {
|
||||
let mut this = Self {
|
||||
data: Vec::from_iter(iter),
|
||||
};
|
||||
for i in (0..=(this.data.len() / 2)).rev() {
|
||||
this.sift_down(i);
|
||||
this.sift_down(i).expect("Index error during heapify");
|
||||
}
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord + Copy> PureBacking<T> for BinaryHeap<T> {
|
||||
fn new() -> Self {
|
||||
Self { data: vec![] }
|
||||
}
|
||||
|
||||
impl<T: Ord + Clone + Send + Sync> PureBacking<T> for BinaryHeap<T> {
|
||||
fn add(&mut self, item: T) {
|
||||
// Append item
|
||||
self.data.push(item);
|
||||
|
@ -132,22 +133,18 @@ impl<T: Ord + Copy> PureBacking<T> for BinaryHeap<T> {
|
|||
// No extra processing
|
||||
0 | 1 => self.data.pop(),
|
||||
_ => {
|
||||
let last = self
|
||||
// Get the original root
|
||||
let root = self.data[0].clone();
|
||||
|
||||
// Move final item to the root and sift down to regain heap property
|
||||
self.data[0] = self
|
||||
.data
|
||||
.pop()
|
||||
.expect("Vector claimed not to be empty but was");
|
||||
let root = self
|
||||
.data
|
||||
.get_mut(0)
|
||||
.expect("Vector claimed to have multiple items but didn't");
|
||||
|
||||
// Move final item to the root and sift down to regain heap property
|
||||
let best = *root;
|
||||
*root = last;
|
||||
self.sift_down(0).unwrap();
|
||||
|
||||
// Return original root
|
||||
Some(best)
|
||||
Some(root)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue