diff --git a/src/backing/containers/py_item.rs b/src/backing/containers/py_item.rs index 9c7110c..81c38af 100644 --- a/src/backing/containers/py_item.rs +++ b/src/backing/containers/py_item.rs @@ -8,12 +8,12 @@ pub struct PyItem(Py); impl PyItem { /// Creates a new instance - fn new(item: Py) -> Self { + pub fn new(item: Py) -> Self { PyItem(item) } /// Retrieves the internal data - fn item(self) -> Py { + pub fn data(self) -> Py { self.0 } } diff --git a/src/queue/paired.rs b/src/queue/paired.rs index 69aaee4..5bb5f06 100644 --- a/src/queue/paired.rs +++ b/src/queue/paired.rs @@ -1,4 +1,3 @@ -/// A "paired" priority queue that links some data to a priority and supports duplicates, but not arbitrary deletions or weight updates use crate::backing::{ containers::Pair, pure::{BinaryHeap, PureBacking}, @@ -14,6 +13,7 @@ pub struct PairedQueue { backing: Box, f64>>>, } +/// A "paired" priority queue that links some data to a priority and supports duplicates, but not arbitrary deletions or priority updates #[pymethods] impl PairedQueue { /// Enables generic typing diff --git a/src/queue/pure.rs b/src/queue/pure.rs index d040ff8..f15a1c8 100644 --- a/src/queue/pure.rs +++ b/src/queue/pure.rs @@ -1,4 +1,8 @@ -use pyo3::{prelude::*, types::PyType}; +use pyo3::{ + exceptions::{PyIndexError, PyStopIteration, PyTypeError}, + prelude::*, + types::PyType, +}; use crate::backing::{ containers::PyItem, @@ -10,23 +14,64 @@ pub struct PureQueue { backing: Box>, } +/// A "pure" priority queue just contains the priorities without any linked data, and does not support arbitrary deletions or priority updates #[pymethods] impl PureQueue { - /// Enables generic typing - #[classmethod] - fn __class_getitem__(cls_: Bound<'_, PyType>, _key: Py) -> Bound<'_, PyType> { - cls_ - } - #[new] #[pyo3(signature = (items=None))] fn new(items: Option>) -> PyResult { if let Some(py_object) = items { - todo!() + Python::with_gil(|py| { + if let Ok(vec) = py_object.extract::>>(py) { + Ok(Self { + backing: Box::new(BinaryHeap::from_iter(vec.into_iter().map(PyItem::new))), + }) + } else { + Err(PyErr::new::("Items was not a sequence")) + } + }) } else { Ok(Self { backing: Box::new(BinaryHeap::new()), }) } } + + fn insert(mut self_: PyRefMut<'_, Self>, item: Py) { + self_.backing.add(PyItem::new(item)); + } + + // Below methods are identical to PairedQueue + // Normally I'd try to solve using a trait with default implementations but that doesn't seem to play nice with #[pymethods] + // (Although I shouldn't rule it out) + + /// Enables generic typing + #[classmethod] + fn __class_getitem__(cls_: Bound<'_, PyType>, _key: Py) -> Bound<'_, PyType> { + cls_ + } + + fn __len__(self_: PyRef<'_, Self>) -> usize { + self_.backing.len() + } + + fn __iter__(self_: PyRef<'_, Self>) -> PyRef<'_, Self> { + self_ + } + + fn __next__(mut self_: PyRefMut<'_, Self>) -> PyResult> { + if let Some(item) = self_.backing.pop() { + Ok(item.data()) + } else { + Err(PyErr::new::(())) + } + } + + fn pop(mut self_: PyRefMut<'_, Self>) -> PyResult> { + if let Some(item) = self_.backing.pop() { + Ok(item.data()) + } else { + Err(PyErr::new::(())) + } + } }