Address Clippy lints
Some checks failed
CI / Formatting (push) Successful in 39s
CI / Clippy (push) Failing after 59s
CI / Build (push) Successful in 1m28s
CI / Python tests (push) Successful in 38s
CI / Rust tests (push) Successful in 1m18s

This commit is contained in:
Michael Bradley 2025-02-23 09:50:20 -05:00
parent b1d49624d4
commit 8ed5e410ff
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
6 changed files with 109 additions and 102 deletions

View file

@ -13,6 +13,14 @@ pub struct IndexedBinaryHeap<
indices: HashMap<D, usize>,
}
impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync> Default
for IndexedBinaryHeap<D, P>
{
fn default() -> Self {
Self::new()
}
}
impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync>
IndexedBinaryHeap<D, P>
{
@ -54,8 +62,7 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync>
if i > self.data.len() {
// Tried to sift a non-existent index
Err(SiftError::new(i, self.data.len()))
} else {
if let Some(first_child) = self.data.get(i * 2 + 1).cloned() {
} else if let Some(first_child) = self.data.get(i * 2 + 1).cloned() {
let smaller_child_index;
let smaller_child;
@ -98,7 +105,6 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync>
Ok(())
}
}
}
fn delete_pair(&mut self, i: usize) -> Option<Pair<D, P>> {
if i >= self.data.len() {
@ -147,6 +153,10 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync> In
self.data.len()
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
fn contains(&self, data: &D) -> bool {
self.indices.contains_key(data)
}
@ -164,7 +174,7 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync> In
}
} else {
let final_index = self.data.len();
if let Some(_) = self.indices.insert(data.clone(), final_index) {
if self.indices.insert(data.clone(), final_index).is_some() {
panic!("Item was not consistently hashed")
}
self.data.push(Pair::new(data, priority));
@ -174,21 +184,14 @@ impl<D: Hash + Eq + Clone + Send + Sync, P: PartialOrd + Clone + Send + Sync> In
fn remove(&mut self, data: D) -> Option<P> {
if let Some(index) = self.indices.get(&data) {
if let Some(pair) = self.delete_pair(*index) {
Some(pair.get_priority().clone())
} else {
None
}
self.delete_pair(*index)
.map(|pair| pair.get_priority().clone())
} else {
None
}
}
fn pop(&mut self) -> Option<D> {
if let Some(pair) = self.delete_pair(0) {
Some(pair.data())
} else {
None
}
self.delete_pair(0).map(|pair| pair.data())
}
}

View file

@ -9,6 +9,8 @@ pub trait IndexedBacking<D: Clone + Send + Sync, P: PartialOrd + Clone + Send +
{
/// The length of the queue
fn len(&self) -> usize;
/// Whether the queue is empty
fn is_empty(&self) -> bool;
/// Check if an item is already in the queue
fn contains(&self, data: &D) -> bool;
/// Set an item's priority, will update if the item is already enqueued

View file

@ -8,6 +8,12 @@ pub struct BinaryHeap<T: PartialOrd + Clone + Send + Sync> {
data: Vec<T>,
}
impl<T: PartialOrd + Clone + Send + Sync> Default for BinaryHeap<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: PartialOrd + Clone + Send + Sync> BinaryHeap<T> {
/// Instantiates a new (empty) binary heap
pub fn new() -> Self {
@ -43,8 +49,7 @@ impl<T: PartialOrd + Clone + Send + Sync> BinaryHeap<T> {
if i > self.data.len() {
// Tried to sift a non-existent index
Err(SiftError::new(i, self.data.len()))
} else {
if let Some(first_child) = self.data.get(i * 2 + 1).cloned() {
} else if let Some(first_child) = self.data.get(i * 2 + 1).cloned() {
let smaller_child_index;
let smaller_child;
@ -80,7 +85,6 @@ impl<T: PartialOrd + Clone + Send + Sync> BinaryHeap<T> {
Ok(())
}
}
}
}
impl<T: PartialOrd + Clone + Send + Sync> FromIterator<T> for BinaryHeap<T> {
@ -127,4 +131,8 @@ impl<T: PartialOrd + Clone + Send + Sync> PureBacking<T> for BinaryHeap<T> {
fn len(&self) -> usize {
self.data.len()
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
}

View file

@ -10,4 +10,6 @@ pub trait PureBacking<T: PartialOrd + Send + Sync>: Send + Sync {
fn pop(&mut self) -> Option<T>;
/// The number of items in the queue
fn len(&self) -> usize;
/// Whether the queue is empty
fn is_empty(&self) -> bool;
}

View file

@ -23,11 +23,9 @@ impl IndexedQueue {
#[pyo3(signature = (items=None))]
fn new(items: Option<Py<PyAny>>) -> PyResult<Self> {
if let Some(py_object) = items {
Python::with_gil(|py| Self::from_any(py_object.bind(py))).and_then(|vec| {
Ok(Self {
Python::with_gil(|py| Self::from_any(py_object.bind(py))).map(|vec| Self {
backing: Box::new(IndexedBinaryHeap::from_iter(vec)),
})
})
} else {
Ok(Self {
backing: Box::new(IndexedBinaryHeap::new()),
@ -88,8 +86,7 @@ impl<'py> IndexedQueue {
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 {
if object.is_instance_of::<PyDict>() {
} else if object.is_instance_of::<PyDict>() {
if let Ok(dict) = object.downcast::<PyDict>() {
Self::from_dict(dict)
} else {
@ -103,7 +100,6 @@ 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<(PyItem, f64)> {

View file

@ -26,11 +26,9 @@ impl PairedQueue {
#[pyo3(signature = (items=None))]
fn new(items: Option<Py<PyAny>>) -> PyResult<Self> {
if let Some(py_object) = items {
Python::with_gil(|py| Self::from_any(py_object.bind(py))).and_then(|vec| {
Ok(Self {
Python::with_gil(|py| Self::from_any(py_object.bind(py))).map(|vec| Self {
backing: Box::new(BinaryHeap::from_iter(vec)),
})
})
} else {
Ok(Self {
backing: Box::new(BinaryHeap::new()),
@ -72,8 +70,7 @@ impl<'py> PairedQueue {
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 {
if object.is_instance_of::<PyDict>() {
} else if object.is_instance_of::<PyDict>() {
if let Ok(dict) = object.downcast::<PyDict>() {
Self::from_dict(dict)
} else {
@ -87,7 +84,6 @@ impl<'py> PairedQueue {
))
}
}
}
/// Converts a vector of Python objects and priorities into a vector of items
fn from_vec(list: Vec<(Py<PyAny>, f64)>) -> Vec<Pair<Py<PyAny>, f64>> {