From 661e1d220ae71002d66d2ba2d3dfddfe4ff1035d Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sat, 4 Jan 2025 23:31:58 +1300 Subject: [PATCH] Move Item to its own file --- src/backing/item.rs | 41 ++++++++++++++++++++++++++++++++++++++++ src/backing/keyed/mod.rs | 31 +----------------------------- src/backing/mod.rs | 1 + 3 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 src/backing/item.rs diff --git a/src/backing/item.rs b/src/backing/item.rs new file mode 100644 index 0000000..424bf45 --- /dev/null +++ b/src/backing/item.rs @@ -0,0 +1,41 @@ +use std::cmp::Ordering; + +/// Helper struct to associate an item with its priority +#[derive(Debug, Clone, Copy)] +pub struct Item { + data: D, + priority: P, +} + +impl Item { + /// 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 Ord for Item { + fn cmp(&self, other: &Self) -> Ordering { + self.priority.cmp(&other.priority) + } +} + +impl PartialOrd for Item { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for Item { + fn eq(&self, other: &Self) -> bool { + self.priority == other.priority + } +} + +impl Eq for Item {} diff --git a/src/backing/keyed/mod.rs b/src/backing/keyed/mod.rs index a574e73..58077c5 100644 --- a/src/backing/keyed/mod.rs +++ b/src/backing/keyed/mod.rs @@ -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 { - data: D, - priority: P, -} - -// The relevant Ord implementations are based just on the priority -impl Ord for Item { - fn cmp(&self, other: &Self) -> Ordering { - self.priority.cmp(&other.priority) - } -} - -impl PartialOrd for Item { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl PartialEq for Item { - fn eq(&self, other: &Self) -> bool { - self.priority == other.priority - } -} - -impl Eq for Item {} +use super::{item::Item, pure::PureBacking}; /// A data structure usable for backing a "keyed" queue pub trait KeyedBacking: PureBacking> { diff --git a/src/backing/mod.rs b/src/backing/mod.rs index ea87b12..f66e16c 100644 --- a/src/backing/mod.rs +++ b/src/backing/mod.rs @@ -1,2 +1,3 @@ +pub mod item; pub mod keyed; pub mod pure;