Rename Item -> containers::Pair

This commit is contained in:
Michael Bradley 2025-01-10 20:02:52 +13:00
parent e424dd42f5
commit ee004bac19
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
5 changed files with 20 additions and 17 deletions

View file

@ -0,0 +1,3 @@
mod pair;
pub use pair::Pair;

View file

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

View file

@ -1,9 +1,9 @@
/// Data structures for the "indexed" min-queues, supporting priority updates and arbitrary removals, but no duplicates
use super::{item::Item, pure::PureBacking};
use super::{containers::Pair, pure::PureBacking};
/// A data structure usable for backing an "indexed" queue
pub trait IndexedBacking<D: Clone + Send + Sync, P: Ord + Clone + Send + Sync>:
PureBacking<Item<D, P>>
PureBacking<Pair<D, P>>
{
/// Update an item's priority
fn update(data: D, priority: P) -> Result<(), ()>;

View file

@ -1,3 +1,3 @@
pub mod containers;
pub mod indexed;
pub mod item;
pub mod pure;