diff --git a/src/backing/keyed/mod.rs b/src/backing/keyed/mod.rs new file mode 100644 index 0000000..a574e73 --- /dev/null +++ b/src/backing/keyed/mod.rs @@ -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 { + data: D, + priority: P, +} + +// The relevant Ord implementations are based just on the priority +impl Ord for Item { + fn cmp(&self, other: &Self) -> Ordering { + self.priority.cmp(&other.priority) + } +} + +impl PartialOrd for Item { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for Item { + fn eq(&self, other: &Self) -> bool { + self.priority == other.priority + } +} + +impl Eq for Item {} + +/// A data structure usable for backing a "keyed" queue +pub trait KeyedBacking: PureBacking> { + /// Update an item's priority + fn update(data: D, priority: P) -> Result<(), ()>; + /// Remove an item from the queue + fn remove(data: D) -> bool; +} diff --git a/src/backing/mod.rs b/src/backing/mod.rs new file mode 100644 index 0000000..ea87b12 --- /dev/null +++ b/src/backing/mod.rs @@ -0,0 +1,2 @@ +pub mod keyed; +pub mod pure; diff --git a/src/backing/pure/mod.rs b/src/backing/pure/mod.rs new file mode 100644 index 0000000..b2d076b --- /dev/null +++ b/src/backing/pure/mod.rs @@ -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 { + /// 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; + /// The number of items in the queue + fn len(&self) -> usize; +} diff --git a/src/lib.rs b/src/lib.rs index 75fa3a9..c2bf43e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +pub mod backing; + use pyo3::prelude::*; /// Formats the sum of two numbers as string.