Rename Item -> containers::Pair
This commit is contained in:
parent
e424dd42f5
commit
ee004bac19
5 changed files with 20 additions and 17 deletions
3
src/backing/containers/mod.rs
Normal file
3
src/backing/containers/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod pair;
|
||||
|
||||
pub use pair::Pair;
|
|
@ -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> {}
|
|
@ -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<(), ()>;
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod containers;
|
||||
pub mod indexed;
|
||||
pub mod item;
|
||||
pub mod pure;
|
||||
|
|
|
@ -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<dyn PureBacking<Item<Py<PyAny>, f64>>>,
|
||||
backing: Box<dyn PureBacking<Pair<Py<PyAny>, f64>>>,
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
|
@ -62,12 +62,12 @@ impl PairedQueue {
|
|||
}
|
||||
|
||||
fn __setitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>, 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<Vec<Item<Py<PyAny>, f64>>> {
|
||||
fn from_any(object: &Bound<'py, PyAny>) -> PyResult<Vec<Pair<Py<PyAny>, f64>>> {
|
||||
if let Ok(vec) = object.extract::<Vec<(Py<PyAny>, f64)>>() {
|
||||
Ok(Self::from_vec(vec))
|
||||
} else {
|
||||
|
@ -87,17 +87,17 @@ impl<'py> PairedQueue {
|
|||
}
|
||||
}
|
||||
|
||||
fn from_vec(list: Vec<(Py<PyAny>, f64)>) -> Vec<Item<Py<PyAny>, f64>> {
|
||||
fn from_vec(list: Vec<(Py<PyAny>, f64)>) -> Vec<Pair<Py<PyAny>, 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<Vec<Item<Py<PyAny>, f64>>> {
|
||||
fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult<Vec<Pair<Py<PyAny>, f64>>> {
|
||||
if let Ok(items) = dict
|
||||
.into_iter()
|
||||
.map(|(data, priority)| match priority.extract::<f64>() {
|
||||
Ok(value) => Ok(Item::new(data.unbind(), value)),
|
||||
Ok(value) => Ok(Pair::new(data.unbind(), value)),
|
||||
Err(err) => Err(err),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue