Add PyItem container

Also reduce queue item trait bound from Ord to just PartialOrd
This commit is contained in:
Michael Bradley 2025-01-10 20:53:28 +13:00
parent ee004bac19
commit 38a544db76
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
8 changed files with 91 additions and 32 deletions

View file

@ -2,12 +2,6 @@ use std::fmt;
use super::PureBacking;
/// A binary min-heap backed by an array
#[derive(Debug)]
pub struct BinaryHeap<T: Ord + Clone + Send + Sync> {
data: Vec<T>,
}
/// Indicates why a sift failed
#[derive(Debug, Clone)]
struct SiftError {
@ -33,7 +27,13 @@ impl fmt::Display for SiftError {
/// Whether a sift operation succeeded
type SiftResult = Result<(), SiftError>;
impl<T: Ord + Clone + Send + Sync> BinaryHeap<T> {
/// A binary min-heap backed by an array
#[derive(Debug)]
pub struct BinaryHeap<T: PartialOrd + Clone + Send + Sync> {
data: Vec<T>,
}
impl<T: PartialOrd + Clone + Send + Sync> BinaryHeap<T> {
/// Instantiates a new (empty) binary heap
pub fn new() -> Self {
Self { data: vec![] }
@ -108,7 +108,7 @@ impl<T: Ord + Clone + Send + Sync> BinaryHeap<T> {
}
}
impl<T: Ord + Clone + Send + Sync> FromIterator<T> for BinaryHeap<T> {
impl<T: PartialOrd + 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),
@ -120,7 +120,7 @@ impl<T: Ord + Clone + Send + Sync> FromIterator<T> for BinaryHeap<T> {
}
}
impl<T: Ord + Clone + Send + Sync> PureBacking<T> for BinaryHeap<T> {
impl<T: PartialOrd + Clone + Send + Sync> PureBacking<T> for BinaryHeap<T> {
fn add(&mut self, item: T) {
// Append item
self.data.push(item);