-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
366 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::core::{ | ||
property::{AddEdge, NoLoops, NoLoopsGraph, Unique, UniqueGraph}, | ||
Ensure, Graph, GraphDerefMut, Undirected, | ||
}; | ||
use duplicate::duplicate_item; | ||
use std::borrow::Borrow; | ||
|
||
/// A marker trait for [simple graphs](https://mathworld.wolfram.com/SimpleGraph.html) | ||
pub trait Simple: NoLoops + Unique {} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct SimpleGraph<C: Ensure>(C); | ||
|
||
impl<C: Ensure> SimpleGraph<C> | ||
where | ||
C::Graph: Graph<EdgeWeight = (), Directedness = Undirected>, | ||
{ | ||
/// Constrains the given graph. | ||
/// | ||
/// The given graph must be simple. This is not checked by this function. | ||
pub fn unchecked(c: C) -> Self | ||
{ | ||
Self(c) | ||
} | ||
} | ||
|
||
impl<C: Ensure> Ensure for SimpleGraph<C> | ||
where | ||
C::Graph: Graph<EdgeWeight = (), Directedness = Undirected>, | ||
{ | ||
fn ensure_unvalidated(c: Self::Ensured, _: ()) -> Self | ||
{ | ||
Self(c) | ||
} | ||
|
||
fn validate(c: &Self::Ensured, _: &()) -> bool | ||
{ | ||
NoLoopsGraph::<C>::validate(c, &()) && UniqueGraph::validate(c, &()) | ||
} | ||
} | ||
|
||
impl<C: Ensure + GraphDerefMut> AddEdge for SimpleGraph<C> | ||
where | ||
C::Graph: AddEdge<EdgeWeight = (), Directedness = Undirected>, | ||
{ | ||
fn add_edge_weighted( | ||
&mut self, | ||
source: impl Borrow<Self::Vertex>, | ||
sink: impl Borrow<Self::Vertex>, | ||
weight: Self::EdgeWeight, | ||
) -> Result<(), ()> | ||
{ | ||
if !(source.borrow() != sink.borrow() | ||
&& Self::can_add_edge(self, source.borrow(), sink.borrow())) | ||
{ | ||
Err(()) | ||
} | ||
else | ||
{ | ||
self.0.graph_mut().add_edge_weighted(source, sink, weight) | ||
} | ||
} | ||
} | ||
|
||
#[duplicate_item( | ||
Prop; [Unique]; [NoLoops]; [Simple]; | ||
)] | ||
impl<C: Ensure> Prop for SimpleGraph<C> where | ||
C::Graph: Graph<EdgeWeight = (), Directedness = Undirected> | ||
{ | ||
} | ||
|
||
impl_ensurer! { | ||
use<C> SimpleGraph<C>: Ensure, Unique, NoLoops, Simple, AddEdge | ||
as (self.0) : C | ||
where C::Graph: Graph<EdgeWeight=(), Directedness=Undirected> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.