Get basic incomplete Python queue API working

This commit is contained in:
Michael Bradley 2025-01-08 22:52:25 +13:00
parent 661e1d220a
commit 0995e6db90
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
8 changed files with 69 additions and 45 deletions

View file

@ -2,12 +2,13 @@ use std::cmp::Ordering;
/// Helper struct to associate an item with its priority
#[derive(Debug, Clone, Copy)]
pub struct Item<D, P: Ord> {
// I mean I guess P should be Ord but I want to use f64 so whatever
pub struct Item<D: Clone, P: PartialOrd + Clone> {
data: D,
priority: P,
}
impl<D, P: Ord> Item<D, P> {
impl<D: Clone, P: PartialOrd + Clone> Item<D, P> {
/// Creates a new instance
fn new(data: D, priority: P) -> Self {
Self { data, priority }
@ -20,22 +21,26 @@ impl<D, P: Ord> Item<D, P> {
}
// The relevant Ord implementations are based just on the priority
impl<D, P: Ord> Ord for Item<D, P> {
impl<D: Clone, P: PartialOrd + Clone> Ord for Item<D, P> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority)
// Yeah this is bad design
// My excuse is that i'm still learning Rust
self.priority
.partial_cmp(&other.priority)
.unwrap_or(Ordering::Equal)
}
}
impl<D, P: Ord> PartialOrd for Item<D, P> {
impl<D: Clone, P: PartialOrd + Clone> PartialOrd for Item<D, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
self.priority.partial_cmp(&other.priority)
}
}
impl<D, P: Ord> PartialEq for Item<D, P> {
impl<D: Clone, P: PartialOrd + Clone> PartialEq for Item<D, P> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}
impl<D, P: Ord> Eq for Item<D, P> {}
impl<D: Clone, P: PartialOrd + Clone> Eq for Item<D, P> {}