Compare commits
2 commits
ee3cb08fe6
...
661e1d220a
Author | SHA1 | Date | |
---|---|---|---|
661e1d220a | |||
6bb65f6493 |
6 changed files with 67 additions and 42 deletions
41
src/backing/item.rs
Normal file
41
src/backing/item.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
/// Helper struct to associate an item with its priority
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Item<D, P: Ord> {
|
||||||
|
data: D,
|
||||||
|
priority: P,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D, P: Ord> Item<D, P> {
|
||||||
|
/// Creates a new instance
|
||||||
|
fn new(data: D, priority: P) -> Self {
|
||||||
|
Self { data, priority }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieve the internal data, it would be nicer to implement this using [`From`] or [`Into`], but I don't see a way to do that using generics
|
||||||
|
fn data(self) -> D {
|
||||||
|
self.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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> {}
|
|
@ -1,34 +1,5 @@
|
||||||
/// Data structures for the "keyed" min-queues, supporting priority updates and arbitrary removals, but no duplicates
|
/// Data structures for the "keyed" min-queues, supporting priority updates and arbitrary removals, but no duplicates
|
||||||
use super::pure::PureBacking;
|
use super::{item::Item, 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
|
/// A data structure usable for backing a "keyed" queue
|
||||||
pub trait KeyedBacking<D, P: Ord>: PureBacking<Item<D, P>> {
|
pub trait KeyedBacking<D, P: Ord>: PureBacking<Item<D, P>> {
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
pub mod item;
|
||||||
pub mod keyed;
|
pub mod keyed;
|
||||||
pub mod pure;
|
pub mod pure;
|
||||||
|
|
|
@ -103,6 +103,18 @@ impl<T: Ord + Copy> BinaryHeap<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Ord + Copy> FromIterator<T> for BinaryHeap<T> {
|
||||||
|
fn from_iter<U: IntoIterator<Item = T>>(iter: U) -> Self {
|
||||||
|
let mut this = Self {
|
||||||
|
data: Vec::from_iter(iter),
|
||||||
|
};
|
||||||
|
for i in (0..=(this.data.len() / 2)).rev() {
|
||||||
|
this.sift_down(i);
|
||||||
|
}
|
||||||
|
this
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Ord + Copy> PureBacking<T> for BinaryHeap<T> {
|
impl<T: Ord + Copy> PureBacking<T> for BinaryHeap<T> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self { data: vec![] }
|
Self { data: vec![] }
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
pub mod binary_heap;
|
pub mod binary_heap;
|
||||||
|
|
||||||
/// A data structure usable for backing a "pure" queue
|
/// A data structure usable for backing a "pure" queue
|
||||||
pub trait PureBacking<T: Ord> {
|
pub trait PureBacking<T: Ord>: FromIterator<T> {
|
||||||
/// Instantiates a new data structure
|
/// Instantiates a new data structure
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
/// Places an item into the queue
|
/// Places an item into the queue
|
||||||
|
|
|
@ -3,26 +3,26 @@ mod tests {
|
||||||
use pyority_queue::backing::pure::{binary_heap::BinaryHeap, PureBacking};
|
use pyority_queue::backing::pure::{binary_heap::BinaryHeap, PureBacking};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pure_binary_heap() {
|
fn test_pure_binary_heap_manual_creation() {
|
||||||
let mut heap = BinaryHeap::new();
|
let mut heap = BinaryHeap::new();
|
||||||
assert_eq!(heap.len(), 0);
|
|
||||||
heap.add(4);
|
heap.add(4);
|
||||||
assert_eq!(heap.len(), 1);
|
|
||||||
heap.add(-3);
|
heap.add(-3);
|
||||||
assert_eq!(heap.len(), 2);
|
|
||||||
heap.add(6);
|
heap.add(6);
|
||||||
assert_eq!(heap.len(), 3);
|
|
||||||
heap.add(1);
|
heap.add(1);
|
||||||
assert_eq!(heap.len(), 4);
|
|
||||||
assert_eq!(heap.pop(), Some(-3));
|
assert_eq!(heap.pop(), Some(-3));
|
||||||
assert_eq!(heap.len(), 3);
|
|
||||||
assert_eq!(heap.pop(), Some(1));
|
assert_eq!(heap.pop(), Some(1));
|
||||||
assert_eq!(heap.len(), 2);
|
|
||||||
assert_eq!(heap.pop(), Some(4));
|
assert_eq!(heap.pop(), Some(4));
|
||||||
assert_eq!(heap.len(), 1);
|
|
||||||
assert_eq!(heap.pop(), Some(6));
|
assert_eq!(heap.pop(), Some(6));
|
||||||
assert_eq!(heap.len(), 0);
|
|
||||||
assert_eq!(heap.pop(), None);
|
assert_eq!(heap.pop(), None);
|
||||||
assert_eq!(heap.len(), 0);
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pure_binary_heap_from_iter() {
|
||||||
|
let mut heap = BinaryHeap::from_iter(vec![7, 3, 6, 9]);
|
||||||
|
assert_eq!(heap.pop(), Some(3));
|
||||||
|
assert_eq!(heap.pop(), Some(6));
|
||||||
|
assert_eq!(heap.pop(), Some(7));
|
||||||
|
assert_eq!(heap.pop(), Some(9));
|
||||||
|
assert_eq!(heap.pop(), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue