Scaffold IndexedQueue and IndexedBinaryHeap

This commit is contained in:
Michael Bradley 2025-01-11 22:38:32 +13:00
parent 64030bd349
commit fec9ccffc6
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
3 changed files with 116 additions and 4 deletions

View file

@ -1,12 +1,67 @@
use pyo3::prelude::*;
use pyo3::{
exceptions::{PyIndexError, PyStopIteration},
prelude::*,
types::PyType,
};
use crate::backing::indexed::IndexedBacking;
#[pyclass]
pub struct IndexedQueue {}
pub struct IndexedQueue {
backing: Box<dyn IndexedBacking<Py<PyAny>, f64>>,
}
#[pymethods]
impl IndexedQueue {
#[new]
fn new() -> Self {
Self {}
#[pyo3(signature = (items=None))]
fn new(items: Option<Py<PyAny>>) -> PyResult<Self> {
if let Some(py_object) = items {
todo!()
} else {
todo!() // TBD: Determine best way to make Python object hashable from Rust
}
}
fn __setitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>, value: Py<PyAny>) {
todo!()
}
fn __delitem__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>) {
todo!()
}
fn __contains__(mut self_: PyRefMut<'_, Self>, key: Py<PyAny>) {
todo!()
}
/// Enables generic typing
#[classmethod]
fn __class_getitem__(cls_: Bound<'_, PyType>, _key: Py<PyAny>) -> 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<Py<PyAny>> {
if let Some(item) = self_.backing.pop() {
Ok(item.data())
} else {
Err(PyErr::new::<PyStopIteration, _>(()))
}
}
fn pop(mut self_: PyRefMut<'_, Self>) -> PyResult<Py<PyAny>> {
if let Some(item) = self_.backing.pop() {
Ok(item.data())
} else {
Err(PyErr::new::<PyIndexError, _>(()))
}
}
}