Simplify IndexedQueue API
Although similar to PureQueue, it doesn't make sense to use the exact same signatures. Also don't make API users use Pair<D, P>.
This commit is contained in:
parent
16c1a4d390
commit
63515e2314
3 changed files with 40 additions and 56 deletions
|
@ -1,11 +1,11 @@
|
|||
use pyo3::{
|
||||
exceptions::{PyIndexError, PyKeyError, PyRuntimeError, PyStopIteration, PyTypeError},
|
||||
exceptions::{PyIndexError, PyKeyError, PyStopIteration, PyTypeError},
|
||||
prelude::*,
|
||||
types::{PyDict, PyType},
|
||||
};
|
||||
|
||||
use crate::backing::{
|
||||
containers::{Pair, PyItem},
|
||||
containers::PyItem,
|
||||
indexed::{IndexedBacking, IndexedBinaryHeap},
|
||||
};
|
||||
|
||||
|
@ -35,26 +35,16 @@ impl IndexedQueue {
|
|||
}
|
||||
}
|
||||
|
||||
fn __setitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>, value: f64) -> PyResult<()> {
|
||||
let data = PyItem::new(key);
|
||||
if self_.backing.contains(&data) {
|
||||
if self_.backing.update(data, value) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PyErr::new::<PyRuntimeError, _>("Key could not be updated"))
|
||||
}
|
||||
} else {
|
||||
Ok(self_.backing.add(Pair::new(data, value)))
|
||||
}
|
||||
fn __setitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>, value: f64) {
|
||||
self_.backing.set(PyItem::new(key), value)
|
||||
}
|
||||
|
||||
fn __delitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>) -> PyResult<()> {
|
||||
if self_.backing.remove(PyItem::new(key)) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PyErr::new::<PyKeyError, _>(
|
||||
match self_.backing.remove(PyItem::new(key)) {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(PyErr::new::<PyKeyError, _>(
|
||||
"Key was not contained in queue",
|
||||
))
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,7 +68,7 @@ impl IndexedQueue {
|
|||
|
||||
fn __next__(mut self_: PyRefMut<'_, Self>) -> PyResult<Py<PyAny>> {
|
||||
if let Some(item) = self_.backing.pop() {
|
||||
Ok(item.data().data())
|
||||
Ok(item.data())
|
||||
} else {
|
||||
Err(PyErr::new::<PyStopIteration, _>(()))
|
||||
}
|
||||
|
@ -86,7 +76,7 @@ impl IndexedQueue {
|
|||
|
||||
fn pop(mut self_: PyRefMut<'_, Self>) -> PyResult<Py<PyAny>> {
|
||||
if let Some(item) = self_.backing.pop() {
|
||||
Ok(item.data().data())
|
||||
Ok(item.data())
|
||||
} else {
|
||||
Err(PyErr::new::<PyIndexError, _>(()))
|
||||
}
|
||||
|
@ -95,7 +85,7 @@ impl IndexedQueue {
|
|||
|
||||
impl<'py> IndexedQueue {
|
||||
/// Tries to a Python object into a vector suitable for ingestion into the backing
|
||||
fn from_any(object: &Bound<'py, PyAny>) -> PyResult<Vec<Pair<PyItem, f64>>> {
|
||||
fn from_any(object: &Bound<'py, PyAny>) -> PyResult<Vec<(PyItem, f64)>> {
|
||||
if let Ok(vec) = object.extract::<Vec<(Py<PyAny>, f64)>>() {
|
||||
Ok(Self::from_vec(vec))
|
||||
} else {
|
||||
|
@ -116,18 +106,18 @@ impl<'py> IndexedQueue {
|
|||
}
|
||||
|
||||
/// Converts a vector of Python objects and priorities into a vector of items
|
||||
fn from_vec(list: Vec<(Py<PyAny>, f64)>) -> Vec<Pair<PyItem, f64>> {
|
||||
fn from_vec(list: Vec<(Py<PyAny>, f64)>) -> Vec<(PyItem, f64)> {
|
||||
list.into_iter()
|
||||
.map(|(data, priority)| Pair::new(PyItem::new(data), priority))
|
||||
.map(|(data, priority)| (PyItem::new(data), priority))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Converts a Python dictionary into a vector of items
|
||||
fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult<Vec<Pair<PyItem, f64>>> {
|
||||
fn from_dict(dict: &Bound<'py, PyDict>) -> PyResult<Vec<(PyItem, f64)>> {
|
||||
if let Ok(items) = dict
|
||||
.into_iter()
|
||||
.map(|(data, priority)| match priority.extract::<f64>() {
|
||||
Ok(value) => Ok(Pair::new(PyItem::new(data.unbind()), value)),
|
||||
Ok(value) => Ok((PyItem::new(data.unbind()), value)),
|
||||
Err(err) => Err(err),
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue