From ee004bac19a992e9810a24af3bcf64d922b19993 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Fri, 10 Jan 2025 20:02:52 +1300 Subject: [PATCH] Rename Item -> containers::Pair --- src/backing/containers/mod.rs | 3 +++ src/backing/{item.rs => containers/pair.rs} | 12 ++++++------ src/backing/indexed/mod.rs | 4 ++-- src/backing/mod.rs | 2 +- src/queue/paired.rs | 16 ++++++++-------- 5 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 src/backing/containers/mod.rs rename src/backing/{item.rs => containers/pair.rs} (75%) diff --git a/src/backing/containers/mod.rs b/src/backing/containers/mod.rs new file mode 100644 index 0000000..79f8058 --- /dev/null +++ b/src/backing/containers/mod.rs @@ -0,0 +1,3 @@ +mod pair; + +pub use pair::Pair; diff --git a/src/backing/item.rs b/src/backing/containers/pair.rs similarity index 75% rename from src/backing/item.rs rename to src/backing/containers/pair.rs index 8c926a5..a204cf9 100644 --- a/src/backing/item.rs +++ b/src/backing/containers/pair.rs @@ -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 { +pub struct Pair { data: D, priority: P, } -impl Item { +impl Pair { /// Creates a new instance pub fn new(data: D, priority: P) -> Self { Self { data, priority } @@ -21,7 +21,7 @@ impl Item { } // The relevant Ord implementations are based just on the priority -impl Ord for Item { +impl Ord for Pair { 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 Ord for Item { } } -impl PartialOrd for Item { +impl PartialOrd for Pair { fn partial_cmp(&self, other: &Self) -> Option { self.priority.partial_cmp(&other.priority) } } -impl PartialEq for Item { +impl PartialEq for Pair { fn eq(&self, other: &Self) -> bool { self.priority == other.priority } } -impl Eq for Item {} +impl Eq for Pair {} diff --git a/src/backing/indexed/mod.rs b/src/backing/indexed/mod.rs index ebc1ad5..c729567 100644 --- a/src/backing/indexed/mod.rs +++ b/src/backing/indexed/mod.rs @@ -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: - PureBacking> + PureBacking> { /// Update an item's priority fn update(data: D, priority: P) -> Result<(), ()>; diff --git a/src/backing/mod.rs b/src/backing/mod.rs index 1c34cd3..65d0cb1 100644 --- a/src/backing/mod.rs +++ b/src/backing/mod.rs @@ -1,3 +1,3 @@ +pub mod containers; pub mod indexed; -pub mod item; pub mod pure; diff --git a/src/queue/paired.rs b/src/queue/paired.rs index 84f743f..611524e 100644 --- a/src/queue/paired.rs +++ b/src/queue/paired.rs @@ -1,6 +1,6 @@ /// A "paired" priority queue that links some data to a priority and supports duplicates, but not arbitrary deletions or weight updates use crate::backing::{ - item::Item, + containers::Pair, pure::{BinaryHeap, PureBacking}, }; use pyo3::{ @@ -11,7 +11,7 @@ use pyo3::{ #[pyclass] pub struct PairedQueue { - backing: Box, f64>>>, + backing: Box, f64>>>, } #[pymethods] @@ -62,12 +62,12 @@ impl PairedQueue { } fn __setitem__(mut self_: PyRefMut<'_, Self>, key: Py, value: f64) { - self_.backing.add(Item::new(key, value)); + self_.backing.add(Pair::new(key, value)); } } impl<'py> PairedQueue { - fn from_any(object: &Bound<'py, PyAny>) -> PyResult, f64>>> { + fn from_any(object: &Bound<'py, PyAny>) -> PyResult, f64>>> { if let Ok(vec) = object.extract::, f64)>>() { Ok(Self::from_vec(vec)) } else { @@ -87,17 +87,17 @@ impl<'py> PairedQueue { } } - fn from_vec(list: Vec<(Py, f64)>) -> Vec, f64>> { + fn from_vec(list: Vec<(Py, f64)>) -> Vec, f64>> { list.into_iter() - .map(|(data, priority)| Item::new(data, priority)) + .map(|(data, priority)| Pair::new(data, priority)) .collect() } - fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult, f64>>> { + fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult, f64>>> { if let Ok(items) = dict .into_iter() .map(|(data, priority)| match priority.extract::() { - Ok(value) => Ok(Item::new(data.unbind(), value)), + Ok(value) => Ok(Pair::new(data.unbind(), value)), Err(err) => Err(err), }) .collect::, _>>()