Move Item to its own file

This commit is contained in:
Michael Bradley 2025-01-04 23:31:58 +13:00
parent 6bb65f6493
commit 661e1d220a
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
3 changed files with 43 additions and 30 deletions

41
src/backing/item.rs Normal file
View file

@ -0,0 +1,41 @@
use std::cmp::Ordering;
/// Helper struct to associate an item with its priority
#[derive(Debug, Clone, Copy)]
pub struct Item<D, P: Ord> {
data: D,
priority: P,
}
impl<D, P: Ord> Item<D, P> {
/// Creates a new instance
fn new(data: D, priority: P) -> Self {
Self { data, priority }
}
/// Retrieve the internal data, it would be nicer to implement this using [`From`] or [`Into`], but I don't see a way to do that using generics
fn data(self) -> D {
self.data
}
}
// The relevant Ord implementations are based just on the priority
impl<D, P: Ord> Ord for Item<D, P> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority)
}
}
impl<D, P: Ord> PartialOrd for Item<D, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<D, P: Ord> PartialEq for Item<D, P> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}
impl<D, P: Ord> Eq for Item<D, P> {}

View file

@ -1,34 +1,5 @@
/// Data structures for the "keyed" min-queues, supporting priority updates and arbitrary removals, but no duplicates
use super::pure::PureBacking;
use std::cmp::Ordering;
/// Helper struct to associate an item with its priority
#[derive(Debug, Copy, Clone)]
pub struct Item<D, P: Ord> {
data: D,
priority: P,
}
// The relevant Ord implementations are based just on the priority
impl<D, P: Ord> Ord for Item<D, P> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority)
}
}
impl<D, P: Ord> PartialOrd for Item<D, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<D, P: Ord> PartialEq for Item<D, P> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}
impl<D, P: Ord> Eq for Item<D, P> {}
use super::{item::Item, pure::PureBacking};
/// A data structure usable for backing a "keyed" queue
pub trait KeyedBacking<D, P: Ord>: PureBacking<Item<D, P>> {

View file

@ -1,2 +1,3 @@
pub mod item;
pub mod keyed;
pub mod pure;