Scaffold crate to manage data structures

This commit is contained in:
Michael Bradley 2025-01-04 00:13:17 +13:00
parent e51655f6e4
commit 28e780d274
Signed by: MichaelBradley
SSH key fingerprint: SHA256:cj/YZ5VT+QOKncqSkx+ibKTIn0Obg7OIzwzl9BL8EO8
4 changed files with 56 additions and 0 deletions

39
src/backing/keyed/mod.rs Normal file
View file

@ -0,0 +1,39 @@
/// Data structures for the "keyed" min-queues, supporting priority updates and arbitrary removals, but no duplicates
use super::pure::PureBacking;
use std::cmp::Ordering;
/// Helper struct to associate an item with its priority
#[derive(Debug, Copy, Clone)]
pub struct Item<D, P: Ord> {
data: D,
priority: P,
}
// The relevant Ord implementations are based just on the priority
impl<D, P: Ord> Ord for Item<D, P> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority)
}
}
impl<D, P: Ord> PartialOrd for Item<D, P> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<D, P: Ord> PartialEq for Item<D, P> {
fn eq(&self, other: &Self) -> bool {
self.priority == other.priority
}
}
impl<D, P: Ord> Eq for Item<D, P> {}
/// A data structure usable for backing a "keyed" queue
pub trait KeyedBacking<D, P: Ord>: PureBacking<Item<D, P>> {
/// Update an item's priority
fn update(data: D, priority: P) -> Result<(), ()>;
/// Remove an item from the queue
fn remove(data: D) -> bool;
}

2
src/backing/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod keyed;
pub mod pure;

13
src/backing/pure/mod.rs Normal file
View file

@ -0,0 +1,13 @@
/// Data structures for the "pure" min-queues, supporting duplicates but no arbitrary updates
/// A data structure usable for backing a "pure" queue
pub trait PureBacking<T: Ord> {
/// Instantiates a new data structure
fn new() -> Self;
/// Places an item into the queue
fn add(&mut self, item: T);
/// Removes the item with minimum priority, if it exists
fn pop(&mut self) -> Option<T>;
/// The number of items in the queue
fn len(&self) -> usize;
}

View file

@ -1,3 +1,5 @@
pub mod backing;
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.