dag: Improve trait bounds
This commit is contained in:
parent
b60569d3cb
commit
5f731fad0e
|
|
@ -5,13 +5,12 @@ use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
collections::{BTreeMap, BTreeSet, VecDeque},
|
collections::{BTreeMap, BTreeSet, VecDeque},
|
||||||
fmt,
|
fmt,
|
||||||
hash::Hash,
|
|
||||||
ops::{ControlFlow, Deref, Index},
|
ops::{ControlFlow, Deref, Index},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A node in the graph.
|
/// A node in the graph.
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct Node<K: Eq, V> {
|
pub struct Node<K, V> {
|
||||||
/// The node value, stored by the user.
|
/// The node value, stored by the user.
|
||||||
pub value: V,
|
pub value: V,
|
||||||
/// Nodes depended on.
|
/// Nodes depended on.
|
||||||
|
|
@ -20,7 +19,7 @@ pub struct Node<K: Eq, V> {
|
||||||
pub dependents: BTreeSet<K>,
|
pub dependents: BTreeSet<K>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Eq + Hash, V> Node<K, V> {
|
impl<K, V> Node<K, V> {
|
||||||
fn new(value: V) -> Self {
|
fn new(value: V) -> Self {
|
||||||
Self {
|
Self {
|
||||||
value,
|
value,
|
||||||
|
|
@ -30,13 +29,13 @@ impl<K: Eq + Hash, V> Node<K, V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Eq + Hash, V> Borrow<V> for &Node<K, V> {
|
impl<K, V> Borrow<V> for &Node<K, V> {
|
||||||
fn borrow(&self) -> &V {
|
fn borrow(&self) -> &V {
|
||||||
&self.value
|
&self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: Eq + Hash, V> Deref for Node<K, V> {
|
impl<K, V> Deref for Node<K, V> {
|
||||||
type Target = V;
|
type Target = V;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
|
|
@ -46,13 +45,13 @@ impl<K: Eq + Hash, V> Deref for Node<K, V> {
|
||||||
|
|
||||||
/// A directed acyclic graph.
|
/// A directed acyclic graph.
|
||||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||||
pub struct Dag<K: Eq + Hash, V> {
|
pub struct Dag<K, V> {
|
||||||
graph: BTreeMap<K, Node<K, V>>,
|
graph: BTreeMap<K, Node<K, V>>,
|
||||||
tips: BTreeSet<K>,
|
tips: BTreeSet<K>,
|
||||||
roots: BTreeSet<K>,
|
roots: BTreeSet<K>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialOrd + Ord + Eq + Copy + Hash, V> Dag<K, V> {
|
impl<K: Ord + Copy, V> Dag<K, V> {
|
||||||
/// Create a new empty DAG.
|
/// Create a new empty DAG.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -256,7 +255,7 @@ impl<K: PartialOrd + Ord + Eq + Copy + Hash, V> Dag<K, V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: PartialOrd + Ord + Eq + Copy + Hash + fmt::Debug, V> Index<&K> for Dag<K, V> {
|
impl<K: Ord + Copy + fmt::Debug, V> Index<&K> for Dag<K, V> {
|
||||||
type Output = Node<K, V>;
|
type Output = Node<K, V>;
|
||||||
|
|
||||||
fn index(&self, key: &K) -> &Self::Output {
|
fn index(&self, key: &K) -> &Self::Output {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue