47 lines
1.2 KiB
Rust
47 lines
1.2 KiB
Rust
use std::cmp::Ordering;
|
|
|
|
/// Container to associate an item with a priority
|
|
#[derive(Debug, Clone, Copy)]
|
|
pub struct Pair<D: Clone, P: PartialOrd + Clone> {
|
|
data: D,
|
|
priority: P,
|
|
}
|
|
|
|
impl<D: Clone, P: PartialOrd + Clone> Pair<D, P> {
|
|
/// Creates a new instance
|
|
pub fn new(data: D, priority: P) -> Self {
|
|
Self { data, priority }
|
|
}
|
|
|
|
/// Returns the internal data.
|
|
/// It might(?) be nicer to implement this using [`From`] or [`Into`], but I don't see a way to do that using generics.
|
|
pub fn data(self) -> D {
|
|
self.data
|
|
}
|
|
|
|
pub fn get_data(&self) -> &D {
|
|
&self.data
|
|
}
|
|
|
|
/// Retrieve the priority associated with the data
|
|
pub fn get_priority(&self) -> &P {
|
|
&self.priority
|
|
}
|
|
|
|
/// Update the priority associated with the data
|
|
pub fn set_priority(&mut self, priority: P) {
|
|
self.priority = priority
|
|
}
|
|
}
|
|
|
|
impl<D: Clone, P: PartialOrd + Clone> PartialOrd for Pair<D, P> {
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
self.priority.partial_cmp(&other.priority)
|
|
}
|
|
}
|
|
|
|
impl<D: Clone, P: PartialOrd + Clone> PartialEq for Pair<D, P> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.priority == other.priority
|
|
}
|
|
}
|