Scaffold crate to manage data structures
This commit is contained in:
parent
e51655f6e4
commit
28e780d274
4 changed files with 56 additions and 0 deletions
39
src/backing/keyed/mod.rs
Normal file
39
src/backing/keyed/mod.rs
Normal 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
2
src/backing/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod keyed;
|
||||
pub mod pure;
|
13
src/backing/pure/mod.rs
Normal file
13
src/backing/pure/mod.rs
Normal 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;
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
pub mod backing;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// Formats the sum of two numbers as string.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue